Skip to content

Terraform

Terraform is an open-source infrastructure-as-code tool by HashiCorp that provisions cloud resources from declarative HCL files, tracking them in a state file.

Definition

What it is

Terraform is an infrastructure-as-code (IaC) tool: engineers describe the desired end state of cloud resources in configuration files, and Terraform makes the real world match. The configuration is written in HCL (HashiCorp Configuration Language), a declarative syntax designed for readability. The tool compares that description against a state file that records what it believes exists, computes a plan of the changes needed to reconcile the two, and applies the plan by calling the APIs of the underlying cloud providers — AWS, Azure, Google Cloud, Kubernetes, Cloudflare, dozens of SaaS platforms.

The critical distinction from configuration management tools (Ansible, Chef, Puppet) is that Terraform is declarative and infrastructure-focused. Ansible describes steps to run on a host; Terraform describes resources that should exist in a cloud. When you run Terraform, it does not execute a script: it figures out which resource API calls are needed to close the gap between declared and actual state, and executes them. That model is what makes Terraform idempotent: applying the same configuration twice produces no change the second time.

Why it matters

Before Terraform, provisioning cloud infrastructure usually meant either clicking through a provider console (slow, undocumented, not reproducible) or writing ad-hoc shell scripts that called each provider's API directly (fragile, non-idempotent, provider-specific). Both approaches made drift inevitable: the environment no longer matched what anyone remembered as the plan. Terraform solved three problems at once. It made infrastructure declarative, so the description is the source of truth. It made it multi-provider, so a single workflow could manage AWS, DNS, and Kubernetes together. And it made it versionable, so every change goes through a review, is attributable, and can be reverted.

Terraform also changed how infrastructure teams think about environments. The same module can provision staging and production, differing only in input variables. The same plan-apply cycle works for the first deployment and for the hundredth change. The same review process catches destructive changes before they reach production. That reproducibility is what makes Terraform trustworthy at scale and what explains its adoption by almost every organisation that manages more than a handful of cloud resources.

How it works

The workflow has four stages. terraform init downloads the providers referenced by the configuration (each provider is a plugin that knows how to talk to a specific cloud API). terraform plan reads the configuration, reads the state, refreshes the state against the live APIs, and computes a diff. The diff is printed as a plan: what will be created, what will be updated, what will be destroyed. terraform apply executes that plan, respecting the dependency graph so that, for example, a subnet is created before the instance that uses it. terraform destroy reverses the operation, deleting everything the configuration manages.

graph LR
  C[Config .tf files] --> I[terraform init]
  I --> P[terraform plan]
  S[State file] --> P
  P -->|diff| R{Review}
  R -->|approve| A[terraform apply]
  A -->|API calls| Cloud[Cloud providers]
  A --> S

The dependency graph is what allows Terraform to apply changes in parallel when they are independent. If the configuration defines a VPC, ten subnets inside it, and a load balancer that references the subnets, Terraform will create the VPC first, then the subnets in parallel, then the load balancer. That parallelism is bounded by the graph, and it is what makes a Terraform apply fast even when the configuration describes hundreds of resources.

Structuring Terraform for production

  1. Split configuration into modules and environments

    Create reusable modules for networks, compute, data, IAM. Create one environment per folder (staging, production) that instantiates the modules with its own variables.

  2. Use a remote backend with locking

    Configure the S3 + DynamoDB backend (or GCS, or Terraform Cloud) before writing any resource. Locking prevents two engineers from applying simultaneously.

  3. Review the plan before every apply

    Require a reviewer for any plan that destroys or replaces a resource. The plan is the review artifact; the configuration is not enough.

  4. Pin provider versions

    Lock provider versions in .terraform.lock.hcl and commit it. Provider upgrades can silently change resource behaviour; pin and upgrade deliberately.

Modern Terraform adds a state-locking mechanism (through the backend) that prevents concurrent applies from corrupting the state, and a terraform import command that adopts existing resources into management without recreating them. The terraform plan -out=plan.tfplan pattern saves the plan to a file and passes it to apply, guaranteeing that what was reviewed is what is applied — a critical safety property for automated pipelines.

Common failures

A second failure is state file corruption. If two engineers apply simultaneously without locking, or if the state file is edited by hand, or if the backend is misconfigured and the state is overwritten, Terraform can lose track of every resource it manages. Recovery requires importing resources one by one, which is slow and error-prone. The preventive fix is a remote backend with locking and versioning from day one.

A third failure is drift. Someone logs into the console, changes a security group rule, and forgets. The next Terraform apply silently reverts the change — sometimes producing an outage. Scheduled drift detection (terraform plan on a cron, or a dedicated tool) catches the problem before it becomes an incident. In practice, any environment that Terraform manages should have a daily plan that alerts on unexpected diffs.

Origin

Terraform emerged in 2014 from HashiCorp, the company Mitchell Hashimoto co-founded after building Vagrant. The insight was that cloud infrastructure should be managed the same way application code is: declarative, versioned, tested, and applied by an automated tool. Earlier tools (Terraform's predecessor, Terraform's designer's own Vagrant, plus Chef and Puppet) had tackled configuration, but none had treated multi-provider cloud resources as first-class citizens. Terraform's provider model made it possible to manage AWS, Azure, GCP, and a hundred other services with the same workflow.

In August 2023, HashiCorp changed Terraform's license from MPL 2.0 to BUSL 1.1, restricting commercial use. In response, a group of vendors and users forked the project as OpenTofu, which was adopted by the Linux Foundation and reached v1.0 in January 2024. Most of Terraform's providers and workflows are compatible with OpenTofu, though some newer features remain Terraform-only.

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. It can manage existing and popular service providers as well as custom in-house solutions.

— HashiCorp, *Terraform Documentation*, 2024

Evidence

Timeline

  1. 2014 — Terraform 0.1 released by HashiCorp; Mitchell Hashimoto.

  2. 2016 — Kief Morris publishes Infrastructure as Code (O'Reilly), formalizing the discipline.

  3. 2019 — Terraform 0.12 introduces HCL2, the modern configuration syntax.

  4. 2020 — HashiCorp launches the Terraform Registry with thousands of modules.

  5. 2023 — HashiCorp changes Terraform's license to BUSL 1.1.

  6. 2024 — OpenTofu 1.0 forks Terraform under the Linux Foundation.

FAQ

Terraform or CloudFormation?

Terraform is multi-cloud; CloudFormation is AWS-only. Terraform's workflow (plan/apply) is more uniform across clouds; CloudFormation integrates more deeply with AWS-specific features. For teams that only use AWS and want maximum native integration, CloudFormation is a reasonable choice. For teams that manage resources on multiple providers, Terraform (or OpenTofu) is the standard.

Terraform or Ansible?

They solve different problems. Terraform is declarative provisioning: "create this VPC, this database, this load balancer". Ansible is procedural configuration: "install these packages, edit these files, restart these services". Most mature teams use both — Terraform for cloud infrastructure, Ansible for host configuration. Pulumi and CloudFormation occupy the Terraform role.

What is the state file and why does it matter?

The state file maps resources declared in configuration to real resource IDs in the cloud. Without it, Terraform cannot know what it manages, so it cannot plan safely. Losing the state file means every resource has to be manually re-imported. The standard practice is a remote backend with locking and versioning (S3 + DynamoDB, GCS, Terraform Cloud).

What happens if HashiCorp changes Terraform's license?

In 2023, HashiCorp changed the license to BUSL 1.1, restricting commercial use. The OpenTofu fork was created in response and adopted by the Linux Foundation. For most teams, OpenTofu is compatible with Terraform's syntax and providers; for teams that rely on newer Terraform features or HashiCorp's Terraform Cloud, Terraform remains the choice. The license change created a strategic decision point that every Terraform adopter should consciously make.

How do I stop Terraform from destroying something by accident?

Three mechanisms: (1) never apply without reviewing the plan, (2) use prevent_destroy = true on resources that must never be deleted, (3) split configuration into layers so that a change to one layer cannot cascade to another. In automated pipelines, require a human approval for any plan that shows a destroy or replace operation.

Connections

TerraformInfrastrucOpenTofuAnsibleKubernetesDockerGitHub ActIdempotentGitVault

● broader ● narrower ● related

Used in the garden

No mentions yet.

Sources

Etymology

Origin: compound

  • terra (noun): Latin for "earth" or "land"; the underlying infrastructure
  • form (verb): from Latin forma, "shape" or "mold"; to shape into a desired state

Semantic network

TerraformInfrastrucOpenTofuAnsibleKubernetesDockerGitHub ActIdempotentGitVault

● broader ● narrower ● related

Sources