Iterate over map with lists

Hi all,

I can’t quite figure out the for_each syntax to iterate over a map, and extract embedded lists to pass to my module, would someone know the syntax for the below?

Variables:

variable "repos" {
  type = map(object({
    name = set(string)
  }))

  default = {

    springboot = {
      name = [
        "springboot-a",
        "springboot-b",
        "springboot-c"
      ],
      push_teams = ["springboot-developers"]
    },

    node = {
      name = [
        "node-a",
        "node-b",
        "node-c"
      ],
      push_teams = ["node-developers" ]
    }
  }
}

Module:

module "repository" {
 for_each = { for k, v in var.repos: ... } # ??

  name         = each.value.name
  push_teams   = each.value.push_teams 
}

I’m unsure of the syntax to extract/flatten this object to pass to the module above.

I’d expect once the data is unrolled to look something like the below:

repos.springboot.name[0]       = "springboot-a"
repos.springboot.push_teams[0] = [ "springboot-developers" ]
repos.springboot.name[1]       = "springboot-b"
repos.springboot.push_teams[1] = [ "springboot-developers" ]
repos.springboot.name[2]       = "springboot-c"
repos.springboot.push_teams[2] = [ "springboot-developers" ]
repos.node.name[0]             = "node-a"
repos.springboot.push_teams[0] = [ "node-developers" ]
repos.node.name[1]             = "node-b"
repos.springboot.push_teams[0] = [ "node-developers" ]
repos.node.name[0]             = "node-c"
repos.springboot.push_teams[0] = [ "node-developers" ]

Thanks!

For the outcome that you are expecting, I don’t think you are building your data structure correctly.

In your example, you have what looks like two types of repos: springboot and node. You could have a variable for each. Instead of just var.repos you could have var.springboot_repos and var.node_repos.

You could also make var.repos a list of objects instead like so:

variable "repos" {
   type = list(object({
    name = string
    push_teams = list(string)
  }))
  default = [
    {
      name = "springboot-a"
      push_teams = [ "springboots-developers" ]
    }
}

I think I’ll likely just reorganize the structure and create it as above, but I was thinking I could have a map of $N elements, which could grow over time, that consist of a list of tech stacks named springboot, node, lambda etc and grows in the future, with a list of repos to manage / create with additional similar types of permission in the future, and an associated team with each tech stack.

But that may be too complex to try and easily flatten out…