Infrastructure as code · Scenario
terraform plan wants to destroy the production database
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
A pull request refactors our Terraform into modules. The plan in CI shows the production RDS database will be destroyed and created again. The PR already has two approvals, and merging applies automatically. What do you do?
Then
Follow-ups the interviewer asks next
- Why would moving code into a module make Terraform want to destroy the database?
- How do you fix it without touching the database?
- After that fix, the plan still says one attribute forces replacement. What could it be?
- What would have stopped an accidental destroy even with an approved plan?
- How should plans be reviewed so this gets noticed?
- If it had been applied, how would you recover?
The answer
What a strong answer covers
- Stop first: block the merge (request changes, or turn off auto-apply) and tell the approvers.
- Terraform tracks resources by address.
aws_db_instance.mainmoving tomodule.database.aws_db_instance.thislooks like "delete the old one, create a new one". - Fix: a
moved { from = aws_db_instance.main to = module.database.aws_db_instance.this }block (Terraform 1.1+), orterraform state mvin a controlled run. The plan must then show no change for it. - Forced replacement: some RDS attributes can't change in place (
storage_encrypted,kms_key_id,identifier,snapshot_identifier, among others). Here the new module defaultsstorage_encrypted = truewhile the real database is unencrypted. Set the module input to match reality and plan encryption as its own migration (snapshot, encrypted copy, restore, cut over). - Guards:
lifecycle { prevent_destroy = true }on stateful resources, RDSdeletion_protection = true, final snapshots (skip_final_snapshot = false), automated backups and point-in-time restore, an IAM deny on deleting databases for the CI role in production, and a policy check in CI (OPA/conftest or Sentinel) that fails any plan deleting or replacing stateful resources. - Review: a plan summary in the PR (add, change, destroy counts, replacements highlighted); production applies only the saved plan that was reviewed; destroys need a separate approval.
- Recovery: restore the latest automated backup or a point-in-time copy to a new instance, repoint the app, accept data loss back to the restore point, which is why the guards matter.
The red pen
Common traps
Avoid:"just apply, RDS keeps snapshots" without checking; -target tricks in production; deleting the resource from state (Terraform then creates a duplicate).
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.