Interview Prep/Real-World Interview/Write-Terraform tasks

Protect a production database from accidental `terraform destroy`.

BeginnerHands-onWrite-Terraform tasks
Answer
resource "aws_db_instance" "prod" {
  identifier          = "prod-db"
  engine              = "postgres"
  instance_class      = "db.r6g.large"
  allocated_storage   = 100
  deletion_protection = true
  skip_final_snapshot = false
  final_snapshot_identifier = "prod-db-final"

  lifecycle {
    prevent_destroy = true
    ignore_changes  = [password]
  }
}

Key trick → prevent_destroy fails the plan in Terraform, and deletion_protection stops the delete in AWS itself, even from the console. Use both.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-Terraform tasks