0
0
Terraformcloud~10 mins

Variable validation rules in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Variable validation rules
Define variable with validation
Input value provided
Check validation rule
Accept value
Use variable
Terraform checks the variable's input against validation rules. If valid, it proceeds; if invalid, it stops with an error.
Execution Sample
Terraform
variable "env" {
  type = string
  validation {
    condition     = contains(["dev", "prod"], var.env)
    error_message = "env must be 'dev' or 'prod'"
  }
}
Defines a variable 'env' that only accepts 'dev' or 'prod' values.
Process Table
StepInput ValueValidation ConditionResultAction
1"dev"contains(["dev", "prod"], "dev")TrueAccept value, continue deployment
2"test"contains(["dev", "prod"], "test")FalseShow error: env must be 'dev' or 'prod', stop deployment
💡 Execution stops if validation condition is False, preventing invalid variable usage.
Status Tracker
VariableStartAfter Step 1After Step 2
var.envundefined"dev"Error - invalid value, deployment halted
Key Moments - 2 Insights
Why does Terraform stop deployment when the variable value is invalid?
Terraform enforces validation rules strictly. As shown in execution_table step 2, when the condition is false, it shows the error message and stops to prevent misconfiguration.
Can the validation condition use complex expressions?
Yes, the condition can use any expression that returns true or false. It runs at plan/apply time to check the input value, as seen in the condition field in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens when the input value is "dev"?
ATerraform stops deployment with an error
BTerraform ignores the validation
CTerraform accepts the value and continues
DTerraform changes the value automatically
💡 Hint
Refer to execution_table row 1 where the condition is true and action is to accept value.
At which step does the validation condition become false?
AStep 2
BStep 1
CNever
DBoth steps
💡 Hint
Check execution_table row 2 where input "test" fails the condition.
If we add "stage" to the allowed list in the condition, how would step 2 change?
AStep 2 would pass validation if input is "stage"
BStep 2 would still fail validation
CStep 2 would cause a syntax error
DStep 2 would be skipped
💡 Hint
Step 2 input is "test". Adding "stage" to ["dev", "prod"] results in ["dev", "prod", "stage"], which still does not contain "test", so it fails.
Concept Snapshot
Terraform variable validation rules:
- Define validation block inside variable
- Use condition expression returning true/false
- Provide error_message for invalid input
- Deployment stops if validation fails
- Helps catch config errors early
Full Transcript
Terraform variable validation rules let you check if a variable's input value meets certain conditions before deployment proceeds. You define a validation block inside the variable with a condition expression that returns true or false. If the input value passes the condition, Terraform accepts it and continues deployment. If it fails, Terraform shows the error message and stops deployment to prevent misconfiguration. This ensures only valid values are used, catching errors early. The validation condition can be any expression, such as checking if the value is in a list of allowed strings.