I have a list of maps like this -
[
{
"outer_key_1" = [
{
"ip_cidr" = "172.16.6.0/24"
"range_name" = "range1"
},
{
"ip_cidr" = "172.16.7.0/24"
"range_name" = "range2"
},
{
"ip_cidr" = "172.17.6.0/24"
"range_name" = "range3"
},
{
"ip_cidr" = "172.17.7.0/24"
"range_name" = "range4"
},
]
},
{
"outer_key_2" = [
{
"ip_cidr" = "172.16.5.0/24"
"range_name" = "range5"
},
{
"ip_cidr" = "172.17.5.0/24"
"range_name" = "range6"
},
]
},
]
I want to merge the maps inside the list. This is in an output variable module.module_name.module_op
.
Required Output:
{
"outer_key_1" = [
{
"ip_cidr" = "172.16.6.0/24"
"range_name" = "range1"
},
{
"ip_cidr" = "172.16.7.0/24"
"range_name" = "range2"
},
{
"ip_cidr" = "172.17.6.0/24"
"range_name" = "range3"
},
{
"ip_cidr" = "172.17.7.0/24"
"range_name" = "range4"
},
]
"outer_key_2" = [
{
"ip_cidr" = "172.16.5.0/24"
"range_name" = "range5"
},
{
"ip_cidr" = "172.17.5.0/24"
"range_name" = "range6"
},
]
}
I have done this using
locals {
result = merge(module.module_name.module_op[0], module.module_name.module_op[1])
}
How do I do this in a more iterative way.
I will not always only 2 maps in my list, it can be more.
I tried using the for loop in terraform 12 like this -
output "result" {
value = [ for tuple in module.module_name.module_op : merge(tuple) ]
}
and this -
output "secondary_subnets" {
value = { for tuple in module.module_name.module_op : merge(tuple) }
}
The first one gives me my input back and the second one gives me an error saying I need to add a key since I am returning an object.
Is there a way to get this done?
Thanks in advance.