How to merge two maps with same key?

I have this:

  local  = {
      aks   = {
          type = "aks"
        }
      private_endpoints = {
          type = "base"
        }
    }
#################################
  subnet_feature1 = {
      aks = {
          cidr = [
               "10.2.1.0/27",
            ]
        }
      private_endpoints = {
          cidr = [
              "10.2.1.64/26",
            ]
        }
    }

I’d love to combine this two maps to one look like this:

  subnet_feature2 = {
      aks               = {
          cidr = [
              + "10.2.1.0/27",
            ],
          type = "aks"
        }
      private_endpoints = {
          cidr = [
              "10.2.1.64/26",
            ],
         type = "base"
        }
    }

Similar question from here

Below code should do the trick.

locals {
	local = {
      		aks   = {
          		type = "aks"
        	}
     		 private_endpoints = {
          		type = "base"
        	}
    	}

	subnet_feature1 = {
      		aks = {
          		cidr = [
               			"10.2.1.0/27",
            		]
        	}
      		private_endpoints = {
          		cidr = [
              			"10.2.1.64/26",
            		]
        	}
    	}
}

output "calc_map" {
	value = {
		for i in keys(local.local) : i => { 
			cidr = local.subnet_feature1[i].cidr, 
			type = local.local[i].type
		} 
	}
}
1 Like