The state file is the product
Terraform's power and its danger both come from state. It is the mapping between your code and real resources; lose it and Terraform will happily try to recreate a production network.
Store state remotely with locking, never in the repository.
One state per environment, per blast radius — not one state for everything.
Enable versioning on the state backend so a bad apply can be rolled back.
A readable module
variable "name" { type = string }
variable "cidr" { type = string }
variable "azs" { type = list(string) }
resource "aws_vpc" "this" {
cidr_block = var.cidr
enable_dns_hostnames = true
tags = { Name = var.name, ManagedBy = "terraform" }
}
resource "aws_subnet" "private" {
for_each = { for i, az in var.azs : az => i }
vpc_id = aws_vpc.this.id
availability_zone = each.key
cidr_block = cidrsubnet(var.cidr, 4, each.value)
tags = { Name = "${var.name}-private-${each.key}" }
}
output "vpc_id" { value = aws_vpc.this.id }Plan review is the control
A plan is a change request. Treat destroy and replace lines as blocking review comments, and make the CI job post the plan on the pull request so the reviewer sees the same output the applier will.
terraform init -backend-config=env/prod.hcl
terraform plan -out=tfplan -lock-timeout=120s
terraform show -no-color tfplan > plan.txt
terraform apply tfplanDrift and imports
Manual changes happen. Run a scheduled plan in read-only mode and alert on non-empty diffs; import existing resources rather than recreating them.
Conventions worth enforcing
Pin provider versions and the Terraform version.
Format and validate in CI: terraform fmt -check, validate, and a policy scan.
Tag everything with owner, environment and cost centre.