You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Terraform has introduced the transpose function for maps, but there is no equivalent function for lists. A transpose function for lists would be beneficial for transforming an input like:
Such a function would be extremely useful when working with multiple lists as outputs from a module, which then need to be combined. For example, my Terraform module outputs the following lists:
output "node_names" {
value = openstack_compute_instance_v2.this.*.name
}
output "fixed_ips" {
value = openstack_networking_floatingip_v2.this.*.fixed_ip
}
output "floating_ips" {
value = openstack_networking_floatingip_v2.this.*.address
}
Each output contains lists of node names, fixed IP addresses, and floating IP addresses. From these lists, I need to create a combined list of [name, fixed_ip, floating_ip] for each node.
Attempted Solutions
The following code snippet transposes a list of lists in Terraform using nested for expressions. The try function is applied to handle cases where some inner lists may be shorter than others, substituting null for any missing elements:
locals {
input_list = [
["a1", "a2", "a3", "a4"],
["b1", "b2", "b3", "b4"],
["c1", "c2", "c3", "c4"]
]
output_list = [
for i in range(max([for l in local.input_list : length(l)]...)) : [
for j in range(length(local.input_list)) : try(
local.input_list[j][i], null
)
]
]
}
Proposal
No response
References
No response
The text was updated successfully, but these errors were encountered:
Hey @rajat-ventura, feel free to propose a PR! If you’re interested in contributing to Terraform, please begin by reviewing the contributing guide and following the process outlined there. As mentioned above, depending on the Terraform core team’s input, this function may be preferred as a provider-defined function within a specific provider.
Terraform Version
Use Cases
Terraform has introduced the transpose function for maps, but there is no equivalent function for lists. A transpose function for lists would be beneficial for transforming an input like:
into an output like:
Such a function would be extremely useful when working with multiple lists as outputs from a module, which then need to be combined. For example, my Terraform module outputs the following lists:
Each output contains lists of node names, fixed IP addresses, and floating IP addresses. From these lists, I need to create a combined list of
[name, fixed_ip, floating_ip]
for each node.Attempted Solutions
The following code snippet transposes a list of lists in Terraform using nested
for
expressions. Thetry
function is applied to handle cases where some inner lists may be shorter than others, substitutingnull
for any missing elements:Proposal
No response
References
No response
The text was updated successfully, but these errors were encountered: