Skip to content
Open Sourceintermediate

What is Terraform?

Updated Nov 30, 20263 min readBased on official documentation
What is Terraform?

In short

Terraform is an infrastructure as code tool. You describe servers, networks, databases and other resources in plain configuration files, and Terraform creates and manages them on cloud platforms or on your own hardware. Because the files are text, you can version, reuse and share them like any other code.

What Terraform does

Terraform is an infrastructure as code tool, often called IaC. Instead of clicking through a cloud console to create a server or a network, you write the setup in configuration files. Terraform reads those files and makes the real infrastructure match them. It can work with cloud resources and with on-premises equipment.

The official documentation describes the files as human-readable, so anyone on your team can open them and see what the system should look like. Since they are plain text, you can store them in a version control system such as Git, review changes, and roll back when something goes wrong.

How it works

Terraform talks to platforms through their application programming interfaces, or APIs. An API is a set of rules that lets one program ask another program to do something, such as create a virtual machine. Terraform does not know how each cloud works by itself. Instead, it uses providers, which are plug-ins that translate your configuration into API calls for a specific platform or service.

The core workflow has three stages:

  • Write: you define the resources you want, possibly across several providers.
  • Plan: Terraform compares your configuration with the existing infrastructure and shows what it will create, change or destroy.
  • Apply: after you approve the plan, Terraform carries out the changes in the right order, respecting dependencies between resources.

Terraform also keeps a state file. This file records what it has built, and it acts as the source of truth for your environment. When you change the configuration later, Terraform compares it with the state file to decide what must change.

The configuration is declarative. You describe the end state you want, not the steps to reach it. Terraform works out the steps itself. It builds a graph of resource dependencies and creates independent resources in parallel, which saves time.

Reuse with modules and providers

A module is a reusable set of configuration files that groups related infrastructure. A team can write a module once for a standard web application and use it in many projects. Modules help keep setups consistent and encourage good practices. You can use public modules from the Terraform Registry or write your own.

Providers work the same way. The Terraform Registry lists many public providers for clouds, Kubernetes, GitHub and other services. You can also write a provider for a platform that is not listed.

When to use it

Terraform fits best when you manage infrastructure that changes over time, or when several people need to change the same environment. It is useful when you want to recreate an environment, for example a test copy of production, with the same results each time.

It is less useful for a single short experiment that you will delete in an hour. Writing configuration takes some effort, and for a tiny task a few manual steps may be faster.

A simple example

A configuration file might look like this. It declares a provider and one resource. The exact names depend on the provider you choose.

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name = "nginx:latest"
}

You would then run the plan stage to preview the change and the apply stage to create the image after you approve it. The first time you do this, follow the official Get Started tutorials, which cover popular cloud providers and Docker.

Common mistakes

Beginners often treat the state file as an unimportant detail. It is the record Terraform uses to decide what to change, so losing or editing it by hand can cause trouble. Store it carefully and avoid changing resources outside Terraform, because the state will no longer match reality.

Another common mistake is skipping the plan. Reading the plan before you apply shows you what will be destroyed or replaced, which helps prevent surprises.

Alternatives and where to go next

Terraform is not the only IaC tool. Other tools take different approaches, and the right choice depends on your platform and team. If you want to learn the basics first, start with the official introduction and the Get Started tutorials. The documentation also points to HashiCorp Discuss for questions, and to a contributing guide and issue tracker for bugs and feature requests.

FAQ

Is Terraform free to use?

Terraform is open source, and you can use it to manage your own infrastructure. HCP Terraform is a separate managed service that adds team features such as shared state and access controls.

What is the difference between Terraform and a provider?

Terraform is the core tool that reads your configuration and plans changes. A provider is a plug-in that lets Terraform talk to a specific platform or service through its API.

Why does Terraform need a state file?

The state file records the infrastructure Terraform has created. Terraform compares it with your configuration to decide which resources to create, change or remove.

Can Terraform manage things other than cloud servers?

Yes. The documentation says Terraform can manage low-level parts such as compute, storage and networking, as well as higher-level items like DNS entries and SaaS features.

Sources

This page is based on the official documentation below. Read it for full detail.

Terraform — official documentation ↗

Terraform in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.