Error: Invalid default value for variable: default does not match the type constraint
Fixes Terraform's "Error: Invalid default value for variable ... not compatible with the variable's type constraint", where a variable's default does not match its declared type. Use when validate fails on a variable default. Not for caller-supplied value failures ("Invalid value for variable").
TL;DR
The default in your variable block does not satisfy the type you declared. Terraform checks this at validate time, before any plan. Change the default to a value of the declared type (or loosen the type), then terraform validate.
The error
Error: Invalid default value for variable
on variables.tf line 5, in variable "tags":
5: default = "none"
This default value is not compatible with the variable's type constraint:
map of string required.Steps to fix
- Open the variable block the error names. Read the
typeline and thedefaultline together.
- Expected: you see the mismatch (string default vs
map(string)type).
- Decide which side is wrong. If callers pass maps, fix the default:
default = {}. If the variable really is a string, fix the type:type = string.
- Expected: default is a valid value of the declared type.
- Run
terraform validate.
- Expected:
Success! The configuration is valid.
When to use this
terraform validatefails withInvalid default value for variableright after you added or edited a variable's type or default.
When NOT to use this
Invalid value for variable(without "default") means a caller-supplied value failed validation, not the default.Invalid function argumentis a function call problem.
Compatibility
- Terraform 0.12+ type system; the check runs in
validate,plan, andapplyacross all 1.x versions.
Root cause
Variable defaults are validated against the type constraint when the configuration loads, independent of any input. A default that cannot convert to the declared type is a configuration bug, so Terraform fails fast instead of waiting for someone to omit the variable.
Edge cases
default = nullis allowed for any type unlessnullable = falseis set.- An empty tuple
[]is not a validmap(string); use{}. anyas the type skips this check entirely, but you lose validation for caller-supplied values too.
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.