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
Terraform Variable Validation Rules
📖 Scenario: You are setting up a Terraform configuration to deploy cloud resources. To avoid mistakes, you want to add rules that check if the input variables meet certain conditions before applying the configuration.
🎯 Goal: Create a Terraform variable with validation rules that ensure the input is a string with a length between 3 and 10 characters.
📋 What You'll Learn
Define a variable named environment of type string
Add a validation block that checks the length of environment is at least 3 characters
Add a validation block that checks the length of environment is at most 10 characters
Provide a custom error message if the validation fails
💡 Why This Matters
🌍 Real World
Terraform variable validation helps prevent mistakes by checking user inputs before deploying cloud infrastructure, saving time and avoiding errors.
💼 Career
Cloud engineers and DevOps professionals use variable validation to create reliable and user-friendly Terraform modules and configurations.
Progress0 / 4 steps
1
Create the environment variable
Create a Terraform variable named environment of type string.
Terraform
Hint
Use the variable block with the name environment and set type = string.
2
Add minimum length validation
Add a validation block inside the environment variable that checks if the length of var.environment is at least 3 characters using length(var.environment) >= 3.
Terraform
Hint
Use a validation block with condition and error_message inside the variable.
3
Add maximum length validation
Add another validation block inside the environment variable that checks if the length of var.environment is at most 10 characters using length(var.environment) <= 10. Keep the previous validation block as well.
Terraform
Hint
Add a second validation block with the maximum length condition and error message.
4
Complete the variable with both validations
Ensure the environment variable block includes both validation blocks for minimum and maximum length with their respective error messages.
Terraform
Hint
Review the entire variable block to confirm both validations are present.
Practice
(1/5)
1. What is the main purpose of variable validation rules in Terraform?
easy
A. To speed up the Terraform apply process
B. To automatically fix errors in the Terraform code
C. To create new variables dynamically during runtime
D. To check if input values meet specific conditions before applying configuration
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 D
Quick Check:
Validation checks inputs = C [OK]
Hint: Validation rules check inputs before use to avoid errors [OK]
Common Mistakes:
Thinking validation fixes errors automatically
Confusing validation with variable creation
Assuming validation speeds up apply
2. Which of the following is the correct syntax to add a validation rule for a variable in Terraform?
easy
A. variable "name" { validation { condition = length(var.name) > 3 error_message = "Name too short" } }
B. variable "name" { validate { condition = length(var.name) > 3 message = "Name too short" } }
C. variable "name" { validation_rule { check = length(var.name) > 3 error = "Name too short" } }
D. variable "name" { validate_rule { condition = length(var.name) > 3 error_message = "Name too short" } }
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]