Setproduct() of individual chilld block of two nested list

Hi there,

I have two sets of lists - one with vpc endpoints per VPC:

> local.vpc_vpce_ids
[
  [
    "vpce-05",
    "vpce-08",
    "vpce-08",
  ],
  [
    "vpce-02",
    "vpce-04",
  ],
]

and another with subnet-ids per VPC:

> local.default_snet_ids
[
  [
    "subnet-0c",
    "subnet-03",
  ],
  [
    "subnet-06",
    "subnet-03",
  ],
]

How can I do a setproduct() of the individual nested block from those two sets?

Basically, I’m trying to do a VPCE subnet-associations to use it part of the for_each, where I have two make 2 associations (2x subnet) for each vpc-endpoints per VPC, so gonna be the total of 10 associations, as per the above example.

Any help would be appreciated!!

-S

Hi,

Doable with setproduct() but simpler with subnet_ids attribute if you can (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc_endpoint#subnet_ids)

Note that I made the vpc_vpce_ids first item list unique (vpce-08 appeared twice)

locals {
  vpc_vpce_ids = [
    [
      "vpce-05",
      "vpce-08",
      "vpce-09",
    ],
    [
      "vpce-02",
      "vpce-04",
    ],
  ]

  default_snet_ids = [
    [
      "subnet-0c",
      "subnet-03",
    ],
    [
      "subnet-06",
      "subnet-03",
    ],
  ]

  vpce_associations = concat([for i in range(length(local.vpc_vpce_ids)) : setproduct(local.vpc_vpce_ids[i], local.default_snet_ids[i])]...)
}

resource "aws_vpc_endpoint_subnet_association" "this" {
  count = length(local.vpce_associations)
  vpc_endpoint_id = element(local.vpce_associations, count.index)[0]
  subnet_id       = element(local.vpce_associations, count.index)[1]
}
1 Like

Thanks @ohmer!!
Totally forgot about that ... operator. I just changed your line a bit to make it a bit shorter:

vpce_associations = concat([
  for idx, key in local.vpc_vpce_ids : 
  setproduct(key, local.default_snet_ids[idx])
]...)

All good now. ty!!:+1:

-S

1 Like

I don’t find this thing very elegant while it does the job. I would to stay away from lists/concat and count and replace for maps/merge and for_each. I tend to always do that since 0.13.

You’re welcome :wink: