Terraform "Reference to undeclared resource" in CI
Terraform validates that every resource.name you reference is actually declared. A reference to a removed, renamed, or misspelled resource fails validation before any plan runs.
What this error means
terraform plan/validate in CI reports a reference to an undeclared resource, pointing at the file and line where the bad address is used.
terraform
Error: Reference to undeclared resource
on main.tf line 42, in resource "aws_eip" "nat":
42: instance = aws_instance.gateway.id
A managed resource "aws_instance" "gateway" has not been declared in the
root module.Common causes
Resource renamed but reference not updated
The resource was renamed (or moved to a module) and a reference still uses the old address.
Typo in the resource name
A misspelled name or type does not match any declared resource.
Resource lives in a module
The target is inside a module and must be referenced via module.x.output, not directly.
How to fix it
Correct the reference
- Find the declared resource address and match the reference exactly.
- If it moved into a module, expose it as a module output and reference
module.x.output. - Run
terraform validatelocally before pushing.
Terminal
terraform validateHow to prevent it
- Use
moved {}blocks when renaming so references and state stay consistent. - Run
terraform validatein a pre-merge check. - Let an editor with HCL language support flag undeclared references.
Related guides
Terraform "value for undeclared variable" warning/error in CIFix Terraform "value for undeclared variable" in CI - a tfvars file or TF_VAR env var sets a variable the con…
Terraform "Missing required argument" in CIFix Terraform "Missing required argument" in CI - a resource, module, or provider block is missing an argumen…
Terraform "Duplicate resource" configuration error in CIFix Terraform "Duplicate resource" errors in CI - two blocks declare the same resource type and name, so Terr…