How do you make sure nobody can accidentally destroy the production database with Terraform?

BeginnerScenarioData and databases
Answer

Situation read → One wrong terraform destroy, a renamed resource, or a forced replacement can delete it. Add layers so no single mistake can.

What I'd do →

  1. Turn on the provider-level protection:
resource "aws_db_instance" "prod" {
  deletion_protection       = true
  skip_final_snapshot       = false
  final_snapshot_identifier = "prod-db-final"
  lifecycle {
    prevent_destroy = true
  }
}
  1. Keep the database in its own Terraform state, separate from app resources that change often.
  2. Fail the pipeline if a plan contains a delete or replace of protected resources:
terraform show -json tfplan | jq -e '[.resource_changes[] | select(.change.actions | index("delete"))] | length == 0'
  1. Only CI can apply to prod; humans can't.
  2. Backups in a separate account, so even a full deletion is recoverable.

Why → prevent_destroy stops Terraform, deletion_protection stops the AWS API, separate backups survive both failing.

Interview tip → Note that prevent_destroy doesn't help if someone deletes the resource block from code; deletion_protection still does.

All Real-World Interview questions Roadmap: Interview readiness

More on Data and databases