0
0
Terraformcloud~10 mins

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

Choose your learning style9 modes available
Process Flow - Variable validation blocks
Define variable with validation block
Input value provided
Check validation condition
Accept value
Stop deployment
Terraform checks the variable's input against the validation condition. If it passes, deployment continues; if not, 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'"
  }
}
This variable 'env' only accepts 'dev' or 'prod'. If another value is given, Terraform shows an error and stops.
Process Table
StepInput ValueValidation ConditionCondition ResultAction
1"dev"contains(["dev", "prod"], "dev")trueAccept value, continue deployment
2"test"contains(["dev", "prod"], "test")falseError: env must be 'dev' or 'prod', stop deployment
💡 Deployment stops if validation condition is false to prevent invalid inputs.
Status Tracker
VariableStartAfter Step 1After Step 2
var.envundefined"dev" (valid)"test" (invalid, error)
Key Moments - 2 Insights
Why does Terraform stop deployment when the validation fails?
Terraform stops because the validation block's condition returned false (see execution_table step 2), which means the input does not meet the required rules.
Can the validation block change the variable value?
No, the validation block only checks the value. It does not modify it. If invalid, it stops deployment instead (see execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens when the input value is "dev"?
ADeployment stops with an error
BValue is changed to "prod" automatically
CValue is accepted and deployment continues
DTerraform ignores the validation block
💡 Hint
Check execution_table row 1 under 'Action' column
At which step does the validation condition become false?
AStep 1
BStep 2
CValidation never fails
DBefore step 1
💡 Hint
Look at execution_table 'Condition Result' column
If the validation condition was changed to allow "test", how would the execution table change?
AStep 2 would accept "test" and continue deployment
BStep 1 would fail instead
CBoth steps would stop deployment
DNo change in execution table
💡 Hint
Think about how condition result affects action in execution_table
Concept Snapshot
Terraform variable validation blocks check input values.
Syntax: validation { condition = <bool>; error_message = "msg" }
If condition is false, deployment stops with error.
Validation does not change values, only verifies them.
Use to enforce allowed inputs and prevent mistakes.
Full Transcript
Terraform variable validation blocks let you set rules for input values. When you run Terraform, it checks if the input meets the condition. If yes, deployment continues. If no, Terraform stops and shows the error message. This helps avoid mistakes by catching wrong inputs early. The validation block only checks values; it does not change them. For example, if you want a variable to be only 'dev' or 'prod', you write a condition to check that. If someone enters 'test', Terraform will stop and show an error. This process ensures your infrastructure is configured correctly before applying changes.