Is it possible to reference list elements that are stored in a map?

Is it possible to reference list elements that are stored in a map?

I’m currently using a list:

assigned_private_ips = [
  "10.20.2.132",
  "10.20.2.133",
  "10.20.2.134",
  "10.20.2.135",
  "10.20.2.136",
  "10.20.2.137"
]

– and referencing elements as:

  private_ip = ["${var.assigned_private_ips[0]}"]

– but I would like to define the values in a map, eg:

assigned_private_ips = {
  "server1" = ["10.20.2.132","10.20.2.133","10.20.2.134"]
  "server2" = ["10.20.2.135","10.20.2.136","10.20.2.137"]
}

– And reference the list elements for a specific key; ie, something along the lines of:

"${var.assigned_private_ips["server1"][0]}"

Is this possible in Terraform?

Thank you!

Hi @alansigudo,

The expression you gave should work as you expected in Terraform 0.12:

private_ip = var.assigned_private_ips["server1"][0]

Terraform 0.11 and earlier do not fully support nested collection types.

1 Like