Interview Prep/Terraform/Plan Errors and Unexpected Replacements

Removing one item from the middle of a list makes the plan destroy and recreate every subsequent resource.

IntermediateTroubleshootingPlan Errors and Unexpected Replacements
Answer

Likely causes → count with a list: instances are addressed by index, so removing [1] shifts [2] → [1] and so on. Terraform sees changed arguments at each index.

Diagnose → The plan shows aws_iam_user.u[1], [2], [3] being changed/replaced.

Fix → Switch to for_each = toset(var.users) with moved blocks from each index to its key, then remove the item.

Prevent → Use count only for "0 or 1" or truly identical copies; use for_each for collections.

All Terraform questions

More on Plan Errors and Unexpected Replacements