String interpolation in Terraform - Time & Space Complexity
We want to understand how the time to process string interpolation changes as we add more variables.
How does the number of variables affect the work Terraform does to build the final string?
Analyze the time complexity of the following operation sequence.
variable "names" {
type = list(string)
}
output "greeting" {
value = "Hello, ${join(", ", var.names)}!"
}
This code creates a greeting string by joining a list of names with commas.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Joining each name in the list into one string.
- How many times: Once per name in the list.
As the number of names grows, Terraform must process each name to build the final string.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 string joins |
| 100 | 100 string joins |
| 1000 | 1000 string joins |
Pattern observation: The work grows directly with the number of names.
Time Complexity: O(n)
This means the time to build the string grows in a straight line as you add more names.
[X] Wrong: "String interpolation happens instantly no matter how many variables there are."
[OK] Correct: Each variable must be processed and combined, so more variables mean more work.
Understanding how string operations scale helps you design efficient infrastructure code that runs smoothly even as inputs grow.
"What if we changed the join separator to a function that processes each name? How would the time complexity change?"