How to iterate a list of map which contains a list

locals {
  x = [
    {
      "group" = [
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-ng--dd2da01e-grp",
      ]
      "name" = "https"
      "node_pool_name" = "test-nodepool"
      "port" = 31313
      "zone" = "us-west1"
    },
    {
      "group" = [
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-poo-246fbb77-grp",
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-ng--38ea97a3-grp",
      ]
      "name" = "http"
      "node_pool_name" = "test-nodepool-1"
      "port" = 80
      "zone" = "us-west1"
    },
    {
      "group" = [
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-poo-246fbb77-grp",
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-ng--38ea97a3-grp",
      ]
      "name" = "https"
      "node_pool_name" = test-nodepool-1"
      "port" = 31313
      "zone" = "us-west1"
    },
  ]
}

I’m trying to use the value of local.x with the following google resource google_compute_instance_group_named_port

So I’m trying something like this:

resource "google_compute_instance_group_named_port" "this" {
  for_each = {
    for ig in local.instance_groups : "${ig.node_pool_name}-${ig.port}" => ig
  }
    zone = each.value.zone
    name = each.value.name
    port = each.value.port
    group = ""

}

As you can see, group is a list in my local.x variable but in the google resource the parameter group only takes a string and not a list so I think a dynamic block cannot be used in this case.
I haven’t been able to figure out how to handle a case like this.
If somebody can show me here how to solve this I would be glad!

To handle this you need multiple nested for expressions - I previously wrote an example of such in Multiple iteration in one resource - #2 by maxb - see if that helps?

Hello @maxb

This is how I solved it. However, I’m not sure if that would be the optimal solution but it works.

locals {
  x = [
    {
      "group" = [
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-ng--dd2da01e-grp",
      ]
      "name" = "https"
      "node_pool_name" = "test-nodepool"
      "port" = 31313
      "zone" = "us-west1"
    },
    {
      "group" = [
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-poo-246fbb77-grp",
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-ng--38ea97a3-grp",
      ]
      "name" = "http"
      "node_pool_name" = "test-nodepool-1"
      "port" = 80
      "zone" = "us-west1"
    },
    {
      "group" = [
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-poo-246fbb77-grp",
        "https://www.googleapis.com/compute/v1/projects/xxxxxxx/zones/us-west2-a/instanceGroups/gke-uat-failover-pri-uat-failover-ng--38ea97a3-grp",
      ]
      "name" = "https"
      "node_pool_name" = test-nodepool-1"
      "port" = 31313
      "zone" = "us-west1"
    },
  ]

  z = {for v in local.x : "${v.node_pool_name}-${v.port}" => v}

  final_ig = flatten([
    for k, v in local.z : [
      for group in v.group: {
        zone = v.zone
        name = v.name
        port = v.port
        node_pool_name = v.node_pool_name
        group = group

      }

    ]
  ])
}




resource "google_compute_instance_group_named_port" "this" {
  for_each = {
    for ig in local.final_ig : "${ig.node_pool_name}-${ig.port}" => ig
  }
    zone = each.value.zone
    name = each.value.name
    port = each.value.port
    group = each.value.group
}

Do you happen to know if there is a better way to get to the same result?

Thanks.