Interview Prep/Terraform/Plan Errors and Unexpected Replacements

Renaming a security group's `name` forces replacement, and the apply fails with `InvalidGroup.Duplicate` or hangs on `DependencyViolation`.

AdvancedTroubleshootingPlan Errors and Unexpected Replacements
Answer

Likely causes → name forces a new group. Default order is destroy-then-create, but the old group is attached to ENIs and can't be deleted. With create_before_destroy, the same name conflicts.

Diagnose →

aws ec2 describe-network-interfaces --filters Name=group-id,Values=sg-0abc123 \
  --query 'NetworkInterfaces[].[NetworkInterfaceId,Description]'

Fix → Use name_prefix plus create_before_destroy = true so a new group is created, attached, then the old one deleted. Or revert the rename (description/tags may be enough).

Prevent → Security groups should use name_prefix and lifecycle CBD from day one.

All Terraform questions

More on Plan Errors and Unexpected Replacements