Input variable precedence order in Terraform - Time & Space Complexity
We want to understand how Terraform decides which input variable value to use when multiple sources provide values.
How does the number of input sources affect the time it takes to determine the final value?
Analyze the time complexity of resolving input variable values from multiple sources.
variable "example" {
type = string
default = "default_value"
}
# Possible input sources:
# 1. CLI -var option
# 2. Environment variable TF_VAR_example
# 3. terraform.tfvars file
# 4. Default value in variable block
# Terraform picks the value based on precedence order.
This shows the variable declaration and the possible input sources Terraform checks in order.
Terraform checks each input source one by one until it finds a value.
- Primary operation: Checking each input source for the variable value.
- How many times: Once per input source, in a fixed order.
As the number of input sources increases, Terraform checks each source in order until it finds a value.
| Input Sources (n) | Approx. Checks |
|---|---|
| 2 | Up to 2 checks |
| 4 | Up to 4 checks |
| 10 | Up to 10 checks |
Pattern observation: The number of checks grows linearly with the number of input sources.
Time Complexity: O(n)
This means the time to find the variable value grows directly with the number of input sources checked.
[X] Wrong: "Terraform checks all input sources every time and merges values."
[OK] Correct: Terraform stops checking as soon as it finds a value, so it does not always check all sources.
Understanding how input variable precedence works helps you explain how Terraform handles configuration inputs efficiently and predictably.
"What if Terraform allowed parallel checking of input sources? How would that affect the time complexity?"