Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of variable validation rules in Terraform?
Variable validation rules check if the input values meet certain conditions before applying changes. They help catch errors early and ensure correct configurations.
Click to reveal answer
beginner
How do you define a validation rule for a variable in Terraform?
Inside the variable block, use the validation block with a condition expression and an error_message to explain the rule.
Click to reveal answer
intermediate
Example: Write a validation rule that ensures a variable port is between 1024 and 65535.
validation {
condition = var.port >= 1024 && var.port <= 65535
error_message = "Port must be between 1024 and 65535."
}
Click to reveal answer
beginner
What happens if a variable value does not meet the validation condition in Terraform?
Terraform stops the run and shows the error message defined in the validation block. This prevents applying invalid configurations.
Click to reveal answer
intermediate
Can validation rules use complex expressions or functions in Terraform?
Yes, validation conditions can use any valid Terraform expressions and functions to create flexible and powerful checks.
Click to reveal answer
Where do you place the validation rules for a variable in Terraform?
AInside the variable block
BIn the provider block
CIn the output block
DIn the terraform block
✗ Incorrect
Validation rules are defined inside the variable block using the validation block.
What does Terraform do if a variable fails its validation rule?
AAutomatically fixes the variable
BIgnores the error and continues
CShows an error and stops the run
DLogs a warning but applies changes
✗ Incorrect
Terraform stops execution and shows the error message to prevent invalid configurations.
Which of these is a valid validation condition for a string variable env to allow only 'dev', 'test', or 'prod'?
4. Identify the error in this variable validation block:
variable "port" {
type = number
validation {
condition = var.port > 0 && var.port < 65536
error_message = "Port must be between 1 and 65535"
}
}
medium
A. Using 'number' type instead of 'number' is invalid
B. Validation condition uses 'var.port' instead of 'self'
C. Error message is missing a period at the end
D. Validation block must be outside the variable block
Solution
Step 1: Understand validation condition context
Inside validation, use self to refer to the variable value, not var.port.
Step 2: Identify incorrect usage
The condition incorrectly uses var.port, which is not best practice.
Final Answer:
Validation condition uses 'var.port' instead of 'self' -> Option B
Quick Check:
Use 'self' in validation condition = B [OK]
Hint: Use 'self' to refer to variable value inside validation [OK]
Common Mistakes:
Using 'var.variable_name' inside validation condition
Placing validation block outside variable block
Ignoring syntax errors in condition
5. You want to validate a list variable so it only accepts lists with exactly 3 strings, each at least 2 characters long. Which validation condition is correct?
hard
A. condition = length(self) == 3 && all([for s in self : length(s) >= 2])
B. condition = length(self) == 3 && all([for s in self : s > 2])
C. condition = length(self) == 3 && alltrue([for s in self : s >= 2])
D. condition = length(self) == 3 && alltrue([for s in self : length(s) >= 2])
Solution
Step 1: Check list length condition
We want exactly 3 items, so length(self) == 3 is correct.
Step 2: Validate each string length
Use all() to ensure all elements satisfy length(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 A
Quick Check:
Use 'all' with length checks and list length = D [OK]
Hint: Use 'all' function and 'self' for list validation [OK]