0
0
Terraformcloud~5 mins

Output values after apply in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Output values after apply
O(n)
Understanding Time Complexity

We want to understand how the time to get output values changes as we add more outputs in Terraform.

How does the number of outputs affect the work Terraform does after applying?

Scenario Under Consideration

Analyze the time complexity of the following output declarations.

output "ip_addresses" {
  value = [for instance in aws_instance.example : instance.public_ip]
}

output "instance_ids" {
  value = aws_instance.example[*].id
}

This code outputs lists of IP addresses and instance IDs from multiple instances.

Identify Repeating Operations

Look at what Terraform does repeatedly to produce outputs.

  • Primary operation: Reading each resource's attribute to include in the output.
  • How many times: Once per resource instance included in the output list.
How Execution Grows With Input

As the number of instances grows, Terraform reads more attributes to build the outputs.

Input Size (n)Approx. API Calls/Operations
1010 attribute reads
100100 attribute reads
10001000 attribute reads

Pattern observation: The work grows directly with the number of resources included in the outputs.

Final Time Complexity

Time Complexity: O(n)

This means the time to produce output values grows in a straight line as you add more resources.

Common Mistake

[X] Wrong: "Getting outputs is instant no matter how many resources there are."

[OK] Correct: Terraform must read each resource's data to show outputs, so more resources mean more work.

Interview Connect

Understanding how output generation scales helps you design Terraform code that stays efficient as your infrastructure grows.

Self-Check

"What if we changed outputs to include nested maps instead of lists? How would the time complexity change?"