This occurs with the AWS provider, but it is a basic aspect of HCL data wrangling. Using Terraform v0.12.24.
I have a data source defined as follows that gives me a map of maps:
# Import CFN stack exports external to Terraform (not my choice, decreed by the admins)
variable "aws_private_subnets" {
default = [
"PrivateSubnet1",
"PrivateSubnet2",
"PrivateSubnet3"
]
}
data "aws_cloudformation_export" "private_subnets" {
for_each = toset(var.aws_private_subnets)
name = each.value
}
The data.aws_cloudformation_export.private_subnets has the following structure:
{
"PrivateSubnet1" = {
"exporting_stack_id" = "masked"
"id" = "b"
"name" = "PrivateSubnet1"
"value" = "subnet-x"
}
"PrivateSubnet2" = {
"exporting_stack_id" = "masked"
"id" = "masked"
"name" = "PrivateSubnet2"
"value" = "subnet-y"
}
"PrivateSubnet3" = {
"exporting_stack_id" = "masked"
"id" = "masked"
"name" = "PrivateSubnet3"
"value" = "subnet-z"
}
}
I wish to transform this during usage into a list of the values of the same key of each map:
["subnet-x","subnet-y","subnet-z"]
, that is the values of the “value” key of each object.
I can cast this map to a list by calling values() to arrive at a list of maps, but I fail to discern the correct way to collect the values. Is there an elegant way to do this basic mapping? The following and variations of it are all invalid:
data.aws_cloudformation_export.private_subnets[*].value
data.aws_cloudformation_export.private_subnets.*.value
Am I somehow implementing worst pratices by organizing data as a map of maps? Is Terraform/HCL not well equipped to wrangle nested data structures?
Thanks in advance.