Sunday, 29 June 2025

Terraform Module Outputs

 ✅ Importance of Terraform Module Outputs 

Terraform module outputs are a powerful mechanism to expose values from a module to its calling code. These outputs act like return values in programming, allowing higher-level modules or root configurations to reference results from reusable components. 

 

🧠 Why Outputs Are Important 

Purpose 

Description 

Expose values to parent modules 

 

Share IDs, ARNs, DNS names, etc., from child modules (like VPC ID, subnet IDs) 

Use output values in other resources 

 

Chain dependencies across modules (e.g., use a VPC ID from one module in another) 

Enable cross-module communication 

 

Output of one module can become an input to another (e.g., EKS outputs feed into ALB module) 

Debugging and visibility 

 

 

Print values to console after terraform apply to confirm resource creation 

Automation & CI/CD 

 

Output values can be consumed in GitHub Actions, scripts, or other pipelines 

Stateful resource references 

 

Avoid hardcoding — use dynamic, dependency-aware values from the actual state 

 

🧩 Example 

 

# modules/vpc/outputs.tf 
output "vpc_id" { 
 value = aws_vpc.main.id 
} 
 
output "public_subnet_ids" { 
 value = aws_subnet.public[*].id 
} 
 

 

# envs/prod/main.tf 
module "vpc" { 
 source         = "../../modules/vpc" 
 vpc_cidr       = "10.0.0.0/16" 
 public_subnets = ["10.0.1.0/24", "10.0.2.0/24"] 
 ... 
} 
 
output "vpc_id" { 
 value = module.vpc.vpc_id 
} 
 

 

📦 Example Use in Other Modules 

hcl 

CopyEdit 

module "eks" { 
 source     = "../../modules/eks" 
 vpc_id     = module.vpc.vpc_id 
 subnet_ids = module.vpc.public_subnet_ids 
 ... 
} 
 

This makes your infrastructure: 

  • Composable: Each module is clean and self-contained. 

  • Reusable: No need to rewrite hardcoded resource names or IDs. 

  • Maintainable: Output values update automatically with the state. 

  • Secure: You can choose to mark outputs as sensitive. 

 

🔐 Sensitive Output Example 

 

output "db_password" { 
 value     = random_password.db.result 
 sensitive = true 
} 
 

Terraform will hide the value in CLI output, logs, and UI. 

 

 

✅ Summary 

Benefit 

Explanation 

Communication between modules 

Share key values like IDs, ARNs, names 

Dynamic infrastructure 

Avoid hardcoding and make code more portable 

Easier debugging 

Outputs help inspect values after apply 

Better CI/CD integration 

Used in scripts and pipelines 

Security 

sensitive = true protects secrets 

 

 

📦 Full Example 

# Calling the module 
module "vpc" { 
 source = "../../modules/vpc" 
 vpc_cidr = "10.0.0.0/16" 
 ... 
} 
 
# Referencing the module output 
output "vpc_id" { 
 value = module.vpc.vpc_id 
} 
 

And in the module code itself: 

# modules/vpc/outputs.tf 
output "vpc_id" { 
 value = aws_vpc.this.id 
} 
 

 

🧠 Why This is Powerful 

Using module.vpc.vpc_id: 

  • Ensures clean code without repeating resource logic. 

  • Allows you to reference outputs dynamically (not hardcoded). 

  • Keeps resources loosely coupled and reusable. 

 

✅ Summary 

Term 

Meaning 

module 

Terraform's object for accessing modules 

vpc 

The name you gave your module instance 

vpc_id 

The output defined inside the VPC module 

No comments:

Post a Comment