Skill 4.7.1

Input variable validation

Key Concepts

Each validation block has a condition, an expression that must be true for a valid value, and an error_message. For example, condition = contains(["dev", "prod"], var.environment). A variable can have several validation blocks, and functions such as can() and regex() are often used, as in can(regex("^ami-", var.ami_id)).

A failed validation is an error that stops Terraform before planning, so bad input never reaches the provider. Since Terraform 1.9, conditions can also refer to other variables and objects in the module, not only the variable being validated.