AWS API Gateway Method Response Correction of Indexes

I’ve an issue, with AWS API Gateway Method Response, while creating them, I made them dynamic so I can add later the extra responses if required, but I’ve noticed that, resource_id is not correct.


Here is the resource

resource "aws_api_gateway_method_response" "response" {
   for_each            = { for key, value in flatten(var.responses) : key => value }
   resource_id         = element([ for key, value in aws_api_gateway_resource.nesteds : value.id ], each.key)
   rest_api_id         = element([ for key, value in aws_api_gateway_rest_api.main    : value.id ], each.key)
   http_method         = element([ for key, value in aws_api_gateway_method.nesteds   : value.http_method ], each.key)
   status_code         = lookup(each.value, "code",     null)
   response_models     = lookup(each.value, "models",   null)
   response_parameters = lookup(each.value, "options",  null)
}

Inputs

# 1ST MAIN
responses   = [
   # 202
   {
       code        = 202
       models      = null
       options     = null
       description = "Slim Request accepted 'Ok'"
   },
   # 400
   {
       code        = 400
       models      = null
       options     = null
       description = "Incorrect slim request body 'Error' "
   }
]


# 2ND NESTED
responses   = [
   # 202
   {
       code        = 202
       models      = null
       options     = null
       description = "Stack Request accepted 'Ok'"
   },
   # 400
   {
       code        = 400
       models      = null
       options     = null
       description = "Incorrect stack request body 'Error' "
   }
]

Terraform Plan/Apply

# module.restapi.aws_api_gateway_method_response.response["0"] will be created
  + resource "aws_api_gateway_method_response" "response" {
      + http_method = "DELETE"
      + id          = (known after apply)
      + resource_id = "fsatfb"
      + rest_api_id = "mtu4b34wn4"
      + status_code = "202"
    }

  # module.restapi.aws_api_gateway_method_response.response["1"] will be created
  + resource "aws_api_gateway_method_response" "response" {
      + http_method = "POST"
      + id          = (known after apply)
      + resource_id = "s3eg5z"
      + rest_api_id = "mtu4b34wn4"
      + status_code = "400"
    }

  # module.restapi.aws_api_gateway_method_response.response["2"] will be created
  + resource "aws_api_gateway_method_response" "response" {
      + http_method = "DELETE"
      + id          = (known after apply)
      + resource_id = "fsatfb"
      + rest_api_id = "mtu4b34wn4"
      + status_code = "202"
    }

  # module.restapi.aws_api_gateway_method_response.response["3"] will be created
  + resource "aws_api_gateway_method_response" "response" {
      + http_method = "POST"
      + id          = (known after apply)
      + resource_id = "s3eg5z"
      + rest_api_id = "mtu4b34wn4"
      + status_code = "400"
    }

Plan: 4 to add, 0 to change, 0 to destroy.

I’ve removed unused elements, from inputs. So issue is with resource_id as mentioned already. If you notice I’ve in total 4 resources that will be created, each of objects are binding with http_method. At the end the method DELETE must use whole block, but it is using only first object of list, instead of using all. DELETE must have status_code 202 and 400 but it has only 202 two times, it will not be created, and will throw an error of conflicts, same is applies for POST method.

To summarise my problem, how my resource creation look like to achieve this:

  # module.restapi.aws_api_gateway_method_response.response["0"] will be created
  + resource "aws_api_gateway_method_response" "response" {
      + http_method = "DELETE"
      + id          = (known after apply)
      + resource_id = "fsatfb"
      + rest_api_id = "mtu4b34wn4"
      + status_code = "202"
    }

  # module.restapi.aws_api_gateway_method_response.response["1"] will be created
  + resource "aws_api_gateway_method_response" "response" {
      + http_method = "DELETE"
      + id          = (known after apply)
      + resource_id = "fsatfb"
      + rest_api_id = "mtu4b34wn4"
      + status_code = "400"
    }