Brushing up my Terraform after a failed apply
- Posted
- Tags
- terraform, iac, aws
A wrong AWS profile broke my apply, so I brushed up my Terraform: the core workflow, state and locking, modules, and the gotchas worth remembering.
My terraform apply failed on a side project because I was using the wrong AWS profile. The fix was small: set AWS_PROFILE (or profile in the provider block) and run aws sts get-caller-identity before applying to confirm which account I am actually about to touch. The failure also made me realize my Terraform was getting rusty, so I took some time to brush up on the fundamentals and wrote down what I reviewed.
The question I had
How much of Terraform do I still remember, and what do I need to refresh?
What I reviewed
The elevator pitch
Terraform is an Infrastructure as Code tool for provisioning, changing, and versioning infrastructure safely. It is declarative: I describe the desired state, and Terraform works out the dependencies and the order of execution. It is cloud-agnostic because providers (plugins) translate HCL into API calls for AWS, Azure, GCP, and even the local machine.
terraform {
required_version = ">= 1.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.aws_region
}
The core workflow
terraform init: set up the working directory, download providers, configure the backend.terraform fmt: rewrite the files into the canonical format.terraform validate: check structure and syntax.terraform plan: compare the state against the code and preview changes without touching anything.terraform apply: carry out the plan.terraform destroy: tear down everything the state file tracks.
Variables, outputs, and tfvars
A variable is the blank form, terraform.tfvars is the filled-in form, and an output exposes a value to the CLI or other configurations.
variable "instance_type" {
type = string
default = "t2.micro"
}
output "server_public_ip" {
value = aws_instance.web_server.public_ip
description = "The public IP of the EC2 instance"
}
Real values go in terraform.tfvars, which should never be committed. Add it to .gitignore.
State and locking
The state file (terraform.tfstate) is the source of truth that maps my code to real resources. In production, never edit it by hand and keep it in a remote backend.
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "project/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-lock-table"
encrypt = true
}
}
The dynamodb_table line turns on state locking. When a state-changing command starts, Terraform writes a row with a unique LockID, and deletes it when the run ends. That prevents two people (or two pipelines) from writing state at the same time. The table is empty when idle, so with pay-per-request pricing it costs close to nothing.
One update: newer Terraform (1.10 and later) can lock with S3 alone using use_lockfile = true, and the DynamoDB option is on its way out. For a new setup I would use that instead.
Modules
A module is a self-contained group of related resources. The root module is the directory where I run commands, and a child module is a bundle the root calls. Modules keep code DRY (Don't Repeat Yourself), limit the blast radius, and help enforce standards.
module "vpc_network" {
source = "./modules/aws-vpc"
vpc_cidr = "10.0.0.0/16"
env_prefix = "production"
}
Gotchas worth remembering
Manual console changes cause drift. Terraform detects it on the next plan or apply, compares the live resource to the code, and plans to bring it back in line with the HCL.
Prefer for_each over count. count tracks resources by index. Remove an item from the middle of the list and everything after it shifts, so Terraform wants to destroy and recreate resources that did not change. for_each uses stable string keys, so each resource lives or dies on its own.
Secrets still end up in state. Mark variables sensitive = true to hide them in CLI output, and prefer IAM instance profiles, environment variables, or an external store like Vault over hardcoded strings. The catch: the state file still stores sensitive values in plain text, so lock down the bucket with strict IAM and server-side encryption.
A crashed pipeline can leave the state locked. First confirm that no run is actually still going. Then release the lock with terraform force-unlock <LOCK_ID>, using the ID from the error message. Forcing an unlock while someone else's apply is running is how state gets corrupted.
Takeaway
A confusing failure is a good excuse to revisit the fundamentals. Check which account and profile I am pointed at before every apply, keep state remote and locked, and reach for for_each, modules, and lifecycle rules before reaching for provisioners or manual state edits.