Interview Prep/Terraform/Plan Errors and Unexpected Replacements

A `for_each` resource is replaced after changing a key from `"web-1"` to `"web_1"` for naming consistency.

BeginnerTroubleshootingPlan Errors and Unexpected Replacements
Answer

Likely causes → for_each keys are part of the resource address; a new key means a new object.

Diagnose → The plan shows aws_instance.app["web-1"] destroyed and ["web_1"] created.

Fix →

moved {
  from = aws_instance.app["web-1"]
  to   = aws_instance.app["web_1"]
}

Prevent → Choose stable keys early; never derive keys from mutable display names.

All Terraform questions

More on Plan Errors and Unexpected Replacements