Selection pattern support for API Gateway with Lambda Integration

I have asked the same question on SO, but got no answer, sorry for copying it here.

I am using Localstack with API Gateway and Lambda Integration to test my APIs. You can find the repository here. Setup scripts are in the folder src/test/resources/localstack/scripts

I am trying to create multiple integration responses for a single API, which is GET /users/{userId}. I have integrated the Lambda function with the API Gateway, the integration type is AWS. The issue is that the default response is always called (200), whereas I would like to return 404 when the user does not exist. It should return 404 when the lambda throws an exception with the message No value present (which is the message of a NoSuchElementException). Basically, I need two integration responses:

  • The default one (200)
awslocal apigateway put-integration-response \
          --rest-api-id "$_REST_API_ID" \
          --resource-id "$_RESOURCE_ID" \
          --http-method "$_HTTP_METHOD" \
          --status-code 200 \
          --selection-pattern ""

I have also tried to remove --selection-pattern but it is the only one called anyway.

  • The one for the “no user with that id” case (404):
awslocal apigateway put-integration-response \
          --rest-api-id "$_REST_API_ID" \
          --resource-id "$_RESOURCE_ID" \
          --http-method "$_HTTP_METHOD" \
          --status-code "404" \
          --selection-pattern ".*No value present.*"

I have read that the selection pattern tries to match the errorMessage of the lambda response, but it does not seem the case.

This is the HTTP response I have intercepted with Wireshark:

GET /restapis/s3zoijp35e/test/_user_request_/users/1 HTTP/1.1
Connection: Upgrade, HTTP2-Settings
Content-Length: 0
Upgrade: h2c
User-Agent: Java-http-client/17.0.7
Content-Type: application/json

HTTP/1.1 200 
Content-Type: text/plain; charset=utf-8
Content-Length: 952
Connection: close
date: Sat, 19 Aug 2023 08:40:32 GMT
server: hypercorn-h11

{"errorMessage":"No value present","errorType":"java.util.NoSuchElementException","stackTrace":["java.base/java.util.Optional.orElseThrow(Unknown Source)","it.unimi.cloudproject.ui.lambda.UserLambda.lambda$getUser$2(","$FunctionInvocationWrapper.invokeFunctionAndEnrichResultIfNecessary(","$FunctionInvocationWrapper.invokeFunction(","$FunctionInvocationWrapper.doApply(","$FunctionInvocationWrapper.apply(",""]}

This test breaks for that reason, showing also the lambda logs.

Does Localstack correctly support the --selection-pattern option? I am pretty sure that it is at least partially supported because if I use the pattern .* with the 404 response, the gateway always chooses this one.

Hi @Maluz, thanks for using Localstack! I’ll try to reproduce this issue and I’ve seen you created issue here so you can follow up there.

Thank you very much for your work on Localstack, it is an awesome product. If you need any additional details or a minimum reproducible example, let me know. It is a blocking issue in my case, so I will be as cooperative as possible. In the meantime, I am using an AWS_PROXY integration type, but it is just a workaround that is not going to last.

Thanks again