Transform nested list values to include value from parent map

Viewed 16

Is it possible to construct a data structure given the input variable below, where you append the name value to each value in the stages list;

    environments = [
      {
        name   = "preprod"
        stages = ["blue", "green"]
      },
      {
        name   = "qat"
        stages = ["blue", "green"]
      }
    ]

so that I end up with a data structure that looks like this once flattened:

local.transformed = [
"preprod-blue",
"preprod-green",
"prod-blue",
"prod-green"
]
1 Answers

You can use flatten:

locals {
  transformed = flatten([for val in var.environments: [
                  for color in val.stages:
                      "${val.name}-${color}"
                  ]
                ])
  
}
Related