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 →
- Add
prevent_destroy = true. - Also set
deletion_protection = trueon the resource itself. - 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.
More on Import Refactoring Drift and Lifecycle
An S3 bucket was created by hand in the console and now must be managed by Terraform. How do you bring it in?BeginnerYou must adopt 200 existing resources and don't want to write all the HCL by hand.IntermediateHow do you find out that someone changed infrastructure outside Terraform?BeginnerSomeone opened port 22 on a security group in the console. Terraform wants to remove it. What's your process?IntermediateAn Auto Scaling group's desired capacity is changed by autoscaling, and Terraform keeps resetting it.IntermediateA load balancer's TLS certificate must be rotated with no outage.Intermediate