Search code examples
terraformhcl

Looking for a more concise way of doing a nested loop


Looking for a cleaner/more-readable way to achieve a nested loop in Terraform. I will illustrate with an example.

Let's say we have variable for roles that looks like this:

variable "roles" {
  type    = "list"
  default = [
    {
      name    = "LOADER"
      schemas = {
        RAW = ["USAGE", "ALL"]
        SRC = ["ALL"]
      }
    },
    {
      name    = "USER"
      schemas = {
        RAW = ["DELETE", "OBJECT"]
        SRC = ["USE"]
      }
    }
  ]
}

From this, I want to end up with a List of dictionaries that looks something like:

output = [
  {
    "privilege" = "USAGE"
    "role" = "LOADER"
    "schema" = "RAW"
  },
  {
    "privilege" = "ALL"
    "role" = "LOADER"
    "schema" = "RAW"
  },
  {
    "privilege" = "ALL"
    "role" = "LOADER"
    "schema" = "SRC"
  },
  {
    "privilege" = "DELETE"
    "role" = "USER"
    "schema" = "RAW"
  },
  {
    "privilege" = "OBJECT"
    "role" = "USER"
    "schema" = "RAW"
  },
  {
    "privilege" = "USE"
    "role" = "USER"
    "schema" = "SRC"
  },
]

What I have tried so far (seems to work but I am looking for a more concise/readable way to do it):

locals {
  # FlatMapping to a list of dictionaries. Each dict in the form of {schema=<schema>, role=<role>, privilege=<privilege>}
  key_val       = [for role in var.roles : [for schema, privilege in role["schemas"]: {
    role      = role["name"]
    schema    = schema
    privilege = privilege
  }]]
  other_key_val = [for dict in flatten(local.key_val): [for priv in dict["privilege"]: {
    role      = dict["role"]
    schema    = dict["schema"]
    privilege = priv
  }]]
}

output "output" {
  value = flatten(local.other_key_val)
}

My main objective is to have readable code that can be understood better by others. Given that I am using loops in Terraform for the first time, I can't judge if my implementation is considered readable.


Solution

  • Maybe this would be a little bit simpler way to achieve the same result:

    locals {
      roles = [
          {
            name    = "LOADER"
            schemas = {
              RAW = ["USAGE", "ALL"]
              SRC = ["ALL"]
            }
          },
          {
            name    = "USER"
            schemas = {
              RAW = ["DELETE", "OBJECT"]
              SRC = ["USE"]
            }
          }
      ]
    
      out = flatten([
        for item in local.roles: [
          for schema, privileges in item.schemas: [
            for privilege in privileges: {
              role = item.name
              privilege = privilege
              schema = schema
            }
          ]
        ]
      ])
    }