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 a variable validation block in Terraform?
A variable validation block checks if the input value for a variable meets certain rules before applying the configuration. It helps catch errors early by enforcing constraints.
Click to reveal answer
beginner
How do you define a validation block inside a Terraform variable?
Inside the variable block, you add a validation block with a condition expression and an error message. For example:
variable "age" { type = number validation { condition = age >= 18 error_message = "Age must be 18 or older." } }
Click to reveal answer
beginner
What happens if a variable value does not meet the validation condition?
Terraform stops the plan or apply process and shows the error message defined in the validation block. This prevents invalid configurations from being deployed.
Click to reveal answer
intermediate
Can validation blocks use complex expressions?
Yes, validation conditions can use any valid Terraform expression, including functions, comparisons, and logical operators to create complex rules.
Click to reveal answer
intermediate
Why is using variable validation blocks considered a best practice?
They improve reliability by catching invalid inputs early, reduce debugging time, and make configurations easier to understand and maintain.
Click to reveal answer
What keyword starts a validation block inside a Terraform variable?
Avalidation
Bcheck
Cassert
Dverify
✗ Incorrect
The correct keyword to define a validation block inside a variable is 'validation'.
What must a validation block always include?
Adescription and default
Btype and default
Ccondition and error_message
Dcondition and default
✗ Incorrect
A validation block requires a 'condition' expression and an 'error_message' to show if the condition fails.
If a variable fails validation, what does Terraform do?
AIgnores the error and continues
BStops and shows the error message
CUses the default value automatically
DLogs a warning but applies anyway
✗ Incorrect
Terraform stops the process and shows the error message to prevent invalid inputs.
Can you use functions inside a validation condition?
ANo, only simple comparisons are allowed
BOnly string functions are allowed
COnly arithmetic functions are allowed
DYes, any valid Terraform expression is allowed
✗ Incorrect
Validation conditions can use any valid Terraform expression, including functions.
Which of these is a good reason to use variable validation blocks?
ATo catch invalid inputs early
BTo speed up Terraform apply
CTo automatically fix errors
DTo reduce variable declarations
✗ Incorrect
Validation blocks help catch invalid inputs early, improving reliability.
Explain how to create a variable validation block in Terraform and what it does.
Think about how you check if a value is allowed before using it.
You got /5 concepts.
Why should you use variable validation blocks in your Terraform configurations?
Consider how catching mistakes early helps in real life.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a validation block inside a Terraform variable?
easy
A. To check if the input value meets specific rules before applying the configuration
B. To assign a default value to the variable
C. To declare the variable type
D. To output the variable value after deployment
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 A
Quick Check:
Validation block purpose = input checking [OK]
Hint: Validation blocks check inputs before use [OK]
Common Mistakes:
Confusing validation with default value assignment
Thinking validation outputs variable values
Mixing validation with type declaration
2. Which of the following is the correct syntax to add a validation block inside a Terraform variable?
easy
A. variable "example" { validate { condition = length(var.example) > 0 error = "Must not be empty" } }
B. variable "example" { validation { condition = length(var.example) > 0 error_message = "Must not be empty" } }
C. variable "example" { validation { check = length(var.example) > 0 message = "Must not be empty" } }
D. variable "example" { validation { condition = length(example) > 0 error_message = "Must not be empty" } }
Solution
Step 1: Identify correct block and attribute names
The correct block is validation with attributes condition and error_message.
Step 2: Check variable references and syntax
Inside the condition, use var.example to 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 B
Quick Check:
Validation syntax = correct block and attributes [OK]
Hint: Use 'validation' block with 'condition' and 'error_message' [OK]
Common Mistakes:
Using 'validate' instead of 'validation'
Using wrong attribute names like 'check' or 'error'
Referencing variable without 'var.' prefix
3. Given this variable declaration:
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?
medium
A. Terraform will apply successfully with port 80
B. Terraform will prompt to enter a valid port
C. Terraform will ignore the validation and use default port
D. Terraform will fail with error: Port must be between 1024 and 65535
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 from error_message.
Final Answer:
Terraform will fail with error: Port must be between 1024 and 65535 -> Option D
Quick Check:
Validation fails = error message shown [OK]
Hint: Validation blocks stop apply if condition is false [OK]
Common Mistakes:
Assuming Terraform applies anyway
Thinking default values are used automatically
Expecting interactive prompts for invalid input
4. Identify the error in this variable validation block:
variable "env" {
type = string
validation {
condition = var.env == "dev" || "prod"
error_message = "env must be 'dev' or 'prod'"
}
}
medium
A. Validation blocks cannot use logical OR operators
B. The error_message attribute is misspelled
C. The condition syntax is incorrect; it should compare both values explicitly
D. The variable type should be list, not string
Solution
Step 1: Review the condition expression
The condition var.env == "dev" || "prod" is invalid because "prod" alone is always true.
Step 2: Correct the condition syntax
It should be var.env == "dev" || var.env == "prod" to compare both values explicitly.
Final Answer:
The condition syntax is incorrect; it should compare both values explicitly -> Option C
Quick Check:
Logical OR needs full comparisons [OK]
Hint: Use full comparisons on both sides of OR [OK]
Common Mistakes:
Writing incomplete logical expressions
Assuming string alone works as condition
Confusing error_message spelling
5. You want to validate a list variable users so it must have at least 2 unique names and none can be empty strings. Which validation block correctly enforces this?
hard
A. 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" }
B. validation { condition = length(var.users) > 2 && distinct(var.users) != [] error_message = "Users must have 2+ unique names" }
C. validation { condition = length(var.users) >= 2 && var.users != [""] error_message = "Users must not be empty" }
D. validation { condition = length(var.users) >= 2 && length(var.users) == length(distinct(var.users)) error_message = "Users must have unique names" }
Solution
Step 1: Check list length and uniqueness
Condition requires at least 2 items and all must be unique, so length(var.users) >= 2 and length(distinct(var.users)) == length(var.users) ensure this.
Step 2: Ensure no empty strings
The alltrue([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 A
Quick 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]
Hint: Combine length, distinct, and alltrue for list validation [OK]