Error: Invalid value for variable: input failed the variable validation block
Fixes Terraform's "Error: Invalid value for variable ..." when a caller-supplied value fails a variable's validation block. Use when plan rejects an input value with your custom error message. Fix the input or relax the condition; not for bad defaults ("Invalid default value for variable").
TL;DR
The value you passed failed the variable's own validation block. The error message is the custom error_message from the config, so it tells you exactly what is wrong. Pass a value that satisfies the condition, or fix the condition.
The error
Error: Invalid value for variable
Environment must be dev, staging, or production.Steps to fix
- Read the message after the header: it is the
error_messagefrom the variable'svalidationblock (here: must be dev, staging, or production).
- Expected: you know the allowed values.
- Find where you set the value:
-varflag,.tfvarsfile, or environment variableTF_VAR_[name].
- Expected: you locate the offending input (e.g.
-var="environment=prod").
- Either pass an allowed value, or if the validation is wrong, update the
conditioninvariables.tf.
- Expected: the value satisfies the condition.
- Re-run
terraform plan.
- Expected: validation passes and planning continues.
When to use this
plan/applyfails withInvalid value for variableplus a human-written message, after someone added avalidationblock or changed inputs.
When NOT to use this
Invalid default value for variableis about the default in the config, not your input.Value for undeclared variablemeans the variable does not exist at the root at all.
Compatibility
- Variable validation blocks: Terraform 0.13+. Behavior stable across 1.x.
Root cause
validation blocks are assertions the module author writes about acceptable inputs. Terraform evaluates the condition against the final value (default overridden by caller input); a false result fails the run with the author's message before any planning.
Edge cases
- The condition can reference other variables, but not resources or data sources.
nullablevariables: validation is skipped when the value is null unless the condition handles it.- Passing the variable at the wrong level (module instead of root) gives
Value for undeclared variable, a different error with a different fix.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.