Terraform

One language to describe cloud infrastructure on AWS, Azure, GCP, Cloudflare, Datadog — and dozens more. The default for Infrastructure as Code.

1. Write .tf files in Git 2. Plan diff vs state 3. Apply create / update / delete 4. Detect drift nightly plan state truth
Lifecycle Write → Plan → Apply → Drift — around the state file

A startup runs everything on AWS. They’ve been growing fast. They need an identical second region for a European customer. They estimate: "two weeks of clicking, plus a fortnight of bug fixes."

A new hire opens their laptop. She copies the existing Terraform code. She changes one variable: region = "eu-west-1". She runs terraform apply. Ninety minutes later, the second region exists, with the same VPC layout, the same security groups, the same databases, the same IAM roles. Tested, repeatable, in Git.

That’s Terraform. The cloud becomes a function of the code.

01What is Terraform?

Terraform is an open-source tool that takes a text description of infrastructure (written in HashiCorp Configuration Language, or HCL) and turns it into real resources by calling cloud APIs. It tracks what it has created in a "state file" and uses that state to compute the difference between desired and actual.

The simple version

Terraform reads a file that says what cloud resources you want, talks to the cloud and makes them exist. Run it again, it changes nothing. Change the file, run it again, it brings the cloud to match.

The technical version

Terraform is a declarative provisioning tool. Providers (plugins) translate HCL into cloud API calls. The state file is a JSON representation of every managed resource; the planner diffs desired HCL against actual state to decide what to create, update or delete.

The boring version

Terraform is the most installed cloud automation tool in the world. Almost every production cloud account you work on for the next decade will be Terraformed.

02Six block types — that’s the whole language

Terraform’s configuration language (HCL) has very few primitives. Master these six and you can read any Terraform codebase on Earth.

provider
Tells Terraform which cloud or service to talk to: aws, azurerm, google, cloudflare, datadog, github. Configured with credentials and region.
resource
A thing to create and manage: aws_s3_bucket.main, azurerm_kubernetes_cluster.aks. The verbs of Terraform.
data
A thing to look up, not manage: fetch the latest AMI ID, read an existing secret, find an AWS account number. Read-only.
variable
Inputs to a module: environment, region, instance_count. Typed, validated, optionally marked sensitive.
output
Values the module exposes to its caller or to humans — e.g. the public IP of an instance, the URL of a load balancer.
module
A folder of .tf files used as a reusable unit. Modules are how teams stop copying the same 200 lines into every project.

03State — the sacred file

Almost every Terraform problem people hit is actually a state problem. Understanding state is the difference between using Terraform and fearing it.

Idea 01

What state actually contains

A JSON file listing every resource Terraform created, with the cloud’s ID for each (e.g. i-0abc123), the configuration that produced it, and metadata. It is the source of truth about what you own.

Idea 02

Never store it in Git

State contains secrets (encrypted attributes are decrypted in state), resource IDs, and no merge story. Use a remote backend with locking — S3 + DynamoDB on AWS, blob storage on Azure, GCS on GCP.

Idea 03

One state per environment

Separate dev, stage and prod state files. A bug in dev should not be one bad apply away from breaking prod.

Idea 04

Split by lifecycle, not by team

Networking changes once a quarter. Application configs change daily. Keep them in separate states so a 40-minute network plan doesn’t block a 2-minute app deploy.

04A real module — a secure S3 bucket

Twenty lines of HCL that wrap "an encrypted, private S3 bucket" so every project gets it right the first time.

modules/s3-secure/main.tfHCL
variable "name"    { type = string }
variable "kms_key" { type = string }

resource "aws_s3_bucket" "this" {
  bucket = var.name
}

resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
  bucket = aws_s3_bucket.this.id
  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm     = "aws:kms"
      kms_master_key_id = var.kms_key
    }
  }
}

resource "aws_s3_bucket_public_access_block" "this" {
  bucket                  = aws_s3_bucket.this.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

output "arn" {
  value = aws_s3_bucket.this.arn
}

Now consumers just write module "logs" { source = "../modules/s3-secure"; name = "app-logs"; kms_key = aws_kms_key.app.id }. The security defaults are inherited; there is no opportunity to forget them.

05The commands you’ll type every day

Eight verbs and you can do almost anything. The state-surgery commands are for rare rescue operations — treat them with respect.

Inspect & format

terraform init        # download providers, configure backend
terraform fmt -recursive
terraform validate
terraform providers

Change & apply

terraform plan -out=tfplan
terraform apply tfplan
terraform destroy -target=<res>
terraform import <addr> <id>

State surgery (carefully)

terraform state list
terraform state show <addr>
terraform state mv  <src> <dst>
terraform state rm  <addr>

Workspaces (for experiments)

terraform workspace list
terraform workspace new feat-x
terraform workspace select dev

06Terraform vs OpenTofu

In 2023 HashiCorp relicensed Terraform under the BSL. The community forked it. Both projects are alive in 2026 and you should know what they are.

Terraform (HashiCorp)

  • Licence: Business Source Licence (BSL) since 2023.
  • Ecosystem: the original; provider registry; Terraform Cloud.
  • Some features are paid: Sentinel policy, run-tasks, larger plans.
  • Default for most existing teams.
vs

OpenTofu (Linux Foundation)

  • Licence: MPL-2.0, fully open source.
  • Drop-in: the tofu CLI is API-compatible with Terraform.
  • State encryption built into the open-source binary.
  • Growing fast — a real option for greenfield projects in 2026.

07Terraform in CI — plan in PR, apply on merge

Running Terraform from a laptop is fine for a side project. For anything real, the pipeline runs it. The discipline that makes that safe is the same in every team.

PR opened

A bot runs terraform init and terraform plan -out=tfplan. The plan output is posted as a PR comment.

Review

Humans read the plan. CODEOWNERS gate sensitive paths (network, IAM, prod).

Approve

Required approvers click. The plan file is preserved as a CI artifact.

Merge & apply

On merge to main, the pipeline runs terraform apply tfplan against the saved plan. No re-planning — what you reviewed is what you apply.

Audit & drift check

Plan, apply log and state version land in the audit store. A nightly plan runs and alerts if anyone clicked in the console.

08Habits that keep Terraform safe

Terraform problems are slow, expensive and hard to undo. These habits prevent the worst of them.

Anti-pattern
Editing state by hand. Even with terraform state mv. If you have to do it more than once, your modules are wrong — refactor them.
Anti-pattern
Unpinned provider versions. A minor provider release can change resource schemas overnight. Always pin required_providers versions with ~> ranges.
Anti-pattern
"Force-applies" that skip the plan. The plan is the review artifact. Skipping it is skipping the review. There’s no production worth this shortcut.
Anti-pattern
One giant root module. A 40-minute plan teaches developers to avoid Terraform. Split by lifecycle (networking / data / apps), keep plans under 5 minutes.
Healthy
Modules with sane defaults. Wrap risky resources (S3, IAM, security groups) in internal modules that bake in security defaults. Other teams can’t go wrong.

09Putting it together

Take-aways

1. Terraform turns text files into real cloud resources, tracks them in a state file, and brings reality to match the code on every apply.

2. The whole language is six block types: provider, resource, data, variable, output, module.

3. State is sacred. Remote backend, one per environment, never in Git, never edited by hand.

4. Plan in PR, apply on merge, audit everything, detect drift nightly. Pick Terraform or OpenTofu — both will work.

Next chapter: putting all of this on a specific cloud — starting with AWS DevOps.