⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Terraform & IaC Interview Questions Scenario 45 of 120 in Terraform & IaC
Senior DevOps / SRE [L2] Terraform Terraform Modules & State Production Scenario [L2]
🎯 Target Role / Context: Terraform Scenario #40 (L2) · Use VPC ID from another module

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.

#Terraform #L2 #IaC #Cloud Infrastructure #VPC
🎙️ Candidate Opening & Architectural Context
"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.&lt;name&gt;.&lt;output&gt;</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."
Advertisement
⚡ Recommended Practice Lab

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.&lt;name&gt;.&lt;output&gt;</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.

1️⃣

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 output block exposes it.
  • Child inputs: the consuming module declares a matching variable to receive the value.
  • Root wiring: the root module connects them with module.vpc.private_subnet_ids style 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.
2️⃣

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.tf exports the IDs the consumer needs.
  • Consumer: modules/app/variables.tf declares typed variables with descriptions.
  • Root: main.tf passes module.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 init then terraform plan; Terraform shows the app module's inputs as known after apply until the VPC exists.
Advertisement
3️⃣

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_id to pick an instance, or module.vpc[*].vpc_id for 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 a for expression: { 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 console evaluates module.vpc.private_subnet_ids after the first apply.
  • Splat expressions: with count on the producer, module.vpc[*].private_subnet_ids returns a list of lists, so use flatten() when the consumer expects a flat list.
4️⃣

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 type and 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() or toset() deliberately when an output is a set but the consumer variable is a list.
5️⃣

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 description so 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_ids rather than subnets, 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.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Export only the IDs consumers need, and wire them in the root with <code>module.name.output</code>; if two modules need each other, extract the shared resource into a third module."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Trying to reference a resource inside another module directly Child module internals are not accessible; only declared outputs are.
  2. Adding depends_on where a reference already exists It adds unnecessary coupling and can create cycles or delay planning.
  3. Outputting entire resource objects Consumers then depend on provider schema details that break during refactors.
  4. Forgetting the index on count or for_each modules The expression fails because the module is a list or map, not a single object.
Advertisement
Want more Terraform & IaC scenarios?
Explore our complete collection of scenario-based Terraform & IaC interview runbooks.
Browse All Terraform & IaC Questions →