Variable validation rules in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to check variable rules grows as we add more variables.
How does validation time change when we have many variables with rules?
Analyze the time complexity of the following variable validation rules.
variable "names" {
type = list(string)
validation {
condition = alltrue([for n in var.names : length(n) > 2])
error_message = "Each name must be longer than 2 characters."
}
}
variable "count" {
type = number
validation {
condition = var.count > 0
error_message = "Count must be positive."
}
}
This checks each name in a list and a single number variable for rules.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Checking each item in the list against the condition.
- How many times: Once per item in the list variable.
As the number of items in the list grows, the number of checks grows too.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows directly with the number of items.
Time Complexity: O(n)
This means the validation time grows in a straight line as the list gets bigger.
[X] Wrong: "Validation time stays the same no matter how many items are in the list."
[OK] Correct: Each item must be checked, so more items mean more checks and more time.
Understanding how validation scales helps you design efficient configurations and shows you think about cost and speed.
"What if we added nested lists inside the variable? How would the time complexity change?"
Practice
variable validation rules in Terraform?Solution
Step 1: Understand variable validation role
Variable validation rules ensure inputs are correct before Terraform uses them.Step 2: Identify the purpose
They check conditions and show errors if inputs are invalid, preventing mistakes.Final Answer:
To check if input values meet specific conditions before applying configuration -> Option DQuick Check:
Validation checks inputs = C [OK]
- Thinking validation fixes errors automatically
- Confusing validation with variable creation
- Assuming validation speeds up apply
Solution
Step 1: Recall Terraform variable validation syntax
Terraform usesvalidationblock inside variable withconditionanderror_message.Step 2: Match syntax with options
variable "name" { validation { condition = length(var.name) > 3 error_message = "Name too short" } } matches correct keywords and structure exactly.Final Answer:
variable "name" { validation { condition = length(var.name) > 3 error_message = "Name too short" } } -> Option AQuick Check:
Correct keywords: validation, condition, error_message = A [OK]
- Using 'validate' instead of 'validation'
- Using wrong keys like 'message' or 'error'
- Incorrect block names like 'validation_rule'
variable "username" {
type = string
validation {
condition = length(var.username) >= 3
error_message = "Username must be at least 3 characters"
}
}Solution
Step 1: Check validation condition
The condition requires username length >= 3.Step 2: Evaluate input "ab" length
"ab" length is 2, which is less than 3, so condition fails.Final Answer:
Terraform apply fails with error: Username must be at least 3 characters -> Option CQuick Check:
Input length < 3 triggers error = A [OK]
- Assuming apply succeeds despite validation failure
- Thinking validation only warns, not errors
- Believing default values replace invalid input
variable "port" {
type = number
validation {
condition = var.port > 0 && var.port < 65536
error_message = "Port must be between 1 and 65535"
}
}Solution
Step 1: Understand validation condition context
Inside validation, useselfto refer to the variable value, notvar.port.Step 2: Identify incorrect usage
The condition incorrectly usesvar.port, which is not best practice.Final Answer:
Validation condition uses 'var.port' instead of 'self' -> Option BQuick Check:
Use 'self' in validation condition = B [OK]
- Using 'var.variable_name' inside validation condition
- Placing validation block outside variable block
- Ignoring syntax errors in condition
Solution
Step 1: Check list length condition
We want exactly 3 items, solength(self) == 3is correct.Step 2: Validate each string length
Useall()to ensure all elements satisfylength(s) >= 2.Step 3: Identify correct function and condition
all()is the modern function;alltrue()is legacy.Final Answer:
condition = length(self) == 3 && all([for s in self : length(s) >= 2]) -> Option AQuick Check:
Use 'all' with length checks and list length = D [OK]
- Using 'alltrue' instead of 'all'
- Comparing strings directly to numbers
- Using 's > 2' instead of 'length(s) >= 2'
