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 zipTerraform 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
pgbackend). Never committerraform.tfstateor.terraform/to Git; add them to.gitignore. - State can contain secrets. Treat it as sensitive.
- Before
terraform state mv,state rmorimport: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 {}andimport {}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_versionandrequired_providerswith~>constraints; commit.terraform.lock.hcl. - Inputs in
variables.tfwith types and descriptions; outputs inoutputs.tf; mark secretssensitive = true. - Use
localsfor repeated expressions,for_eachovercountwhen 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
LICENSESKILL.mdSOURCE.md
Related terms
- apply
- attribute
- block
- count
- data source
- depends_on
- destroy
- drift
- expression
- fmt
- for expression
- for_each
- heredoc
- IaC (Infrastructure as Code)
- import
- init
- interpolation
- lifecycle
- locals
- module
- output
- plan
- provider
- provider alias
- refresh
- remote backend
- resource
- state file
- state lock
- state mv / rm
- taint
- ternary
- terraform block
- terraform.tfstate
- validate
- variable
- workspace
Installing an agent skill in Claude Code, Codex, Cursor and Copilot