Maintain infrastructure with Terraform
Day-2 operations
Maintaining infrastructure means handling drift, upgrades, refactors and partial failures without destroying data.
- Drift - real infrastructure changed outside Terraform.
terraform plansurfaces it (refresh runs as part of plan);terraform apply -refresh-onlyupdates state without changing real resources;-refresh=falseskips it entirely (rarely what you want). - Forced replacement -
terraform apply -replace=aws_instance.webis the current mechanism;terraform taintis deprecated in its favor.terraform untaintreverses a taint. - Refactoring -
moved { from = ... to = ... }blocks orterraform state mvchange addresses without recreation.import { to = ..., id = ... }blocks (Terraform 1.5+) adopt existing objects declaratively;-generate-config-out=file.tfdrafts matching configuration for you to review. - Upgrades - raise
required_versionor provider constraints, runterraform init -upgrade, read the changelog, then read the plan closely: provider majors often force replacements. - Diagnostics -
TF_LOG=DEBUG(or TRACE) andTF_LOG_PATHfor logs. - Pipeline discipline -
fmt -check,validate,plan -out, human approval, then apply the saved plan. Never apply from a laptop against production state.
Traps
importalone does not write or fix configuration, and imported attributes are not validated against the block.-replacecauses real downtime or data loss if the resource is stateful.- A stale lock after a crashed run needs
force-unlock, not a manually deleted lock row.
Rule of thumb
Before approving any apply, scan the plan summary line for 0 to add, 0 to change, 0 to destroy versus replacement and destroy counts.