I am trying to use the following data source, it produces the output I want but it is a nested map with the “filter” parameters which has each of the individual vpc id’s along with the “ids” which are the actual subnet-id’s associated with that vpc.
data "aws_subnets" "eks_subnets" {
for_each = var.eks_vpc_map
filter {
name = "vpc-id"
values = ["${each.value.eks_vpc_id}"]
}
}
How do I transform this into a simple map which is defined like this?
vpc_id=“string”
subnets_ids=list of all subnet ids
The design of this data source unfortunately makes this a bit awkward, because the VPC ID is not a first-class argument but instead just one of many possible filter block names. That means it’s kinda bothersome to obtain that single VPC ID value from just the data.aws_subnets.eks_subnets mapping.
However, since the instance keys of data.aws_subnets.eks_subnets are guaranteed to match the keys of your var.eks_vpc_map you can cross-reference with the original map to more easily recover the VPC ID belonging to a particular key, like this:
locals {
vpcs = tomap({
for k, result in data.aws_subnets.eks_subnets : k => {
vpc_id = var.eks_vpc_map[k].eks_vpc_id
subnet_ids = result.ids
}
})
}
With the declaration above I think local.vpcs will be a map value shaped like you want, where the keys are all the same keys from var.eks_vpc_map and the values are all objects with vpc_id and subnet_ids attributes.