AWS Custom Lambda returns status OK but API Gateway v2 fails with 500

0

Hi, the lambda works fine with nodejs bookworm custom image and API Gateway v2 (http). Similar code is used for the lambda with ubuntu image and the code works fine when testing the container locally and calling the lambda via AWS CLI but fails with 500 "Internal Server Error" when the call is coming from API Gateway v2 (http). Lambda logs and X-ray both show status OK (all green). I tried simple payloads and responses without success. How can I fix the issue? Setup can be reproduced in one minute with https://github.com/Ducharme/awsCustomLambdas (default values). Thanks in advance

3 Answers
0
Accepted Answer

The problem has been fixed -> There was a JSON format issue in the response returned by the lambda. The function returned 200 both locally and from AWS with the Console and AWS CLI but failed with 500 when called via the API Gateway v2 with curl. The "body" part of the response had to be escaped when it is a JSON (simple string had no issue).

There is unfortunatelly no example of "body" in https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html

Expected format as seen in the Console Lambda/Test execution log '{ "isBase64Encoded": false, "statusCode": 200, "body": "{ \"allo\": \"hehe\" }" }'

Code used in the bash handler:

  ESCAPED=$(echo "$EVENT_DATA" | sed 's/"/\\"/g')
  RESPONSE="{ \"isBase64Encoded\": false, \"statusCode\": 200, \"body\": \"$ESCAPED\" }"
  echo $RESPONSE

Code used in the nodejs handler:

  return {
    "isBase64Encoded": false,
    "statusCode": 200,
    "body": JSON.stringify(event)
  };

GitHub repository https://github.com/Ducharme/awsCustomLambdas was updated to include the fixes. Hope this helps

Claude
answered 9 months ago
0

If you haven't gone through following articles, please take a look at these. These two talk about the same problem in very detail:

Hope you find this useful.

Comment here if you have additional questions.

Abhishek

profile pictureAWS
EXPERT
answered 9 months ago
  • Did you take a look at here

    To create a HTTP integration request

    Command:

        aws apigateway put-integration --rest-api-id 1234123412 --resource-id a1b2c3 --http-method GET --type HTTP --integration-http-method GET --uri 'https://domain.tld/path'
    

    To create an AWS integration request with a Lambda Function endpoint

    Command:

       aws apigateway put-integration --rest-api-id 1234123412 --resource-id a1b2c3 --http-method GET --type AWS --integration-http-method POST --uri 'arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:123412341234:function:function_name/invocations'
    
  • Link provided is to put-integration with AWS CLI v1 is for API Gateway v1 (apigateway). My code in https://github.com/Ducharme/awsCustomLambdas/blob/master/create_api.sh is using AWS CLI v2 for API Gateway v2 (apigatewayv2) as mentioned in the title and first comment. Have you been able to reproduce the issue?

    Here is the command to handle the integration for easy reference aws apigatewayv2 create-integration --api-id $API_ID --integration-type AWS_PROXY --integration-uri $LAMBDA_ARN --payload-format-version "1.0"

    Note: adding "--integration-method $ROUTE_VERB" makes no difference

0

Thanks for the links, they gave me more perspective. The call is successful with exactly same lambda via API Gateway v2 when the "aws apigatewayv2 create-integration" command argument "--integration-type" is "HTTP_PROXY" with Function URL instead of "AWS_PROXY" with the lambda ARN. Would it be possible to provide a more specific solution for the AWS_PROXY integration problem? Thanks

Things that did not work or do not apply in the setup:

  • HTTP APIs currently support access logging only
  • There is no error in the Lambda function code
  • Permissions are set properly for the lambda (it is called and returns OK)
  • Incorrect or missing HTTP status code mapping does not seem to apply to v2
  • Throttling issues -> There is only one call every 1-2 minute or so approx
  • Undefined HTTP method of POST -> Does not seem to be relevant for v2
  • Lambda permissions is set properly since it is called and logs are as expected
  • Lambda function JSON format issue -> Multiple payloads were tried
  • Backend payload size exceeding 10 MB -> Less than 50 byes are returned
  • Private endpoint integration -> Not used
Claude
answered 9 months ago
  • I also tried docker image ubuntu:18.04 as base instead of ubuntu:22.04.

    Other attempts were made to change/override ENTRYPOINT and CMD in the Dockerfile and in the Lambda (different permutations of /var/task/aws-lambda-rie, /var/task/bootstrap, function.handler). It normally gives "Error: Runtime exited with error: exit status 2" and sometimes "[PANIC] (rapid) Runtime API Server failed to listen error=listen tcp 127.0.0.1:9001: bind: address already in use" when testing the lambda in the console.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions