I have a scenario, where I would like to create three different route tables and each of these route tables will have their own unique routing rules. I am looking to achieve all of these in a single resource block.
Below is my main.tf file and the resource block:
resource "aws_route_table" "tgw_subnet_rt" {
count = local.json_data.create_tgw_subnets_rt ? local.json_data.no_of_tgw_subnet_rt : 0
vpc_id = module.vpc.vpc_id
tags = merge(
local.standard_tags,
map(
"Name", "${local.json_data.environment}_${local.json_data.vpc_type}_vpc_${local.json_data.tgw_subnet_type}_subnet_rt_0${count.index + 1}",
)
)
dynamic "route" {
for_each = {
**for route in local.network_routes : "${route.route_table_key}.${route.route_key}" => route }**
content {
cidr_block = lookup(route.value, "cidr_block", null)
destination_prefix_list_id = lookup(route.value, "destination_prefix_list_id", null)
ipv6_cidr_block = lookup(route.value, "ipv6_cidr_block", null)
carrier_gateway_id = lookup(route.value, "carrier_gateway_id", null)
egress_only_gateway_id = lookup(route.value, "egress_only_gateway_id", null)
gateway_id = lookup(route.value, "gateway_id", null)
instance_id = lookup(route.value, "instance_id", null)
nat_gateway_id = lookup(route.value, "nat_gateway_id", null)
local_gateway_id = lookup(route.value, "local_gateway_id", null)
network_interface_id = lookup(route.value, "network_interface_id", null)
transit_gateway_id = lookup(route.value, "transit_gateway_id", null)
vpc_endpoint_id = lookup(route.value, "vpc_endpoint_id", null)
vpc_peering_connection_id = lookup(route.value, "vpc_peering_connection_id", null)
}
}
}
locals {
network_routes = flatten([
for route_table_key, route_table in local.json_data.test_routes : [
for route_key, route in route_table.routes :{
route_table_key = route_table_key
route_key = route_key
cidr_block = route.cidr_block
gateway_id = route.gateway_id
}
]
])
}
and there is my .JSON file
“test_routes”: { “route-table1”:
{
“routes”:
[{
“cidr_block” : “X.X.X.X/X”,
“destination_prefix_list_id” : “”,
“ipv6_cidr_block”:"",
“carrier_gateway_id”: “”,
“egress_only_gateway_id”: “”,
“gateway_id”:"",
“instance_id”:"",
“nat_gateway_id”:"",
“local_gateway_id”:"",
“network_interface_id”:"",
“transit_gateway_id”: “XXXX”,
“vpc_endpoint_id”:"",
“vpc_peering_connection_id” : “”
},
{
“cidr_block” : “XXX”,
“destination_prefix_list_id” : “”,
“ipv6_cidr_block”:"",
“carrier_gateway_id”: “”,
“egress_only_gateway_id”: “”,
“gateway_id”:"",
“instance_id”:"",
“nat_gateway_id”:"",
“local_gateway_id”:"",
“network_interface_id”:"",
“transit_gateway_id”: “XXX”,
“vpc_endpoint_id”:"",
“vpc_peering_connection_id” : “”
}]
},
“route-table2”:
{
“routes”:
[{
“cidr_block” : “XXXX”,
“destination_prefix_list_id” : “”,
“ipv6_cidr_block”:"",
“carrier_gateway_id”: “”,
“egress_only_gateway_id”: “”,
“gateway_id”:"",
“instance_id”:"",
“nat_gateway_id”:"",
“local_gateway_id”:"",
“network_interface_id”:"",
“transit_gateway_id”: “XXXX”,
“vpc_endpoint_id”:"",
“vpc_peering_connection_id” : “”
},
{
“cidr_block” : “XXXX”,
“destination_prefix_list_id” : “”,
“ipv6_cidr_block”:"",
“carrier_gateway_id”: “”,
“egress_only_gateway_id”: “”,
“gateway_id”:"",
“instance_id”:"",
“nat_gateway_id”:"",
“local_gateway_id”:"",
“network_interface_id”:"",
“transit_gateway_id”: “XXXX”,
“vpc_endpoint_id”:"",
“vpc_peering_connection_id” : “”
}]
}
},
When I try and execute this code, the rules are adding to only Route Table, however, I would like to achieve the following:
Route Table -1 : Routing Table-1 Rule 1, Routing Table-1 Rule 2
Route Table -2 : Routing Table-2 Rule A, Routing Table-1 Rule B
So basically each route table will have their own unique routing rules. Can somone please help me, I am struck here and I am not sure how to proceed.