Creating new list from a map

Hello terraform newbie here. I have a map as such:

variable "region_list" {
  type = list(object({
    region      = string          
    azs         = list(string) 
  }))
  default = [
    {
      region      = "us-east-1"
      azs         = ["a", "b"]
    },
    {
      region      = "us-west-2"
      azs         = ["a", "b"]
    },
    {
      region      = "ap-southeast-1"
      azs         = ["a", "b"]
    },
    {
      region      = "ap-northeast-1"
      azs         = ["a", "c"]
    },
    {
      region      = "eu-central-1"
      azs         = ["a", "b"]
    },
    {
      region      = "eu-west-1"
      azs         = ["a", "b"]
    }
  ]
}

What syntax do i use in the locals block to create a new list of availability zones as such if possible:

new_az_list  = ["us-east1a", "us-east1b", "us-west-2a", "us-west-2b", "ap-southeast-1a","ap-southeast-1b","ap-northeast-1a","ap-northeast-1c", "eu-central-1a", "eu-central-1b", "eu-west-1a", "eu-west-1b"]

An aside, please don’t ping specific users when asking general questions. There are lots of people who can help here!

To get the output you’re looking for, try using a for expression. I’d recommend experimenting in terraform console until you find the right expression.

Here’s a starting point:

> [for r in var.region_list: r.region]
[
  "us-east-1",
  "us-west-2",
  "ap-southeast-1",
  "ap-northeast-1",
  "eu-central-1",
  "eu-west-1",
]

You can nest these for expressions, like so:

> [for r in var.region_list: [for az in r.azs: "az-${az}"]]
[
  [
    "az-a",
    "az-b",
  ],
  [
    "az-a",
    "az-b",
  ],
  [
    "az-a",
    "az-b",
  ],
  [
    "az-a",
    "az-c",
  ],
  [
    "az-a",
    "az-b",
  ],
  [
    "az-a",
    "az-b",
  ],
]

Here we’re using string interpolation to create a new value for each element of azs within a given region object.

Finally, since you want a single list, the flatten function will be useful. Adding to the previous expression:

> flatten([for r in var.region_list: [for az in r.azs: "az-${az}"]])
[
  "az-a",
  "az-b",
  "az-a",
  "az-b",
  "az-a",
  "az-b",
  "az-a",
  "az-c",
  "az-a",
  "az-b",
  "az-a",
  "az-b",
]

Hopefully with these tools you can figure out the next steps to transforming the data in the way you’re looking for. Let me know if you try something and get stuck, or if you find error messages unclear. Good luck!

Hello @alisdair, thanks a lot for the input. Would definitely give it a try. Thank you!