Skip to content
Latchkey

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

  1. Find the declared resource address and match the reference exactly.
  2. If it moved into a module, expose it as a module output and reference module.x.output.
  3. Run terraform validate locally before pushing.
Terminal
terraform validate

How to prevent it

  • Use moved {} blocks when renaming so references and state stay consistent.
  • Run terraform validate in a pre-merge check.
  • Let an editor with HCL language support flag undeclared references.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →