Terraform: Variable of type map<string, list>

Created on 17 May 2016  ·  8Comments  ·  Source: hashicorp/terraform

I've just built Terraform from the master branch in order to play with list variables. I've noticed that it is not possible to create a variable of type map which maps string to list. For example, the following variable is invalid:

variable "availability_zones" {
  type = "map"

  default {
    ap-southeast-2 = ["ap-southeast-2a", "ap-southeast-2b", "ap-southeast-2c"]
    us-east-1      = ["us-east-1a", "us-east-1d", "us-east-1e"]
  }
}

The following error is reported:

Error loading Terraform: Error loading config: Error loading REDACTED/common_variables.tf: 'availability_zones' has a default value which is not of type 'map'

Is this expected or a bug?

question

Most helpful comment

I would second the request for this functionality.

All 8 comments

Hi @joshuaspence!

The new variable type support in 0.7 (master, for now) only supports strings, lists of strings, and maps from string to string. More complex nested structures are not supported yet.

Hi @joshuaspence! As @apparentlymart points out, for now it is only possible to have maps which are string -> string (as before). Internally there is some support for this, but it is not currently exposed and that is not planned for 0.7. We will circle back to this post-0.7, but for now I'll close this as "by design". Thanks for bringing it up!

Is there an issue that I can follow for supporting this type of variable?

I would second the request for this functionality.

I've ran into exactly the same issue (having conditional region-az map) today...

Workaround for not having list support in map could be done like this:

variable "region" {
  description = "AWS region"
  default = "us-east-1"
}
variable "availability_zones_in_ap_southeast_2" {
  description = "VPC-capable AZ's in ap-southeast-2 region"
  type = "list"
  default = [ "ap-southeast-2a", "ap-southeast-2b", "ap-southeast-2c" ]
}
variable "availability_zones_in_us_east_1" {
  description = "VPC-capable AZ's in us-east-1 region"
  type = "list"
  default = [ "us-east-1a", "us-east-1b" ,"us-east-1d" ]
}
variable "region_to_azs" {
  type = "map"
  default = {
    us-east-1 = "availability_zones_in_us_east_1"
    ap-southeast-2 = "availability_zones_in_ap_southeast_2"
  }

And this compiles fine without any single error.

_However_, in order to get that A-Z's list you'd need to do the following:

}
variable "availability_zones" {
  description = "choose proper AZ based on var.region variable"
  type = "list"
  default = ["${${lookup(var.region_to_azs,var.region)}}"]
}

which basically doesn't work 😞

Part of the set of changes that would make this (map of lists) possible is currently going on in hashicorp/hil#42. That addresses making the interpolation language be able to represent the element types of maps and lists so that it can be understood that indexing a map of lists produces a list.

There's still at least one follow up change required in Terraform to make variables accept such values, and likely more work at various layers in between to bring these things together.

This comment is mainly just to create a link between these issues to remind me to pay attention to this one later when posting follow-ups. :D

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ronnix picture ronnix  ·  3Comments

pawelsawicz picture pawelsawicz  ·  3Comments

rjinski picture rjinski  ·  3Comments

rkulagowski picture rkulagowski  ·  3Comments

rnowosielski picture rnowosielski  ·  3Comments