Interview Prep/Terraform/Import Refactoring Drift and Lifecycle

How do you protect a production database from an accidental `terraform destroy`?

BeginnerScenarioImport Refactoring Drift and Lifecycle
Answer

Situation read → A plan that deletes the DB must fail loudly.

What I'd do →

  1. Add prevent_destroy = true.
  2. Also set deletion_protection = true on the resource itself.
  3. Enable final snapshots (skip_final_snapshot = false).
resource "aws_db_instance" "main" {
  deletion_protection       = true
  skip_final_snapshot       = false
  final_snapshot_identifier = "main-final"
  lifecycle { prevent_destroy = true }
}

Why → prevent_destroy stops Terraform; deletion_protection stops the API even if someone bypasses Terraform.

Interview tip → prevent_destroy only works while the block is in code; deleting the block deletes the protection.

All Terraform questions Roadmap: Terraform in production

More on Import Refactoring Drift and Lifecycle