Looping through a tuple list, for unique subnet_ids

After generating a data structure of the following, I was trying to use the following to generate a list of subnet_ids per vpc. I ended up calling data aws_subnets_ids (after creating my subnets, however I was wondering if there was a more eloquent way to work with the data that I created):

     {
         subnet_id   = "12345"
         subnet_name = "happydays-0"
         vpc_id      = "1"
      },
     {
         subnet_id   = "6789"
         subnet_name = "happydays-1"
         vpc_id      = "1"
      },
     {
         subnet_id   = "1010101"
         subnet_name = "happydays-2"
         vpc_id      = "1"
      },
     {
         subnet_id   = "123123"
         subnet_name = "monday-3"
         vpc_id      = "2"
      },
     {
         subnet_id   = "!2321"
         subnet_name = "monday-4"
         vpc_id      = "2"
      },
     {
         subnet_id   = "123124"
         subnet_name = "monday-5"
         vpc_id      = "2"
      },
  ]

I tried making a map based on each vpc_id, but then looping through the list and creating the new data structure left me lost.

I was trying to end up with some structure like the following:

vpc_list = {
  1 =  ​[ "12345", "6789", "1010101"]

​2 =[" "123123", "123124"]
}

Thanks for the help

Hi @smith.jamiej,

What I understood from your question is that you want to create a map from VPC IDs to lists of subnet IDs that belong to each VPC.

Assuming I understood that correctly, I’d try to solve that using a for expression, using the ... modifier to group results by map key:

locals {
  subnets_by_vpc = {
    for obj in var.subnets : obj.vpc_id => obj.subnet_id...
  }
}

I tried something similar, but then I have duplicate ids:

output "vpc-subnets" {
    value = {
        for i in local.subnets:
          i.vpc_id => i.subnet_id
        }
    }

What I was hoping it that I could have a list of vpc_id’s (as map keys) then a list of all the subnets that are in each vpc, does that make sense?

Error: Duplicate object key

  on main.tf line 89, in output "vpc-subnets":
  87:     value = {
  88:         for i in local.subnets:
  89:           i.vpc_id => i.subnet_id
  90:         }
    |----------------
    | i.vpc_id is "2"

Two different items produced the key "2" in this 'for' expression. If
duplicates are expected, use the ellipsis (...) after the value expression to
enable grouping by key.

Hi @smith.jamiej,

That is what the ... sequence in my example should achieve. In effect, my example is the result of following the suggestion given in that error message.

I understand now, thank you for your help (I mis-understand what you meant) I grew up on Terraform <= 11 :slightly_smiling_face:

I saw what you meant on this page:

Finally, the map form of a for expression has a grouping mode where the map key is used to group items together into a list for each distinct key. This mode is activated by placing an ellipsis (...) after the value expression:Finally, the map form of a for expression has a grouping mode where the map key is used to group items together into a list for each distinct key. This mode is activated by placing an ellipsis (...) after the value expression:

Thanks

Great! I’m glad it worked out.

I just want to note that the 0.12 preview blog posts are three years old now and will not be updated for future changes to the language, so to get the latest details it’s generally better to refer to the main documentation for for expressions, which also discuss that feature as follows:

Finally, if the result type is an object (using { and } delimiters) then the value result expression can be followed by the ... symbol to group together results that have a common key:

{for s in var.list : substr(s, 0, 1) => s... if s != ""}