Terraform "Invalid value for variable" - Custom Validation Failed in CI
An input value did not satisfy a variable’s declared type or a custom validation block. Terraform stops because the value is outside what the configuration allows.
What this error means
plan/apply fails with "Invalid value for variable" and the custom error message from the validation block, or a type error. It surfaces when CI passes a tfvar/env value that breaks the variable’s constraints.
Error: Invalid value for variable
on variables.tf line 5:
5: variable "environment" {
The "environment" value must be one of: dev, staging, prod.
This was checked by the validation rule at variables.tf:8,3-13.Common causes
Value fails a custom validation rule
A validation block’s condition returned false for the supplied value, so Terraform rejects it with the block’s error_message.
Value is the wrong type
Passing a string where a number/bool/list is declared (common with env-var inputs that arrive as strings) fails the variable’s type constraint.
How to fix it
Supply a value that satisfies the rule
Read the validation message and pass an allowed value; for typed inputs, pass the correct type.
# the validation that rejected the input
variable "environment" {
type = string
validation {
condition = contains(["dev","staging","prod"], var.environment)
error_message = "The environment value must be one of: dev, staging, prod."
}
}Pass correctly-typed values from CI
- Confirm the tfvar/env value matches the variable’s declared type.
- For numbers/bools coming from env vars, ensure they are not quoted strings where a primitive is expected.
- Re-run with a value the validation allows.
How to prevent it
- Document allowed values in
error_messageso CI failures are self-explanatory. - Validate tfvars against the variable types before applying.
- Keep CI-provided inputs within the declared constraints.