Create list of objects from multiple list of objects

Hello Everyone,

I have following lists

bucket_dns_names = [
“1”,
“2”,
“3”,
]

bucket_ids = [
“11”,
“22”,
“33”,
]

cloudfront_origin_access_identity_paths = [
“111”,
“222”,
“333”,
]

What I want to create is :
final_list = [ [“1”, “11”, “111”], [“2”, “22”, “222”], [“3”, “33”, “333”] ]

How can we achieve this in terraform 12?

Thanks in advance!

As long as your lists are always the same length, you can do this using a for expression with the range function and the length function:

locals {
  as = ["1", "2", "3"]
  bs = ["11", "22", "33"]
  cs = ["111", "222", "333"]
}

output "all" {
  value = [
    for i in range(length(local.as)) : [
      local.as[i],
      local.bs[i],
      local.cs[i]
    ]
  ]
}
$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

all = [
  [
    "1",
    "11",
    "111",
  ],
  [
    "2",
    "22",
    "222",
  ],
  [
    "3",
    "33",
    "333",
  ],
]