How do I troubleshoot issues for API Gateway endpoints that integrate with Lambda?

3 minute read
0

I want to integrate an Amazon API Gateway endpoint with AWS Lambda, but I'm experiencing issues.

Short description

The following reasons can cause issues for your API Gateway endpoint that integrates with Lambda:

  • API Gateway doesn't have Lambda function permissions.
  • Throttling occurs.
  • The HTTP status code mapping is incorrect or missing.
  • Lambda service exceptions aren't managed.

Resolution

Note: The following resolution uses REST API Gateway to troubleshoot.

Before you begin, turn on Amazon CloudWatch Logs to troubleshoot API Gateway errors. For Log level, choose INFO to generate execution logs for all requests.

API Gateway doesn't have Lambda function permissions

The Invalid permissions on Lambda function error occurs when API Gateway doesn't have permissions to invoke the Lambda function. To resolve this issue, see How do I resolve "Invalid permissions on Lambda function" errors from API Gateway REST APIs?

Note: If you use another service to deploy your API Gateway resources, such as AWS CloudFormation, then grant permissions to the AWS::Lambda::Permission resource.

Throttling occurs

If the backend service is throttled because of a high number of requests, then the API Gateway API might return an Internal server error. Activate an exponential backoff and retry mechanism, and then try the request again. If the issue persists, then check your API Gateway quota. If you exceed the service quota, then request a quota increase.

You might also receive Rate exceeded and 429 TooManyRequestsException errors because of Lambda function throttling issues. For more information, see How do I troubleshoot Lambda function throttling with "Rate exceeded" and 429 "TooManyRequestsException" errors?

Request a quota increase for the concurrency of the Lambda function so that the function doesn't throttle.

The status code mapping is incorrect or missing

When the HTTP status code mapping is incorrect or missing, you can receive a 500 error message similar to the following one:

"Execution failed due to configuration error: Output mapping refers to an invalid method response: 2xx/4xx/5xx".

To resolve this error, configure the API operation's integration request and response to return the correct status code.

Lambda service exceptions aren't managed

If you don't manage Lambda service exceptions, such as a Lambda function timeout, then you receive a Lambda.Unknown error. To prevent a Lambda function timeout, make your Lambda function code idempotent.

For more information, see Error handling patterns in Amazon API Gateway and AWS Lambda.

Related information

How do I troubleshoot 5xx errors for API Gateway REST API?

4 Comments

The underlying reason I was getting 500 with API Gateway v2 was malformed payload (HTTP status code mapping was correct in json). The quotes needed to be escaped in the json or the payload to be encoded in Base64. An example of valid payload would be '{ "isBase64Encoded": false, "statusCode": 200, "body": "{ "allo": "hehe" }" }'. More details in this article and AWS Custom Lambda returns status OK but API Gateway v2 fails with 500

replied 2 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
EXPERT
replied 2 years ago

Is it possible to map a throttling issue of an authorizer lambda (429 error in execution logs) to an ApiGateway response status code 429? Or does it in this case always return a 500 error to the client and there is no way to change that?

replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
EXPERT
replied a year ago