Course contentsShow
← Cloud and Infrastructure
Lesson 767 of 2,98914. Terraform FundamentalsFree lesson

Using Outputs in Root and Child Modules

Pass data between modules and expose outputs to parent configurations and callers.

Using Outputs in Root and Child Modules

What you'll learn: How to pass data from child modules back to the root module and between different parts of your Terraform configuration.

The Core Idea

Outputs in Terraform serve as the export interface of a module. When you define an output in a child module, you're essentially saying, "Here's some information that callers of this module might need." The root module (your main configuration) can then access these outputs and use them elsewhere—or even expose them as its own outputs.

Think of it like a function in programming: a child module is like a function that does work, and outputs are the return values you can use after calling it.

How Outputs Flow Between Modules

When you call a child module using a module block, any outputs defined in that child module become accessible through the module reference:

module "networking" {
  source = "./modules/vpc"
  # ... arguments ...
}

# Access child module outputs
resource "aws_instance" "app" {
  subnet_id = module.networking.subnet_id
  # Uses the 'subnet_id' output from the child module
}

The child module (./modules/vpc) would define:

output "subnet_id" {
  value = aws_subnet.main.id
}

Root Module Outputs

Your root module can also expose outputs—these become the final values displayed after terraform apply and can be queried with terraform output. You might expose a child module's output directly or transform it:

output "app_subnet" {
  value = module.networking.subnet_id
}

Key Takeaway: Outputs create a data pipeline between modules—child modules expose information through outputs, parent modules consume them via module.<name>.<output>, enabling modular and composable infrastructure code.