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 →
- 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
}
}
- Keep the database in its own Terraform state, separate from app resources that change often.
- 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'
- Only CI can apply to prod; humans can't.
- 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.
More on Data and databases
A release needs to rename a column that the running app reads and writes. How do you do it with zero downtime?IntermediateHow do you prove your database backups actually work?IntermediateDesign backups for an RDS PostgreSQL database with RPO of 5 minutes and RTO of 1 hour, including a region failure.AdvancedYour migrations run in an init container and two pods ran them at the same time during a deploy, leaving the schema half-applied. How do you fix the approach?IntermediateUpgrade RDS PostgreSQL from 13 to 17 with minimal downtime. How?AdvancedYou need to add an index and a column to a 500-million-row table that's written to constantly. How do you avoid locking it?Advanced