validation block
A block inside a variable that states a condition the value must meet and the error message to show when it does not.
In more detail
Validation runs before anything is planned, so a bad value stops the run with your own clear message instead of producing wrongly named resources. The condition is any expression that returns true or false.
Example
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "The environment must be dev, staging or prod."
}Related terms
- input variable — A named, typed value a configuration or module takes from outside, declared in a variable block and read as var.<name>.
- built-in function — One of Terraform's fixed set of functions, such as lower(), join() or jsonencode(), that transform values inside expressions.
Taught in
- Parameterising With Input Variables — Infrastructure as Code with Terraform
- Input Variables, Outputs, And Which Value Wins — HashiCorp Terraform Associate (004) Exam Prep
- Custom Conditions: Validation, Pre And Postconditions, Checks — HashiCorp Terraform Associate (004) Exam Prep
- The Full Mock: Sixty Minutes, Eight Areas, One Plan For The Last Week — HashiCorp Terraform Associate (004) Exam Prep