0
0
Terraformcloud~5 mins

Variable validation rules in Terraform - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
Which of these is a valid validation condition for a string variable env to allow only 'dev', 'test', or 'prod'?
Acontains(["dev", "test", "prod"], var.env)
Bvar.env == "dev" || var.env == "test" || var.env == "prod"
Cvar.env in ["dev", "test", "prod"]
DAll of the above
What is the purpose of the error_message in a validation block?
ATo describe the validation rule to users when it fails
BTo fix the variable automatically
CTo log the variable value
DTo set a default value
Can validation rules in Terraform use functions like length() or regex()?
ANo, only simple comparisons are allowed
BYes, any Terraform expression is allowed
COnly regex is allowed
DOnly length() is allowed
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.