Explain the Terraform resource lifecycle and meta-arguments like create_before_destroy.

Medium Topic: Terraform May 24, 2026

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.

← Previous What is the purpose of terraform.tfvars files? Next → What are Terraform providers and how do you...

Practice Similar Questions

Back to Terraform Topics