Interview Prep/Terraform/Plan Errors and Unexpected Replacements

`Error: Reference to undeclared resource` — `A managed resource "aws_vpc" "main" has not been declared in the root module.`

BeginnerTroubleshootingPlan Errors and Unexpected Replacements
Answer

Likely causes → The VPC is defined inside a module (so you need module.network.vpc_id), the resource name differs (aws_vpc.this), or the file defining it was deleted/renamed with a non-.tf extension.

Diagnose → grep -rn 'resource "aws_vpc"' .

Fix → Reference the right address or module output; if the VPC exists outside this stack, use data "aws_vpc".

Prevent → Run terraform validate in pre-commit and PR checks.

All Terraform questions

More on Plan Errors and Unexpected Replacements