Interpreting Terraform Plan Output and Preventing Accidental Resource Deletion
A guide to interpreting diff symbols in terraform plan output, distinguishing in-place updates from destructive replacements, and implementing a safe apply workflow using saved plan files.
Curated by Vibe DevOps · approx. 507 words
Key Takeaways
- The terraform plan command compares your configuration code against live infrastructure without modifying cloud resources.
- Diff symbols indicate risk levels ranging from resource creation and in-place updates to destructive recreations.
- The 'forces replacement' annotation warns of impending resource destruction and recreation, which carries a high risk of data loss.
- Saving the execution plan to a file before applying prevents unintended changes.
From Original Source
Understanding Diff Symbols and the Plan Summary
The plan output displays diff symbols for each resource. A plus (+) indicates creation, while a tilde (~) denotes an in-place update, such as modifying tags. The most critical symbol is -/+, which signifies destroy and recreate when a configuration change mandates replacement (such as updating an OS image or AMI).
Engineers should always inspect the final summary line to check the count of resources to be added, changed, or destroyed. If the destroy count is greater than zero or unexpected changes appear, halt execution immediately to investigate.
- Exercise extreme caution with destroy-and-recreate actions in production environments.
- Thoroughly inspect attributes flagged with 'forces replacement' to prevent unintentional data loss.
From Original Source
Handling Out-of-Band Drift and Establishing a Safe Apply Workflow
When plan output shows diffs despite no code changes, the cause is typically out-of-band manual intervention in the cloud console or API. Proceeding with apply will overwrite these manual modifications with the state defined in code.
The safest approach is to output the plan to an execution file before applying. While resource targeting (-target) can be used for incident mitigation, it is not a substitute for a comprehensive plan review.
- Save the plan to a file to ensure that only reviewed and approved changes are applied.
- Identify the root cause of out-of-band changes before deciding to overwrite them.
Vibe DevOps Editorial Commentary
Recommended Infrastructure Change Approval Workflow
Vibe DevOps Perspective: In infrastructure automation pipelines, teams should store plan files as intermediate CI/CD artifacts. Requiring explicit manual approval whenever resource destruction or replacement is detected provides a reliable safeguard against service disruptions.
Commands & Configuration Snippets
Verbatim extract for quick lookup. Verify against your infrastructure before running.
terraform plan -out=tfplanSave the change plan results to a file for safe review.
terraform apply tfplanApply exactly the changes saved in the plan file.
terraform plan -target=aws_instance.webCheck changes specifically for the designated server resource.
⚠️ Operational Caveats & Limitations
- Resource targeting (-target) should be reserved for emergency situations and is not a substitute for full plan validation.
- Computed values (known after apply) can only be resolved once the resource provisioning operation completes.