Q: terraform plan shows unexpected changes — what would you investigate?
Deep investigation runbook for terraform plan unexpected changes: distinguishing configuration drift, provider upgrade defaults, dynamic attributes, and preventing accidental destruction.
Terraform Plan Unexpected Changes: STOP Before Apply!
Watch Naveed break down how to investigate unexpected diffs in Terraform, decode forces replacement warnings, isolate cloud state drift with terraform plan -refresh-only, and prevent accidental resource destruction in 60 seconds.
- Rule #1: STOP! Never run terraform apply when unexpected destructive changes appear
- Decode plan action symbols: identify ~ update in-place vs -/+ forces replacement
- Isolate out-of-band cloud drift using terraform plan -refresh-only and AWS CloudTrail
- Safeguards: use moved blocks for refactoring and lifecycle { prevent_destroy = true }
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: When terraform plan unexpected changes appear — especially destructive replacements (-/+) or unexplained updates — my absolute first rule is: STOP. Do not run terraform apply. Investigate the diff systematically.
Analyze the Diff & Action Symbols
Read the exact plan output carefully:
~ update in-place: Non-destructive attribute change.-/+ replace (forces replacement): High danger — Terraform must DESTROY the existing resource and recreate a new one! Look for the# ... forces replacementcomment next to the specific attribute that triggered it (e.g. EC2 AMI change, VPC CIDR, or EBS volume type).- destroy: Resource is being deleted because it was removed from code or renamed.
Check for Out-of-Band State Drift
Did someone modify the cloud resource manually in the AWS Console or CLI?
- Run:
terraform plan -refresh-only. - This compares the Terraform state file directly against real-world cloud infrastructure without proposing changes to match the code.
- If
-refresh-onlyshows drift: Someone touched the resource out-of-band. - Check AWS CloudTrail: Filter event history by resource name to identify who modified the resource, when, and via which API call.
Provider Upgrades & Variable Drift
Inspect underlying dependencies:
- Provider Version Changes: Check
.terraform.lock.hcl. Did a provider update (e.g. AWS provider v5.0 → v5.2) change default attributes, deprecate tags, or modify schema defaults? - Variable & tfvars Mismatch: Was a different
.tfvarsfile passed? Did an engineer override variables viaTF_VAR_*environment variables? - Dynamic Inputs: Is code using dynamic functions like
timestamp()oruuid()that produce new diffs on every single run?
Resolve and Prevent Recurrence
How to resolve safely:
- If code was renamed: Use
moved { from = ... to = ... }blocks (Terraform 1.1+) instead of destroying and recreating! - If manual change was intentional: Update Terraform code to match reality, run
terraform apply -refresh-only. - If manual change was rogue: Run
terraform applyto overwrite manual changes and restore code enforcement. - Add
lifecycle { prevent_destroy = true }to critical databases and VPCs.
- Freeze: Do not apply. Inspect plan diff for '~' (in-place) vs '-/+' (destroy and recreate - forces replacement).
- Isolate drift: Run 'terraform plan -refresh-only' to identify out-of-band manual changes in AWS Console.
- Check CloudTrail: Identify who modified the resource outside Terraform and when.
- Check provider & variables: Check '.terraform.lock.hcl' for provider upgrades and verify correct .tfvars file.
- Use moved blocks: If refactoring, use 'moved' blocks to prevent destroy/recreate cycles.
- Safeguards: Add 'lifecycle { prevent_destroy = true }' on critical production resources.
Mastered this procedure? Subscribe to @naveedkumbhar01 on YouTube for daily 60-second SRE incident runbooks, Kubernetes triage shortcuts, and high-impact interview elevator pitches.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Executing Terraform applies without inspecting '# forces replacement' warnings in plan output when configuring terraform plan Shows Attributes that force resource replacement destroy and recreate critical infrastructure (databases, load balancers), inducing unannounced production outages.
- Omitting remote state locking mechanisms (DynamoDB or backend native locks) for shared state files when configuring terraform plan Shows Without state locking, concurrent CI/CD pipeline executions or team applies create race conditions that corrupt the state file.
- Structuring entire enterprise cloud footprints into a single monolithic Terraform root module when configuring terraform plan Shows Monolithic state files slow plan execution dramatically, increase blast radius during failed applies, and create severe pipeline locking contention.
- Failing to pin required_providers versions, causing breaking provider upgrades in CI pipelines when configuring terraform plan Shows Unpinned provider versions automatically download breaking major upgrades during CI runs, introducing unexpected schema validation failures.