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.
More on Write-Terraform tasks
Write Terraform for an S3 bucket with versioning, encryption and public access blocked.BeginnerWrite the remote backend configuration with S3 and state locking.BeginnerWrite a VPC with public and private subnets across 3 AZs using `count` or `for_each`.IntermediateWrite a reusable module for an EC2 instance with inputs, outputs and validation.AdvancedWrite a security group that allows HTTPS from anywhere and SSH only from an office CIDR.BeginnerCreate multiple IAM users from a list with `for_each`, and output their ARNs as a map.Beginner