Skip to the page

Infrastructure as code · Scenario

terraform plan wants to destroy the production database

The 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

  1. Why would moving code into a module make Terraform want to destroy the database?
  2. How do you fix it without touching the database?
  3. After that fix, the plan still says one attribute forces replacement. What could it be?
  4. What would have stopped an accidental destroy even with an approved plan?
  5. How should plans be reviewed so this gets noticed?
  6. 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.main moving to module.database.aws_db_instance.this looks 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+), or terraform state mv in 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 defaults storage_encrypted = true while 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, RDS deletion_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 voice

Practise 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.

Start your free interview