Transit Gateway Route in Route Table

Hello,

I’m using transit gateway route for the route table. The code is like this


resource "aws_route_table" "private" {
  count = var.create_vpc && local.max_subnet_length > 0 ? local.nat_gateway_count : 0

  vpc_id = local.vpc_id
  provider = aws.dst
  depends_on = [null_resource.dependency]

  route {
     cidr_block         = var.tgw-route-cidr-1
     transit_gateway_id = var.transit_gateway_id
  }
  route {
    cidr_block         = var.tgw-route-cidr-2
    transit_gateway_id = var.transit_gateway_id
  }
  route {
    cidr_block         = var.tgw-route-cidr-3
    transit_gateway_id = var.transit_gateway_id
  }
  route {
    cidr_block         = var.tgw-route-cidr-4
    transit_gateway_id = var.transit_gateway_id
  }
  route {
    cidr_block         = var.tgw-route-cidr-5
    transit_gateway_id = var.transit_gateway_id
  }
  route {
    cidr_block         = var.tgw-route-cidr-6
    transit_gateway_id = var.transit_gateway_id
  }
  route {
    cidr_block         = var.tgw-route-cidr-7
    transit_gateway_id = var.transit_gateway_id
  }
  route {
    cidr_block         = var.tgw-route-cidr-8
    transit_gateway_id = var.transit_gateway_id
  }


  #depends_on = [var.transit_gateway_id]
  tags = merge(
    {
      "Name" = var.single_nat_gateway ? "${var.name}-${var.private_subnet_suffix}" : format(
        "%s-${var.private_subnet_suffix}-%s",
        var.name,
        element(var.azs, count.index),
      )
    },
    var.tags,
    var.private_route_table_tags,
  )
}

But this is not the best practice. When I remove or add one vpc, I need to change the code from the module.

I also tried from aws_route like this

resource "aws_route" "public-transit-gaetway" {
  count = length(var.public-tgw-route-cidr)
  route_table_id         = aws_route_table.public[0].id
  destination_cidr_block = var.public-tgw-route-cidr-1[count.index]
  gateway_id             = var.transit_gateway_id
  //depends_on           = [null_resource.public_rt_id]

  provider = aws.dst
}

resource "aws_route" "private-transit-gaetway" {
  count = length(var.private-tgw-route-cidr)
  route_table_id         = aws_route_table.private.*.id[count.index]
  destination_cidr_block = var.private-tgw-route-cidr[count.index]
  gateway_id             = var.transit_gateway_id
  //depends_on           = [null_resource.private_rt_id]

  provider = aws.dst
}

When I put the value of destrination_cidr_block in list, it only picks up the first cidr and ignores the other.

I think also tried by using csv file, the destination_cidr_block for the transit gateway route in route table are different as per the vpc. As the route table in VPC doesn’t takes the cidr_block of its own vpc, when we put the list of destination_cidr_block in csv file, it tries to put the same cidr in route table of all the vpcs. This will give error.

Please help me do with another and best approach which can be reused and non-hardcoded.