Hash table in terraform

Hello,

Is-it possible to create an hash table containing an array in terraform ?

hash[“apache”] = [‘80’,‘443’]

Thanks.

Hi @smutel,

In the Terraform language type system we say “map” and “list” rather than “hash table” and “array”, but indeed you can have a map of lists of strings.

If you want to accept such a data structure as an input variable for a module then you can declare it with the type constraint map(list(string)):

variable "service_ports" {
  type = map(list(string))
}

You can write a value which conforms to that type constraint using { ... } and [ ... ] constructor expressions:

  service_ports = {
    apache = ["80", "443"]
  }

I defined this as a map(list(string)) because you showed the port numbers in quotes, but you can also potentially take them as numbers if you like, which would be written as map(list(number)).