Nested optional() vars question

Greetings,

I have this

variable "druid_cluster" {
  description = "Druid cluster"
  type        = map(map(object({
    instance_type = string
    private_ip = string
    root_block_device = optional(list(map(object({
      encrypted = bool
      volume_type = string
      volume_size = number
      tags  = list(string)
    }))), [{
      encrypted   = true
      volume_type = "gp2"
      volume_size = 512
      tags = { some = "tag"}
    }])
  })))
  default = {}
}

I’m trying to get it work. Getting random errors like

This default value is not compatible with the attribute's type constraint: element 0: element "encrypted": object required.

or

This default value is not compatible with the attribute's type constraint: element 0: element "tags": attributes "encrypted", "tags", "volume_size", and "volume_type" are required.

The format of root_block_device is the following

root_block_device = [
    {
      encrypted   = true
      volume_type = "gp3"
      throughput  = 200
      volume_size = 50
      tags = {
        Name = "my-root-block"
      }
    },
  ]

I’m just trying implement defaults for root_block_device

They are not random errors, they’re pointing out your default doesn’t match the type constraint you’re claiming it should - you’ve specified:

but then as a default you’ve put:

There are two issues:

  • there’s no map here at all, at the top level - so remove the map(...) from the type constraint
  • Your tags are of type map(string) not list(string), so update the type constraint accordingly
1 Like

The errors aren’t so random though.

You defined your map as having all those elements. Any map (or nested map) elements that are not optional themselves must be declared so your default value must be something like:

  default = {
    instance_type = "blah"
    private_ip = "bleh"
    root_block_device = [{
      encrypted   = true
      volume_type = "gp2"
      volume_size = 512
      tags = { some = "tag"}
    }]
  }

And that list shouldn’t be in the type constraint declaration. I also think you have one map too many in map(map(object({

1 Like

Thanks guys, appreciate your help!

1 Like