Output values after apply in Terraform - Time & Space 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?
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.
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.
As the number of instances grows, Terraform reads more attributes to build the outputs.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 attribute reads |
| 100 | 100 attribute reads |
| 1000 | 1000 attribute reads |
Pattern observation: The work grows directly with the number of resources included in the outputs.
Time Complexity: O(n)
This means the time to produce output values grows in a straight line as you add more resources.
[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.
Understanding how output generation scales helps you design Terraform code that stays efficient as your infrastructure grows.
"What if we changed outputs to include nested maps instead of lists? How would the time complexity change?"