Terraform | Resource mapping is not working

Hello, I’ve faced an issue when I was trying to create a zipmap function.


I’ve a resource which must take correct id, however in my situation terraform starts to indexing, and mapping wrong id, therefore I decided to use zipmap function, however it is not working as expected.

resource "aws_api_gateway_resource" "nesteds" {
   for_each    = { for api, name in var.layers : api => name }
   depends_on  = [  ]
   path_part   = lookup(each.value, "path", null)
   parent_id   = element([ for key, value in aws_api_gateway_resource.managed : value.id ], each.key)
   rest_api_id = element([ for key, value in aws_api_gateway_rest_api.managed : value.id ], each.key)
}

Where var.layers is list of objects:

resource = [
   # Resource A
   {
      ... code ... 
   
   
      layers = [
         {
             ... objects 1A ...
         },
         {
             ... objects 2A ...
         },
      ]
   },

   # Resource B
   {
      ... code ... 
   
   
      layers = [
         {
             ... objects 1B ...
         },
         {
             ... objects 2B ...
         },
      ]
   }
]

There are no issue with creating this resource, but as I said the layers are not mapping parent id correctly.

Case is: I’ve 2 resources and each of this resources have layers, with multiple objects inside (like dynamic blocks), with current setup, the layers are randomly asigning parent ids. In my case is:

Resource A is asigning, Resource B.Layer.Object1B, instead of Resource A.Layer.Object1A