You’ve inherited a cloud account where every resource was clicked into existence in the console. Nobody knows which security group is unused, the staging VPC and the production VPC look almost identical except where they don’t, and you cannot reliably rebuild any of it. Terraform is the way out — declarative HCL, a state file that records what you own, and a plan/apply loop that shows exactly what is going to change before it changes. This article gets a first-time operator from zero to a working terraform apply against a real cloud account, with the layout that does not paint you into a corner.
How to verify
In any Terraform directory, you can see what is currently understood and what state lives where:
terraform version
terraform init -backend=false
terraform fmt -check -recursive
terraform validate
terraform state list
terraform providers
terraform fmt -check returns non-zero on unformatted files — wire it into the pre-commit hook. terraform state list shows every resource Terraform thinks it owns; the count should match the count in the cloud console for the same scope.
What’s happening
Terraform reads HCL files in the current directory, talks to the cloud provider’s API through a provider plugin, and reconciles the declared resources against a state file. The state file is the source of truth for “what Terraform owns” — without it, Terraform cannot map cloud resources back to HCL blocks. Every apply records the result back to state.
The mental model is three-sided: the HCL is what you want, the cloud API is what exists, the state is what Terraform last saw. terraform plan diffs all three and prints what would change. terraform apply executes that diff. If two operators run apply at the same time against the same state, the second one fails (or worse, corrupts state) — which is why state belongs in a remote backend with locking. See /en/knowledge-base/terraform-state-backends/.
The procedure
-
Install Terraform via the official tarball, not Homebrew. Pinning to a specific minor version matters; HCL is mostly forward-compatible but providers tighten constraints occasionally.
TF_VERSION=1.9.5 curl -fsSL -o /tmp/tf.zip \ "https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_amd64.zip" sudo unzip -o /tmp/tf.zip -d /usr/local/bin/ terraform version -
Lay out a minimal project. Five files in a single directory is enough for the first iteration; the structure stays the same as it grows.
. ├── versions.tf # required_version and required_providers ├── providers.tf # provider blocks ├── main.tf # resources ├── variables.tf # input variables └── outputs.tf # outputs -
Pin Terraform and the providers in
versions.tf. This is the single highest-impact discipline — without it, a provider major upgrade two months later breaks your apply.terraform { required_version = ">= 1.9.0, < 2.0.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.60" } } } -
Configure the provider with credentials from outside the file. Never put access keys in HCL. The AWS provider reads
AWS_PROFILE,AWS_ACCESS_KEY_ID, or instance role automatically; for OIDC from CI, use the cloud-specificconfigure-aws-credentialsaction and let the provider pick up the env vars.provider "aws" { region = var.aws_region default_tags { tags = { ManagedBy = "terraform" Project = var.project } } } -
Write your first resource. Start tiny — a VPC, an S3 bucket, or a security group. Avoid the temptation to write the whole stack on day one.
resource "aws_s3_bucket" "logs" { bucket = "${var.project}-logs-${var.environment}" } resource "aws_s3_bucket_versioning" "logs" { bucket = aws_s3_bucket.logs.id versioning_configuration { status = "Enabled" } } resource "aws_s3_bucket_public_access_block" "logs" { bucket = aws_s3_bucket.logs.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } -
Plan, read the diff, then apply. The
-outflag saves the plan so apply runs exactly what you reviewed — no last-second drift.terraform init terraform plan -out=tfplan terraform show tfplan | less terraform apply tfplan -
Move shared code into a module. Once two environments use the same resource set, extract it. A module is a directory with its own
main.tf/variables.tf/outputs.tfand is called from the root.module "logs_bucket" { source = "./modules/logs-bucket" project = var.project environment = "production" }
Common pitfalls
- A
terraform destroyon the wrong working directory has caused production outages — name the directory after the environment and put the environment name in the shell prompt. terraform applywithout-outplans implicitly and can drift from what you reviewed if another change lands between plan and apply — always use a saved plan in CI.countandfor_eachlook similar butcountkeys resources by index, so removing the middle item re-creates everything after it; usefor_eachfor any list where order is not the identity.- The
local-execprovisioner is an escape hatch; every use of it is a sign the provider does not cover a use case and the workaround needs review. - Provider deprecation warnings in plan output are not noise — they become breaking changes on the next major upgrade.
In the engagements we run, Terraform is the source of truth for everything the cloud API supports — modules per service, state in S3 with DynamoDB locking, OIDC from CI to the cloud, and a terraform plan posted as a PR comment for every change. The cloud console is read-only for engineers; writes happen through code. See /en/solutions/aws-management/.