Outputs for module communication in Terraform - Time & Space Complexity
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?"