Interview Prep/Terraform/Plan Errors and Unexpected Replacements

`Error: Missing resource instance key` — `Because aws_instance.web has "count" set, its attributes must be accessed on specific instances.`

BeginnerTroubleshootingPlan Errors and Unexpected Replacements
Answer

Likely causes → You added count (or for_each) to a resource but references still use aws_instance.web.id.

Diagnose → grep -rn 'aws_instance.web\.' .

Fix → Use an index or splat: aws_instance.web[0].id, aws_instance.web[*].id, or values(aws_instance.web)[*].id for for_each. Add a moved block from aws_instance.web to aws_instance.web[0] so it isn't replaced.

Prevent → When introducing count/for_each, update references and add moved blocks in the same PR.

All Terraform questions

More on Plan Errors and Unexpected Replacements