A new AMI release makes Terraform want to replace every EC2 instance in prod. How do you control this?
IntermediateScenarioImport Refactoring Drift and Lifecycle
Answer
Situation read → most_recent AMI lookups change the AMI attribute, which forces replacement.
What I'd do →
- Pin the AMI ID per environment via a variable.
- Promote new AMIs through dev and stage first.
- For fleets, use a launch template plus an ASG with
instance_refreshso rollout is rolling instead of all-at-once.
Why → You choose when a new image reaches prod, and rollouts replace instances gradually.
Interview tip → ignore_changes = [ami] hides the problem; use it only where an external pipeline owns the AMI.
More on Import Refactoring Drift and Lifecycle
An S3 bucket was created by hand in the console and now must be managed by Terraform. How do you bring it in?BeginnerYou must adopt 200 existing resources and don't want to write all the HCL by hand.IntermediateHow do you find out that someone changed infrastructure outside Terraform?BeginnerSomeone opened port 22 on a security group in the console. Terraform wants to remove it. What's your process?IntermediateAn Auto Scaling group's desired capacity is changed by autoscaling, and Terraform keeps resetting it.IntermediateHow do you protect a production database from an accidental `terraform destroy`?Beginner