Variable validation blocks in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When Terraform runs, it checks if variables meet rules set by validation blocks.
We want to know how the time to check variables changes as we add more variables or rules.
Analyze the time complexity of the following variable validation blocks.
variable "instance_count" {
type = number
validation {
condition = var.instance_count > 0
error_message = "Must be greater than zero."
}
}
variable "instance_names" {
type = list(string)
validation {
condition = length(var.instance_names) == var.instance_count
error_message = "Names count must match instance count."
}
}
This code checks that the number of instances is positive and that the list of names matches the count.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Evaluating each validation condition for every variable.
- How many times: Once per variable with validation blocks during plan or apply.
Each variable with validation adds a fixed number of checks.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 validation checks |
| 100 | 100 validation checks |
| 1000 | 1000 validation checks |
Pattern observation: The number of checks grows directly with the number of variables having validations.
Time Complexity: O(n)
This means the time to validate grows in a straight line as you add more variables with validation.
[X] Wrong: "Validation time stays the same no matter how many variables we add."
[OK] Correct: Each variable with validation adds extra checks, so more variables mean more time.
Understanding how validation scales helps you design Terraform code that stays fast and clear as it grows.
"What if we added nested validation inside a variable with many elements? How would the time complexity change?"
Practice
validation block inside a Terraform variable?Solution
Step 1: Understand variable validation purpose
The validation block is used to enforce rules on input values to prevent invalid configurations.Step 2: Differentiate from other variable features
Default values assign fallback values, type declares data type, and output shows results, but validation specifically checks input correctness.Final Answer:
To check if the input value meets specific rules before applying the configuration -> Option AQuick Check:
Validation block purpose = input checking [OK]
- Confusing validation with default value assignment
- Thinking validation outputs variable values
- Mixing validation with type declaration
Solution
Step 1: Identify correct block and attribute names
The correct block isvalidationwith attributesconditionanderror_message.Step 2: Check variable references and syntax
Inside the condition, usevar.exampleto refer to the variable value. variable "example" { validation { condition = length(var.example) > 0 error_message = "Must not be empty" } } matches this exactly.Final Answer:
variable "example" { validation { condition = length(var.example) > 0 error_message = "Must not be empty" } } -> Option BQuick Check:
Validation syntax = correct block and attributes [OK]
- Using 'validate' instead of 'validation'
- Using wrong attribute names like 'check' or 'error'
- Referencing variable without 'var.' prefix
variable "port" {
type = number
validation {
condition = var.port >= 1024 && var.port <= 65535
error_message = "Port must be between 1024 and 65535"
}
}What happens if you set
port = 80 when applying Terraform?Solution
Step 1: Analyze the validation condition
The condition requires the port to be between 1024 and 65535 inclusive.Step 2: Check the input value against the condition
Port 80 is less than 1024, so the condition fails.Step 3: Understand Terraform behavior on validation failure
Terraform stops and shows the error message fromerror_message.Final Answer:
Terraform will fail with error: Port must be between 1024 and 65535 -> Option DQuick Check:
Validation fails = error message shown [OK]
- Assuming Terraform applies anyway
- Thinking default values are used automatically
- Expecting interactive prompts for invalid input
variable "env" {
type = string
validation {
condition = var.env == "dev" || "prod"
error_message = "env must be 'dev' or 'prod'"
}
}Solution
Step 1: Review the condition expression
The conditionvar.env == "dev" || "prod"is invalid because "prod" alone is always true.Step 2: Correct the condition syntax
It should bevar.env == "dev" || var.env == "prod"to compare both values explicitly.Final Answer:
The condition syntax is incorrect; it should compare both values explicitly -> Option CQuick Check:
Logical OR needs full comparisons [OK]
- Writing incomplete logical expressions
- Assuming string alone works as condition
- Confusing error_message spelling
users so it must have at least 2 unique names and none can be empty strings. Which validation block correctly enforces this?Solution
Step 1: Check list length and uniqueness
Condition requires at least 2 items and all must be unique, solength(var.users) >= 2andlength(distinct(var.users)) == length(var.users)ensure this.Step 2: Ensure no empty strings
Thealltrue([for u in var.users : u != ""])checks every user is not empty.Step 3: Compare options
validation { condition = length(var.users) >= 2 && length(distinct(var.users)) == length(var.users) && alltrue([for u in var.users : u != ""]) error_message = "Users must have 2+ unique non-empty names" } includes all these checks correctly; others miss empty string check or uniqueness properly.Final Answer:
validation { condition = length(var.users) >= 2 && length(distinct(var.users)) == length(var.users) && alltrue([for u in var.users : u != ""]) error_message = "Users must have 2+ unique non-empty names" } -> Option AQuick Check:
All conditions combined = validation { condition = length(var.users) >= 2 && length(distinct(var.users)) == length(var.users) && alltrue([for u in var.users : u != ""]) error_message = "Users must have 2+ unique non-empty names" } [OK]
- Missing empty string check
- Using > 2 instead of >= 2 for minimum count
- Not verifying uniqueness correctly
