How to add field to each object inside a map(object)?

Hi everyone,
I have the following variable:

variable "aks_node_pools" {
  type = map(object({
    name                                               = string
    node_count                                    = optional(number)
    vm_size                                           = string
    capacity_reservation_group_id   = optional(string)
    }))

  default     = {}
  description = "Additional nodepools to be created on XYZ cluster."
  nullable    = false
}

Basically a map of objects that represent nodepools (number of attributes cut down for example purposes). What is need to do with this is make sure I add a field called “vnet_subnet_id” to each object before I can use it.

I thought that with locals block and merge function I can do the job, but I’m not sure what I do is correct

locals {
  # Store the field 'vnet_subnet_id' into an object then merge it with the nodepool object

  subnet_aks = {
    vnet_subnet_id = data.azurerm_subnet.aks.id
  }
 
  node_pools = { for nodepool in var.aks_node_pools : nodepool => merge(nodepool, local.subnet_aks) }
 
}

This gives me errors:

Error: Invalid object key
│ 
│   on main.tf line 46, in locals:
│   46:   node_pools = { for nodepool in var.aks_node_pools : nodepool => merge(nodepool, local.subnet_aks_elk) }
│ 
│ The key expression produced an invalid result: string required.

My intention as you can see was to make a for loop on each object in the map and merge it with the “object” which hold the field that I need to be added.

I’m guessing that this sounds good but maybe I’m on the wrong way to solve the problem. Do you have any ideas on this?

Seems like I already managed to solve the issue. The problem was at the for loops. The correct form is:

node_pools = { for nodepool, values in var.aks_node_pools : nodepool => merge(values, local.subnet_aks) }

I knew that the error meant something when I saw it saying “string” key. Using just for nodepool it was actually going trough the keys of the map which are string. So to modify the objects themselves you need to select the value not the key → for key, value in ....

Hopefully this might help somebody else in the future as well.