Accessing values in nested map

Hi,

From a data source I get a list of ids. For each of these, I need to call a module with the id as input1 and several other settings. Something like this.

module "a" {
   for_each = toset(local.ids)
   source = "./modules/a"
   id = each.key
   setting1 = ...
   setting2 = ...
   ...
}

But, I need to have a map with custom settings

locals {
  custom_settings = {
    "id1" = {
      "setting1" : "..."
      "setting2" : "..."
    }
    "id3" = {
      "setting2" : "..."
    }
  }
}

If a custom setting exists for the id, I should use it in the module call, otherwise a settings default.

For the case of no setting, I was planning to pass in null and have the module input variable be non-nullable so it gets the default.

variable "setting1" {
  nullable = false
  default = "default"
}

But I cannot figure out how to look for the nested value. What approach could I use? I was trying to use a for loop

value = [ for application in local.applications : {
      query = lookup(local.custom_settings, application, null) == null ? null : lookup(local.custom_settings.application, "setting1", null) 
      }
   ]

But it fails, I don’t see this syntax with [ ] in the for loop documentation at all so I don’t know what the iterating variable really is.

Maybe the premise with the customizations map seems strange, but unfortunately, I cannot change it.

It feels like you skipped a bit of the explanation in the middle? What nested value?

module "a" {
   for_each = toset(local.ids)
   source = "./modules/a"
   id = each.key
   setting1 = try(local.custom_settings[each.key]["setting1"], null)
   setting2 = try(local.custom_settings[each.key]["setting2"], null)
   ...
}

I don’t follow all you said, but I think you are looking for something like that.

Hi, this post got locked by the spam filter for an extended period when it was posted for some reason, and I couldn’t comment or delete the post although I had found my solution during the time it was locked.

The solution was try, exactly as grimm26 suggested.