terraform
terraform copied to clipboard
Strange behavior with optional type constraint and variable default values
Terraform Version
Terraform v1.3.3
on linux_amd64
Terraform Configuration Files
variable "input" {
type = list(object({
a = string # a required attribute
b = optional(string) # an optional attribute
c = optional(number, 127) # an optional attribute with default value
d = optional(list(any))
}))
default = [
{ a = "a" }, { a = "b", d = [1] }
]
}
output "out" {
value = var.input
}
Debug Output
https://gist.github.com/seggcsont/e7a04c37155e876c4868e9acdc46268b
Expected Behavior
Without variable override it should use default value
Actual Behavior
Error: Invalid default value for module argument
on 1.tf line 1: 1: variable "input" {
The default value for variable "input" is incompatible with its type constraint: element types must all match for conversion to list.
Steps to Reproduce
-
terraform init
-
terraform plan
- run command:
tf plan -var "input=[{a=1},{a=2, d=[]}]"
tf plan -var "input=[{a=1},{a=2, d=[1]}]"
tf plan -var "input=[{a=1},{a=2}]"
tf plan -var "input=[{a=1}]"
tf plan
Additional Context
It works if you apply one of these:
- remove the
1
from theb=[1]
so it will be an empty list - use optional default value
d = optional(list(any), [])
- change type
any
tonumber
liked = optional(list(number))
References
No response