Explain the Terraform resource lifecycle and meta-arguments like create_before_destroy.
The lifecycle block gives you fine-grained control over how Terraform manages resource replacement:
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t3.medium"
lifecycle {
create_before_destroy = true # New instance created before old one is destroyed
ignore_changes = [ami] # Ignore external AMI changes
prevent_destroy = true # Block accidental deletion
}
}
create_before_destroy is critical for zero-downtime replacements. Without it, Terraform destroys the old resource first, creating a gap in availability.