A platform team's requirement says the environment input of a shared module may only be dev, staging or prod, and a mistyped value must stop the run before Terraform produces any plan. What should the module author add?
Choose one.
Input variable validation encodes allowed values for an input and is checked before Terraform builds a plan.
The requirement has two parts: only three allowed values, and failure before planning. A validation block on the variable checks the value as soon as Terraform evaluates the input, before planning, and errors with your error_message. A precondition would also error, but it is checked later, while the plan is built, and must be copied onto every resource; a check block only warns; a type constraint cannot list allowed values.
- Identify where the rule belongs: on the input itself.
- Note the timing requirement: before a plan exists.
- Recall that validation errors before planning while check blocks only warn.
- Reject type constraints, which cannot express an allowed-value list.
Exam tip: Allowed values for an input belong in a validation block on that variable.
Terraform Professional (002): Turning Requirements into Predictable, Secure Configuration — the lesson that teaches this.