0
0
Terraformcloud~5 mins

Outputs for module communication in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Outputs for module communication
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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).
How Execution Grows With Input

As you add more module instances, Terraform reads more outputs.

Input Size (n)Approx. Api Calls/Operations
1010 output reads
100100 output reads
10001000 output reads

Pattern observation: The work grows directly with the number of module outputs collected.

Final Time Complexity

Time Complexity: O(n)

This means the time to gather outputs grows in a straight line as you add more modules.

Common Mistake

[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.

Interview Connect

Understanding how output gathering scales helps you design modules that communicate efficiently without slowing down your infrastructure deployment.

Self-Check

"What if we combined multiple outputs into one object per module instead of separate outputs? How would that affect the time complexity?"