Interview Prep/Terraform/Plan Errors and Unexpected Replacements

A plan replaces an ALB target group after changing `port`, and the apply fails with `ResourceInUse: Target group is currently in use by a listener or a rule`.

AdvancedTroubleshootingPlan Errors and Unexpected Replacements
Answer

Likely causes → port/protocol/name force replacement. Destroy-before-create can't delete a target group still referenced by a listener.

Diagnose → aws elbv2 describe-target-groups --names app-tg --query 'TargetGroups[0].LoadBalancerArns'

Fix → Use name_prefix and create_before_destroy = true on the target group so the listener moves to the new one first.

Prevent → Make replaceable resources CBD-friendly (name_prefix, no hardcoded names).

All Terraform questions

More on Plan Errors and Unexpected Replacements