Q: How do you reference the output of one module in another in the same root module?
To reference the output of one module in another in the same root module, declare an output in the first module and pass it to the second as an input argument using module.name.output.
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To reference the output of one module in another in the same root module, declare an <code>output</code> in the first module and pass it to the second as an input argument using <code>module.<name>.<output></code>. Modules never read each other's internals; the root module wires them together, and Terraform infers the creation order from that reference. Interviewers use this question to check you understand module interfaces (variables in, outputs out), implicit dependencies, and how to avoid circular wiring when networking, compute and database modules depend on each other.
The module interface: variables in, outputs out
A module exposes only what its outputs declare. This separation is what lets teams swap or upgrade a module without touching its consumers, as long as the outputs keep their shape.
- Child outputs: a resource attribute is invisible outside the module until an
outputblock exposes it. - Child inputs: the consuming module declares a matching
variableto receive the value. - Root wiring: the root module connects them with
module.vpc.private_subnet_idsstyle expressions. - Implicit ordering: that reference adds a dependency edge, so the VPC module is applied before the app module without
depends_on. - Terminology: the calling configuration is the parent, the called one is the child; the root module is the directory where you run
terraform plan.
Hands-on: reference the output of one module in another
Three files: the producer's output, the consumer's variable and the root wiring. The example below is the complete pattern, and every cross-module reference in a real project follows the same three steps.
# modules/vpc/outputs.tf
output "vpc_id" {
value = aws_vpc.this.id
}
output "private_subnet_ids" {
value = aws_subnet.private[*].id
}
# modules/app/variables.tf
variable "vpc_id" { type = string }
variable "subnet_ids" { type = list(string) }
# main.tf (root)
module "vpc" {
source = "./modules/vpc"
}
module "app" {
source = "./modules/app"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnet_ids
}
- Producer:
modules/vpc/outputs.tfexports the IDs the consumer needs. - Consumer:
modules/app/variables.tfdeclares typed variables with descriptions. - Root:
main.tfpassesmodule.vpc.*into the app module call. - Types: keep outputs typed and minimal, for example a list of subnet IDs, not the whole resource object.
- Run it:
terraform initthenterraform plan; Terraform shows the app module's inputs as known after apply until the VPC exists.
Modules with count and for_each
The reference syntax changes when a module call is expanded. Expanded modules are the most common source of confusing reference errors in larger codebases.
module "vpc" {
for_each = toset(["prod", "stage"])
source = "./modules/vpc"
env = each.key
}
module "app_prod" {
source = "./modules/app"
subnet_ids = module.vpc["prod"].private_subnet_ids
}
- count: use
module.vpc[0].vpc_idto pick an instance, ormodule.vpc[*].vpc_idfor all. Pick the form that matches how the producer module is called. - for_each: use
module.vpc["prod"].vpc_id, or build a map with aforexpression:{ for k, m in module.vpc : k => m.vpc_id }. - Sensitive: an output derived from a sensitive value must be marked
sensitive = true, and consumers keep the flag. - Inspect:
terraform consoleevaluatesmodule.vpc.private_subnet_idsafter the first apply. - Splat expressions: with
counton the producer,module.vpc[*].private_subnet_idsreturns a list of lists, so useflatten()when the consumer expects a flat list.
Failure modes and fixes
Most errors are missing outputs, wrong types or cycles. Reading the error message carefully usually tells you which side of the interface is missing or mismatched. Fix the interface rather than working around it.
- Unsupported attribute: "This object does not have an attribute named ..." means the child module has no such output. Add the output block and re-run.
- Type mismatch: passing a string where a list is expected. Check variable
typeand the output's value shape. - Cycle between modules: if networking needs a security group from compute and compute needs a subnet from networking, split the shared piece into a third module.
- depends_on on modules: only add it for hidden side effects; references already create ordering.
- Type conversion: use
tolist()ortoset()deliberately when an output is a set but the consumer variable is a list.
Practice for maintainable modules
Design outputs as a stable API. Good module interfaces are small, typed and documented, which keeps root modules readable.
- Describe outputs: add
descriptionso the registry and docs are useful. - Avoid leaking internals: export IDs and ARNs, not entire resource objects, so refactors inside the module do not break consumers.
- Version modules: when you rename or remove an output, treat it as a breaking change and bump the module version.
- Naming outputs: choose names that describe the value, such as
private_subnet_idsrather thansubnets, so call sites read clearly. - Testing: use
terraform test(Terraform 1.6 and later) or a small example directory to check that outputs keep their shape when the module changes.
- You expose a value with an output block in module A and pass module.a.output_name into a variable of module B in the root module.
- I check wiring in terraform console and with plan, where a missing output fails immediately.
- I treat outputs as a versioned API, describe them and avoid leaking whole resource objects.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Trying to reference a resource inside another module directly Child module internals are not accessible; only declared outputs are.
- Adding depends_on where a reference already exists It adds unnecessary coupling and can create cycles or delay planning.
- Outputting entire resource objects Consumers then depend on provider schema details that break during refactors.
- Forgetting the index on count or for_each modules The expression fails because the module is a list or map, not a single object.