Hi everyone
I have json file with the following list of object (can change to map of object if is necessary):
"route_table_name": ["Route_table_Management" , "Route_table_Service"],
"route": [
{
"route_name" : "Service",
"address_prefix" : "172.16.0.0/16",
"next_hop_type": "VirtualAppliance",
"next_hop_in_ip_address": "172.16.13.8",
"rt_name": "Route_table_Service" -------> ROUTE TABLE NAME 1
},
{
"route_name" : "Internet",
"address_prefix" : "0.0.0.0/0",
"next_hop_type": "Internet",
"rt_name": "Route_table_Service" -------> ROUTE TABLE NAME 1
},
{
"route_name": "Management",
"address_prefix": "172.31.196.128/25",
"next_hop_type" : "VirtualAppliance",
"next_hop_in_ip_address" : "172.31.8.4",
"rt_name": "Route_table_Management" -----> ROUTE TABLE NAME 2
},
{
"route_name" : "Management_trust" ,
"address_prefix" : "172.31.128.0/19" ,
"next_hop_type": "VirtualAppliance",
"next_hop_in_ip_address" : "172.31.11.4",
"rt_name": "Route_table_Management" -------> ROUTE TABLE NAME 2
}
,
{
"route_name" : "Management2" ,
"address_prefix" : "172.31.148.0/19" ,
"next_hop_type": "VirtualAppliance",
"next_hop_in_ip_address" : "172.31.14.4",
"rt_name": "Route_table_Management" -------> ROUTE TABLE NAME 2
}
]
And a module defined with the resource “azurerm_route_table” with dynamic content.
I would like to be able to iterate over this resource “azurerm_route_table” with for_each=var.route and that the routes to be generated in their corresponding route table name.
So if route_table_name and rt_name dont match, i would pass to next iteration, but I don’t know if it is at all possible and if it is I don’t know the correct syntax or function to use
Maybe to create a sub map wth only the map that satisfy the condition.
This code leaves the route that doesn´t match with variable name = 0, address =0 because the condition forces to give a value.
resource "azurerm_route_table" "r_rt" {
name = "${var.name}"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
disable_bgp_route_propagation = "${var.disable_bgp_route_propagation}"
dynamic "route" {
for_each = var.route
content {
name = (route.value["rt_name"]=="${var.name}") ? route.value["route_name"] : 0
address_prefix = (route.value["rt_name"]=="${var.name}") ? route.value["address_prefix"] :0
next_hop_type = (route.value["rt_name"]=="${var.name}") ? route.value["next_hop_type"] : "None"
next_hop_in_ip_address = (route.value["rt_name"]=="${var.name}") ? route.value["next_hop_in_ip_address"] : ""
}
}
}
Output
Thanks in advance