Variable definition for a map of lists of objects of strings

What’s the proper type constraint for a variable definition if the variable needs to look like this?

"keys": {
  "vendor_1": [
    {
      "public_key": "foo",
      "private_key": "bar"
    }
  ]
}

keys should be a map with zero or more vendors, and each vendor should be a list with one or more objects, and each object should represent two key/value pairs.

I’m looking for how I should set the type here:

variable "keys" {
  type    = THIS_PART_IS_WHAT_IM_LOOKING_FOR
  default = {}
}

Thanks.

Hi @jrobison-sb :smiley:

Try this:

variable "keys" {
  type    =  map(list(object({ public_key=string, private_key=string })))
  default = {}
}

Objects is used when do you need to define a “contract” of the inner thing…

I used this documentation to help me

Works great. Thanks @claytonsilva

1 Like

One thing to note is that, as things currently stand, objects may silently allow variables which aren’t defined, which is intended, but not always what people expect.

So another option, if you want to defend against typos etc, is to use map(any) or list(map(any)) or whatever, and do custom validation in the variables – for reasons described in that issue, custom validation doesn’t work as you’d expect with an object definition in place. That said, the more complex / nested the object, the trickier that validation is to write and maintain.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.