Invert the association of yaml nodes in terraform

Assuming the following yaml input file

admins:
  members:
    - john@gmail.com
    - peter@gmail.com
viewers:
  members:
    - maria@gmail.com
    - john@gmail.com

that is passed to var.memberships via yamldecode, is it possible to produce a list of objects that has the member as key and the groups it belongs to as values, i.e. something like

{
  "maria@gmail.com" = {
    groups = ["viewers"]
  },
  "john@gmail.com" = {
    groups = ["admins", "viewers"]
  },
  "peter@gmail.com" = {
    groups = ["admins"]
  }
}

Hi @pantelis-karamolegko!

I think I remember answering this question already in some other forum. If I’m remembering correctly, would you mind copying my answer into here (or just a link to it, if that’s easier) so that folks viewing this forum can also find it?

If I’m misremembering and this is actually a different question then I apologize.