Nested for loop with conditional

Conditionals in terraform are proving to be a thorn for me. I know that the following is not valid but it demonstrates what I’m trying to do (compare two maps and merge if they have a matching tag).

  route_table_subnet_map = {
    for route_table in aws_route_table.private : [
      for subnet in aws_subnet.private : {
        if subnet.tags["env"] == route_table.tags["env"] :  <---BROKEN
          merge(subnet, {route_table_id = route_table.id})
      }
    ]
  }
}

Edit: This is probably closer to right but still not functional

    for route_table in aws_route_table.private : [
      for subnet in aws_subnet.private : {
        merge(subnet, {route_table_id = route_table.id} ? subnet.tags["env"] == route_table.tags["env"] : false)
      }
    ]
  }

Please and thank you.

Is it just a broken question? Such quiet.

Hi @b1tbucket,

It’s not clear what exactly you are trying to do here. It would probably help if you could provide a sample of the data and the structure you want to create.

If you want to filter elements within a for expression, the if clause goes after the element. See filtering elements for some examples.