Dynamically building map with keys

Hi,

I have a list of ids and a map containing settings x, y… for the ids. The map does not contain entries all the ids. I was hoping to build a map with an entry for each id with this format:

{
    id1 = {
        x = ...
        y = ...
    },
    id2 = {
        x = ...
        y = ...
    }
    ...
}

I thought about using a for loop like this:

instruction_map = [ for id in local.ids : {
    "${id}" = {
      x = lookup...
      y = lookup...
    }
  }
]

But that created the following:

[
  {
    "id1" = {
      "x" = "..."
      "y" = "..."
    }
  },
  {
    "id2" = {
      "x" = "...",
      "y" = "..."
    }
  },
]

Is there a way to transform that structure to the one I want, or a better way to build the map?

I came upon it now. The => symbol when building maps. That made me realize to change the outer return type (which was an obvious mistake by me). So it should be:

instruction_map = { for id in local.ids : 
   "${id}" => {
      x = ...
   }
}

Sorry if this seems obvious to some of you. I’m just new and didn’t find much examples under the documentation of the for expression, and had googled around till I was a bit dizzy.

1 Like