Skip to content
Latchkey

Terraform "Reference to undeclared module" in CI

An expression such as module.network.vpc_id refers to a module call that does not exist in the current module. Terraform has no module "network" block in scope, so the reference cannot resolve.

What this error means

plan or validate stops with "Error: Reference to undeclared module" and "No module call named \"X\" is declared in ...".

Terraform
Error: Reference to undeclared module

  on outputs.tf line 2, in output "vpc_id":
   2:   value = module.network.vpc_id

No module call named "network" is declared in the root module.

Common causes

The module block name does not match the reference

The reference uses module.network but the block is named differently, or the module block was removed while a reference remained.

The reference is in the wrong module scope

A child module cannot see the root module's calls; module.X only resolves within the module that declares that block.

How to fix it

Declare or rename the module block

  1. Confirm a module "network" block exists in the same configuration.
  2. Make the block name match the module.<name> used in the expression.
  3. Re-run validate to confirm the reference resolves.
main.tf
module "network" {
  source = "./modules/network"
}

output "vpc_id" {
  value = module.network.vpc_id
}

Move the reference into the right scope

Reference module outputs only from the module that declares the module call, passing values down via variables where needed.

How to prevent it

  • Keep module block names and references in sync.
  • Reference module.X only within the module that declares it.
  • Run terraform validate in CI to catch dangling references.

Related guides

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