do-terraform
When to use
You need to author infrastructure-as-code, refactor sprawling configs into modules, set up remote state, investigate drift, or review a terraform plan before apply.
Not for: Kubernetes workload manifests (use do-kubernetes), CI that runs the plan/apply (use do-ci-pipeline), or the production cutover procedure (use do-release-checklist).
Method
- Structure code: reusable modules with explicit
variables.tf/outputs.tf, thin root configs per environment, and pinned provider and module versions.
- Configure remote state with locking (S3+DynamoDB, GCS, or Terraform Cloud). Decision: isolate state per environment to limit blast radius; never commit state or secrets.
- Keep resources declarative and idempotent. Decision: prefer
for_each over count so removing one element does not re-index and recreate the others; use data sources instead of hardcoded IDs.
- Detect drift with
terraform plan -detailed-exitcode on a schedule. Decision: reconcile by import or by correcting config — never by manual console edits, which re-introduce drift.
- Review every plan: read
+/~/- symbols, flag any destroy-then-create on stateful resources, check for unintended replacements, and confirm secret values are not printed.
- Gate apply behind CI with a saved plan artifact so the reviewed plan is the applied plan.
Example
# for_each gives stable addressing: removing "web" leaves "api" untouched
variable "buckets" {
type = set(string)
default = ["web", "api"]
}
resource "aws_s3_bucket" "this" {
for_each = var.buckets
bucket = "acme-${terraform.workspace}-${each.key}"
}
terraform {
required_version = ">= 1.6"
backend "s3" {
bucket = "acme-tfstate"
key = "prod/app.tfstate" # isolated per env
dynamodb_table = "tf-locks" # state locking
encrypt = true
}
}
Pitfalls
count on a list where you delete a middle element — Terraform re-indexes and destroys/recreates every resource after it.
- Local or committed state, so two engineers apply concurrently and corrupt it (no lock).
- Fixing drift by hand in the cloud console; the next plan wants to revert your fix, or silently overwrites it.
- Skimming a plan and missing a
-/+ replacement on a database or volume — that is data loss, not an update.
Output format
Layout: module/ and root file tree, key .tf snippets.
Backend: remote state + locking config.
Plan review:
- adds: <n>
- changes: <n>
- destroys: <n>
- HIGH RISK: <resource> replaced (destroy-then-create on stateful) — call out explicitly
1---2name: do-terraform3version: 1.0.04description: Write or review Terraform with clean versioned modules, locked remote state, drift detection, and disciplined plan review before apply.5author: matrixx00706tags: [terraform, iac, devops, cloud, review, state]7capabilities: []8metadata:9 source: https://skillmd.com/skills/matrixx0070/do-terraform10---1112# do-terraform1314## When to use1516You need to author infrastructure-as-code, refactor sprawling configs into modules, set up remote state, investigate drift, or review a `terraform plan` before apply.1718**Not for:** Kubernetes workload manifests (use do-kubernetes), CI that runs the plan/apply (use do-ci-pipeline), or the production cutover procedure (use do-release-checklist).1920## Method21221. Structure code: reusable modules with explicit `variables.tf`/`outputs.tf`, thin root configs per environment, and pinned provider and module versions.232. Configure remote state with locking (S3+DynamoDB, GCS, or Terraform Cloud). Decision: isolate state per environment to limit blast radius; never commit state or secrets.243. Keep resources declarative and idempotent. Decision: prefer `for_each` over `count` so removing one element does not re-index and recreate the others; use `data` sources instead of hardcoded IDs.254. Detect drift with `terraform plan -detailed-exitcode` on a schedule. Decision: reconcile by `import` or by correcting config — never by manual console edits, which re-introduce drift.265. Review every plan: read `+`/`~`/`-` symbols, flag any destroy-then-create on stateful resources, check for unintended replacements, and confirm secret values are not printed.276. Gate apply behind CI with a saved plan artifact so the reviewed plan is the applied plan.2829## Example3031```hcl32# for_each gives stable addressing: removing "web" leaves "api" untouched33variable "buckets" {34 type = set(string)35 default = ["web", "api"]36}3738resource "aws_s3_bucket" "this" {39 for_each = var.buckets40 bucket = "acme-${terraform.workspace}-${each.key}"41}4243terraform {44 required_version = ">= 1.6"45 backend "s3" {46 bucket = "acme-tfstate"47 key = "prod/app.tfstate" # isolated per env48 dynamodb_table = "tf-locks" # state locking49 encrypt = true50 }51}52```5354## Pitfalls5556- `count` on a list where you delete a middle element — Terraform re-indexes and destroys/recreates every resource after it.57- Local or committed state, so two engineers apply concurrently and corrupt it (no lock).58- Fixing drift by hand in the cloud console; the next plan wants to revert your fix, or silently overwrites it.59- Skimming a plan and missing a `-/+` replacement on a database or volume — that is data loss, not an update.6061## Output format6263```64Layout: module/ and root file tree, key .tf snippets.65Backend: remote state + locking config.6667Plan review:68- adds: <n>69- changes: <n>70- destroys: <n>71- HIGH RISK: <resource> replaced (destroy-then-create on stateful) — call out explicitly72```