0
0
Terraformcloud~5 mins

Module outputs in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Module outputs
O(n)
Understanding Time Complexity

When using Terraform modules, outputs let us share information from one module to another.

We want to understand how the time to get these outputs changes as we add more outputs.

Scenario Under Consideration

Analyze the time complexity of retrieving outputs from a module.


module "example" {
  source = "./modules/example"
  count  = var.instance_count
}

output "instance_ids" {
  value = [for m in module.example : m.instance_id]
}
    

This code calls a module multiple times and collects all instance IDs as output.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Retrieving each module instance's output value.
  • How many times: Once per module instance (equal to var.instance_count).
How Execution Grows With Input

As the number of module instances grows, the number of output retrievals grows the same way.

Input Size (n)Approx. API Calls/Operations
1010 output retrievals
100100 output retrievals
10001000 output retrievals

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

Final Time Complexity

Time Complexity: O(n)

This means the time to get all outputs grows in a straight line as you add more module instances.

Common Mistake

[X] Wrong: "Getting outputs from multiple modules happens all at once, so time stays the same no matter how many modules there are."

[OK] Correct: Each module output must be retrieved separately, so more modules mean more work and more time.

Interview Connect

Understanding how output retrieval scales helps you design efficient Terraform modules and predict deployment times.

Self-Check

"What if we combined all outputs into a single map inside the module instead of separate outputs? How would the time complexity change?"