Good day,
I have a map of VPC objects returned from the below:
variable "vpcs" {
description = "List of available VPCs."
type = list(string)
default = [
"vpc123",
"vpc456"
]
}
data "aws_vpc" "all_vpcs" {
for_each = toset(var.vpcs)
tags = {
Environment = "dev"
VPC = each.key
}
}
For each of the above VPCs I want to create R53 zones for a list of zones:
variable "dns_zones" {
description = "List of available Route53 DNS Zones to manage."
type = list(string)
default = ["a", "b", "c"]
}
I created the below map to iterate through for the aws_route53_zone resource:
locals {
zone_map = merge([
for vpc in data.aws_vpc.all_vpcs : {
for zone in var.dns_zones :
"${vpc.id}-${zone}" => {
"vpc_id" = vpc.id
"zone" = zone
}
}
]...)
}
resource "aws_route53_zone" "dns_zones" {
for_each = local.zone_map
name = each.value.zone
vpc {
vpc_id = each.value.vpc_id
}
}
This feels a bit like an off-label use of merge and grouping mode. Is there a better design pattern to create the below map:
zone_map = {
+ vpc123-a = {
+ vpc_id = "vpc-1111"
+ zone = "a"
}
+ vpc123-b = {
+ vpc_id = "vpc-1111"
+ zone = "b"
}
+ vpc456-a = {
+ vpc_id = "vpc-2222"
+ zone = "a"
}
+ vpc456-b = {
+ vpc_id = "vpc-2222"
+ zone = "b"
}
}