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
Since I tested with local variables, here’s a solution I have come up with:
This will give:
The solution is using
merge
andflatten
built-in functions as well as the ellipsis operator (...
), so that is not a mistake.You may use the following code: