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 ...".
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
- Confirm a
module "network"block exists in the same configuration. - Make the block name match the
module.<name>used in the expression. - Re-run validate to confirm the reference resolves.
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.Xonly within the module that declares it. - Run
terraform validatein CI to catch dangling references.