Interview Prep/Terraform/Plan Errors and Unexpected Replacements

Plan shows `# aws_instance.web must be replaced` with `~ ami = "ami-0aaa" -> "ami-0bbb" # forces replacement`, but nobody changed the AMI.

IntermediateTroubleshootingPlan Errors and Unexpected Replacements
Answer

Likely causes → data "aws_ami" with most_recent = true picked up a newly published image.

Diagnose → terraform state show data.aws_ami.al2023 | grep -E 'id|creation_date'

Fix → Pin the AMI ID in a variable updated by a deliberate PR, or lifecycle { ignore_changes = [ami] } and roll instances via ASG instance refresh instead.

Prevent → For fleets, use launch templates + ASGs so a new AMI rolls gradually instead of replacing instances during an apply.

All Terraform questions

More on Plan Errors and Unexpected Replacements