DELAPARRA
Index
Color
NL

Terraform safe workflow

Use when running or changing Terraform (or OpenTofu) - init, fmt, validate, plan, apply, destroy, import, state mv/rm, refresh, workspaces, remote backends, state locking, drift, taint/-replace. Enforces a plan-review-apply workflow and protects state.

Licence MIT

Source delaparra-original

Download zip

Terraform safe workflow

Terraform changes real infrastructure. Always work in the order: format → validate → plan → review → apply, and never touch state without a backup.

Standard workflow

terraform init              # providers, modules, backend
terraform fmt -recursive    # format
terraform validate          # syntax and references
terraform plan -out=tfplan  # save the exact plan
terraform show tfplan       # review
terraform apply tfplan      # apply exactly what was reviewed

When reviewing a plan, summarise for the user: how many resources are created (+), changed (~), replaced (-/+) and destroyed (-). Any replace or destroy of a database, volume, bucket or DNS record must be called out explicitly and confirmed by the user.

State rules

  • Use a remote backend with locking (e.g. S3 with locking, GCS, Terraform Cloud/HCP, or pg backend). Never commit terraform.tfstate or .terraform/ to Git; add them to .gitignore.
  • State can contain secrets. Treat it as sensitive.
  • Before terraform state mv, state rm or import: terraform state pull > backup-$(date +%Y%m%d%H%M).tfstate.
  • If a lock is stuck, find out who holds it before terraform force-unlock <ID>; ask the user.
  • Prefer moved {} and import {} blocks in code (Terraform 1.5+) over manual state commands, so changes are reviewable.

Drift

terraform plan -refresh-only shows changes made outside Terraform. Decide with the user: adopt the change into code, or let Terraform revert it. (terraform refresh is deprecated in favour of apply -refresh-only.)

Replacing a resource

Use terraform apply -replace=ADDRESS instead of terraform taint (deprecated). Plan first.

Code conventions

  • Pin versions: required_version and required_providers with ~> constraints; commit .terraform.lock.hcl.
  • Inputs in variables.tf with types and descriptions; outputs in outputs.tf; mark secrets sensitive = true.
  • Use locals for repeated expressions, for_each over count when items have stable keys.
  • lifecycle { prevent_destroy = true } on databases and storage that must never be deleted.
  • Separate environments by directory or workspace with separate state; never point dev at prod state.

Never without explicit user confirmation

terraform destroy, apply -auto-approve, state rm, force-unlock, or any plan that destroys data-bearing resources.

Files

  • LICENSE
  • SKILL.md
  • SOURCE.md

Related terms

Installing an agent skill in Claude Code, Codex, Cursor and Copilot