Outputs as documentation in Terraform - Time & Space Complexity
We want to understand how the time to process outputs in Terraform changes as we add more outputs.
Specifically, how does the number of outputs affect the work Terraform does when showing or using them?
Analyze the time complexity of the following Terraform output declarations.
output "example_1" {
value = aws_instance.example[0].id
}
output "example_2" {
value = aws_instance.example[1].id
}
output "example_n" {
value = aws_instance.example[n-1].id
}
This sequence defines multiple outputs, each referencing a different resource instance ID.
Each output causes Terraform to read and display a resource attribute.
- Primary operation: Reading resource attributes for each output
- How many times: Once per output defined
As the number of outputs increases, Terraform must process each output separately.
| 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 outputs.
Time Complexity: O(n)
This means the time to process outputs grows in a straight line as you add more outputs.
[X] Wrong: "Adding more outputs won't affect performance much because outputs are just labels."
[OK] Correct: Each output requires Terraform to fetch and prepare data, so more outputs mean more work.
Understanding how outputs scale helps you design Terraform configurations that stay efficient and clear as they grow.
"What if outputs referenced computed values that require extra API calls? How would the time complexity change?"