Course contentsShow
← Cloud and Infrastructure
Lesson 803 of 2,98915. Terraform in PracticeFree lesson

Module Output Values

Exposing resource attributes from modules using outputs for use by calling configurations.

Module Output Values

What you'll learn: How to expose specific resource attributes from a Terraform module so other parts of your infrastructure can use them.

The Core Concept

When you create a Terraform module, the resources inside are somewhat isolated—like items stored in a box. Module outputs act as labeled windows into that box, letting you selectively share specific values (like resource IDs, IP addresses, or DNS names) with the calling configuration.

Think of it like a vending machine: the internal mechanisms are hidden, but certain information (the selected item, change returned) is deliberately exposed for you to use.

Why Module Outputs Matter

Without outputs, a child module would be a black box—you'd create resources but have no way to reference their attributes. Outputs solve this by:

  • Exposing resource attributes created inside the module
  • Enabling composition between modules (one module's output becomes another's input)
  • Documenting interfaces for module users

Declaring Module Outputs

Inside your module directory, use the same output block syntax you learned earlier:

# Inside modules/network/outputs.tf
output "vpc_id" {
  description = "The ID of the VPC"
  value       = aws_vpc.main.id
}

output "subnet_ids" {
  description = "List of subnet IDs"
  value       = aws_subnet.private[*].id
}

Using Module Outputs

The calling configuration references module outputs using module.<NAME>.<OUTPUT>:

module "network" {
  source = "./modules/network"
  # ...inputs...
}

resource "aws_instance" "app" {
  subnet_id = module.network.subnet_ids[0]
  # Using the exposed output
}

Key Takeaway: Module outputs create a controlled interface for sharing resource attributes from child modules, enabling modular composition while maintaining encapsulation.