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?
✗ Incorrect
Validation rules are defined inside the variable block using the validation block.
What does Terraform do if a variable fails its validation rule?
✗ 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'?✗ Incorrect
All listed expressions correctly check if var.env is one of the allowed values.
What is the purpose of the
error_message in a validation block?✗ Incorrect
The error_message explains why the validation failed to help users fix the input.
Can validation rules in Terraform use functions like
length() or regex()?✗ Incorrect
Validation conditions can use any valid Terraform expressions and functions.
Explain how to add a validation rule to a Terraform variable and why it is useful.
Think about checking inputs before applying changes.
You got /6 concepts.
Describe what happens when a Terraform variable value does not meet its validation condition during a plan or apply.
Consider how Terraform protects your infrastructure.
You got /4 concepts.