Scenario
You are operating a production Terraform estate. The next
plan is scheduled for the maintenance window. You run terraform plan and see:
Plan: 0 to add, 0 to change, 18 to destroy.
The plan proposes to destroy 18 resources. The expected plan was:
Plan: 0 to add, 0 to change, 0 to destroy.
You did not modify the configuration.
Your task
Investigate the unexpected plan. Do not apply.
Evidence to discover
# Check the recent commits
git log --oneline -20
# Check the diff
git diff HEAD~1 main.tf
# Check the resources in the plan
terraform plan -out=production.tfplan
terraform show -json production.tfplan | jq '.resource_changes[] | select(.change.actions | index("delete"))'
# Check the states view
terraform state list
Questions to answer
- Which resources are slated for destruction?
- Why did the configuration change?
- Are the resources expected to be destroyed?
- What is the correct remediation?
Recovery procedure
(Do not reveal this until the student has reasoned through the problem.)
- Do not apply the plan. The plan is suspicious.
- Identify the resources. The plan output shows the destroyed resources.
- Identify the cause. The configuration has been modified to remove the resources. A teammates PR removed the resource blocks.
- Decide the remediation.
- If the destruction is intentional, document the change and apply.
- If the destruction is accidental, revert the configuration. Re-plan. The plan should be empty.
- Prevent the recurrence. Add
lifecycle.prevent_destroyto critical resources.
Remediation
- The configuration was modified to remove the resources.
- The removed resources were critical.
- The team agreed to restore the configuration.
- The plan is empty after the restore.
- The lifecycle is updated to prevent future destruction.
Prevention
- Review all configuration changes in PR review.
- Use
lifecycle.prevent_destroyon critical resources. - Add tags to critical resources for clarity.
- Require a second engineer to approve PRs that modify critical resources.
What you learned
- An unexpected plan is evidence, not inconvenience.
- The plan is the unit of review. The review is the safety net.
- A destroy in the plan is data loss. Verify before applying.
lifecycle.prevent_destroyis a circuit breaker for critical resources.- PR review is the cheapest production control.