skip to Main Content

I have these locals

vpc_peering = {
  require_dms = 1
  vpc = [
    {
      id     = "vpc-ai"
      owner  = "a"
      cidr   = "10.25.0.0/16"
      name   = "m-development"
      region = "ap-southeast-2"
    }
 ]} 
route_table_ids = [1, 2]
}

And want to combine them to produce an object/map with this format

vpc_peering_routes = {
  1-vpc-ai = {
    id     = "vpc-ai"
    owner  = "a"
    cidr   = "10.25.0.0/16"
    name   = "m-development"
    region = "ap-southeast-2"
    route_table_id = 1
  }
  2-vpc-ai = {
    id     = "vpc-ai"
    owner  = "a"
    cidr   = "10.25.0.0/16"
    name   = "m-development"
    region = "ap-southeast-2"
    route_table_id = 2
  }
}

So that I can use it for adding all cidrs to the aws_route

resource "aws_route" "vpc_peering_private_routes" {
  for_each = local.vpc_peering_routes
   
  route_table_id            = each.value.route_table_id
  destination_cidr_block = each.value.cidr
  vpc_peering_connection_id = aws_vpc_peering_connection.vpc_peering.id
}

I have tried different merge and maps but I always end up with a nested map

Any examples would be great 🙂

EDIT found the solution

route_table_cidr_combination = setproduct(local.route_table_ids, local.vpc_peering.vpc)
vpc_peering_cidr_routes = {
   for item in local.route_table_cidr_combination :  "${item[0]}-${item[1].id}" => merge(item[1], {route_table_id = item[0]})
}

2

Answers


  1. Since I tested with local variables, here’s a solution I have come up with:

    locals {
      vpc_peering = {
        require_dms = 1
        vpc = [
          {
            id     = "vpc-ai"
            owner  = "a"
            cidr   = "10.25.0.0/16"
            name   = "m-development"
            region = "ap-southeast-2"
          }
      ] }
    
      route_table_ids = [1, 2]
    
      vpc_peering_routes = merge(flatten([
        for k, v in local.vpc_peering.vpc[*] : [
          for i in local.route_table_ids : {
            "${i}-${v.id}" = {
              "id"             = "${v.id}"
              "owner"          = "${v.owner}"
              "name"           = "${v.name}"
              "region"         = "${v.region}"
              "route_table_id" = "${i}"
            }
          }
        ]
      ])...)
    }
    

    This will give:

    > local.vpc_peering_routes
    {
      "1-vpc-ai" = {
        "id" = "vpc-ai"
        "name" = "m-development"
        "owner" = "a"
        "region" = "ap-southeast-2"
        "route_table_id" = 1
      }
      "2-vpc-ai" = {
        "id" = "vpc-ai"
        "name" = "m-development"
        "owner" = "a"
        "region" = "ap-southeast-2"
        "route_table_id" = 2
      }
    }
    

    The solution is using merge and flatten built-in functions as well as the ellipsis operator (...), so that is not a mistake.

    Login or Signup to reply.
  2. You may use the following code:

    vpc_peering_routes = merge([
        for r in local.route_table_ids :
        {
          for k, v in local.vpc_peering.vpc : "${r}-${v.id}" => merge(v, { route_table_id = r })
        }
      ]...)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search