Outputs for module communication in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to get outputs from one module to another grows as we add more outputs.
How does the number of outputs affect the work Terraform does during planning and applying?
Analyze the time complexity of outputting multiple values from a module.
module "example" {
source = "./example_module"
count = var.module_count
}
output "module_outputs" {
value = [for m in module.example : m.some_output]
}
This code collects outputs from multiple instances of a module and exposes them as a list.
Look at what repeats as the number of modules grows.
- Primary operation: Reading each module instance's output value.
- How many times: Once per module instance (n times).
As you add more module instances, Terraform reads more outputs.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 output reads |
| 100 | 100 output reads |
| 1000 | 1000 output reads |
Pattern observation: The work grows directly with the number of module outputs collected.
Time Complexity: O(n)
This means the time to gather outputs grows in a straight line as you add more modules.
[X] Wrong: "Getting outputs from many modules happens instantly, no matter how many there are."
[OK] Correct: Each output requires Terraform to read and process data, so more outputs mean more work and longer time.
Understanding how output gathering scales helps you design modules that communicate efficiently without slowing down your infrastructure deployment.
"What if we combined multiple outputs into one object per module instead of separate outputs? How would that affect the time complexity?"
Practice
output blocks in Terraform modules?Solution
Step 1: Understand the role of output blocks
Output blocks are used to expose values from a module so other modules or users can access them.Step 2: Differentiate outputs from other blocks
Unlike resource or variable blocks, outputs do not create or accept input but share information.Final Answer:
To share values from one module to another or to the user -> Option AQuick Check:
Outputs share values = D [OK]
- Confusing outputs with resource creation
- Thinking outputs accept user input
- Assuming outputs delete resources
instance_ip with value aws_instance.web.private_ip?Solution
Step 1: Recall Terraform output block syntax
Outputs use the syntax: output "name" { value = expression }Step 2: Match syntax with options
output "instance_ip" { value = aws_instance.web.private_ip } matches the correct syntax with quotes and value assignment inside braces.Final Answer:
output "instance_ip" { value = aws_instance.web.private_ip } -> Option BQuick Check:
Correct output block syntax = B [OK]
- Missing quotes around output name
- Using equals sign outside braces
- Placing value outside output block
output "db_endpoint" {
value = aws_db_instance.main.endpoint
}And this root module code:
module "database" {
source = "./modules/db"
}
output "database_url" {
value = module.database.db_endpoint
}What will
terraform output database_url show after deployment?Solution
Step 1: Understand module output forwarding
The module outputs 'db_endpoint' which is accessed in root as 'module.database.db_endpoint'.Step 2: Check root output value
The root output 'database_url' uses the module output value, so it will show the actual endpoint string.Final Answer:
The endpoint address of the AWS database instance -> Option AQuick Check:
Module output forwarded = A [OK]
- Thinking output shows variable name as string
- Assuming outputs cannot be nested
- Confusing endpoint with IP address
output "server_ip" {
value = aws_instance.app.private_ip
}But when you run
terraform apply, you get an error: Reference to undeclared resource. What is the likely cause?Solution
Step 1: Analyze the error message
'Reference to undeclared resource' means Terraform cannot find 'aws_instance.app' in the module.Step 2: Check output references
Outputs must reference existing resources; if resource is missing, error occurs.Final Answer:
The resource aws_instance.app is not defined in the module -> Option DQuick Check:
Missing resource causes reference error = A [OK]
- Assuming outputs can't reference resources
- Thinking underscores are invalid in output names
- Believing description is mandatory
output "instance_ips" {
value = [for i in aws_instance.app : i.private_ip]
}How can you use this output in the root module to create a security group rule allowing SSH from all these IPs?
Solution
Step 1: Understand output is a list of IPs
The output returns a list of private IP addresses from multiple instances.Step 2: Use for_each with a set for unique IPs
Security group rules can be created per IP using for_each with toset() to avoid duplicates.Final Answer:
Use for_each = toset(module.app.instance_ips) in the security group rule resource -> Option CQuick Check:
Use for_each with toset for multiple IP rules = C [OK]
- Assigning list directly to cidr_blocks without for_each
- Using count without mapping IPs
- Thinking outputs can't be used in resource blocks
