For Each iteration inside existing For Each iteration

Hello, here again I’m struggling with for_each iteration.

Here what I actually want to achieve:

locals {
   restapi = lookup(var.parameter, "resapi", [])
}


resource "aws_api_gateway_gateway_response"   "rest" {
   for_each            = { for api, name in local.restapi : api => name }
   rest_api_id         = element([ for key, value in aws_api_gateway_rest_api.api : value.id ], each.key)
   status_code         = lookup(each.value.response, "code",       null)
   response_type       = lookup(each.value.response, "type",       null)
   response_templates  = lookup(each.value.response, "templates",  null)
   response_parameters = lookup(each.value.response, "options",    null)
}


... Module `restapi`...
    Other Code
parameters {
   restapi = [
      {
         response = [
            {
               code = "400"
               type = upper("Bad Request")
            },
            {
               code = "401"
               type = upper("Unauthorized")
            },
            {
               code = "403"
               type = upper("Forbidden")
            }
         ]
      }
   ]
}

    Other Code

So now it only taking the first map:

            {
               code = "400"
               type = upper("Bad Request")
            },

Not iterating with others

I think this is incorrect.

restapi as posted is a list.

So, for api, name is actually for index, list_element

Now, you have just one list element - the map that hosts the response

What I could recommend is creating a custom structure in your locals and using that in aws_api_gateway_gateway_response

1 Like

Hi, @gc-ss, thank you for helping me. If it possible can you give an example, so I could catch the logic.
As you said, "restapi as posted is a list", but I would like to have 3-4 restapis, and each of them should also contain the response, and response it self should by dynamic