terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Strange behavior with optional type constraint and variable default values

Open seggcsont opened this issue 2 years ago • 0 comments

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

  1. terraform init
  2. terraform plan
  3. 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 the b=[1] so it will be an empty list
  • use optional default value d = optional(list(any), [])
  • change type any to number like d = optional(list(number))

References

No response

seggcsont avatar Oct 28 '22 13:10 seggcsont