How can I resolve the errors that I receive when I integrate API Gateway with a Lambda function?

6 minute read
0

I want to resolve the errors that I receive when I integrate Amazon API Gateway with an AWS Lambda function.

Resolution

Turn on logging for your API and stage

1.    In the API Gateway console, find the Stage Editor for your API.

2.    On the Stage Editor pane, choose the Logs/Tracing tab.

3.    On the Logs/Tracing tab, for CloudWatch Settings, do the following to turn on logging:
Choose the Enable CloudWatch Logs check box.
For Log level, choose INFO to generate logs for all requests. Or, choose ERROR to generate logs only for requests to your API that result in an error.
For REST APIs, choose the Log full requests/responses data check box. Or, for WebSocket APIs, choose the Log full message data check box.

4.    Under Custom Access Logging, do the following to turn on access logging:
Choose the Enable Access Logging check box.
For Access Log Destination ARN, enter the Amazon Resource Name (ARN) of a CloudWatch log group or an Amazon Kinesis Data Firehose stream.
Enter a Log Format. For guidance, choose CLF, JSON, XML, or CSV to see an example in that format.

5.    Choose Save Changes.
Note: The console doesn't confirm that the settings are saved.

For more information, see Set up CloudWatch API logging using the API Gateway console.

Determine integration types, verify errors, and take next steps

1.    Determine whether a Lambda proxy integration or a Lambda custom integration is set up in API Gateway. You can verify the integration type by reviewing the Lambda function output or by running the get-integration command.

2.    Verify that the errors in API Gateway correspond to the errors in Lambda. Run the following CloudWatch Logs Insights query to find an error status code during a specified time frame:

parse @message '(*) *' as reqId, message
    | filter message like /Method completed with status: \d\d\d/
    | parse message 'Method completed with status: *' as status
    | filter status != 200
    | sort @timestamp asc
    | limit 50

Then, run the following CloudWatch Logs Insights query to search for Lambda error logs during the same time frame:

fields @timestamp, @message
    | filter @message like /(?i)(Exception|error|fail)/
    | sort @timestamp desc
    | limit 20

3.    Based on the type of error that you identify in your logs, choose one of the following:

If you receive the following error, then complete the steps in the Resolve concurrency issues section.

(XXXXX) Lambda invocation failed with status: 429. Lambda request id: XXXXXXXXXX
(XXXXX) Execution failed due to configuration error: Rate Exceeded.
(XXXXX) Method completed with status: 500

If you receive either of the following errors, then complete the steps in the Resolve timeout issues section.

For a Lambda custom integration:

< 29 sec:
(XXXXX) Method response body after transformations: {"errorMessage":"2019-08-14T02:45:14.133Z xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Task timed out after xx.01 seconds"}
> 29 sec:
(XXXXX) Execution failed due to a timeout error

For a Lambda proxy integration:

< 29 sec:
(XXXXX) Endpoint response body before transformations: {"errorMessage":"2019-08-14T02:50:25.865Z xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Task timed out after xx.01 seconds"}
> 29 sec:
(XXXXX) Execution failed due to a timeout error

If you receive the following error, then complete the steps in the Resolve function errors section.

(XXXXX) Execution failed due to configuration error: Malformed Lambda proxy response
(XXXXX) Method response body after transformations: {"errorMessage": "Syntax error in module 'lambda_function'"}

Resolve concurrency issues

You receive 429 throttling errors or 500 errors when additional requests come in from API Gateway faster than your Lambda function can scale.

To resolve these errors, analyze the Count (API Gateway), Throttles (Lambda), and ConcurrentExecutions (Lambda) metrics in CloudWatch. Consider the following:

  • Count (API Gateway) is the total number API requests in a given period.
  • Throttles (Lambda) are the number of invocation requests that are throttled. When all function instances are processing requests and no concurrency is available to scale up, Lambda rejects additional requests with the TooManyRequestsException error. Throttled requests and other invocation errors don't count as invocations or errors.
  • ConcurrentExecutions (Lambda) is the number of function instances that are processing events. If this number reaches your concurrent executions quota for the AWS Region, then additional invocation requests are throttled. Invocation requests are also throttled when the number of function instances reaches the reserved concurrency limit that you configured on the function.

Note: For more information, see API Gateway metrics and Working with Lambda function metrics.

If you set the reserve concurrency in your Lambda function, then set a higher reserve concurrency value for the Lambda function. Or, remove the reverse concurrency value from the Lambda function. The function then draws from the pool of unreserved concurrent executions.

If you don't set the reserve concurrency in the Lambda function, then check the ConcurrentExecutions metric to find out the usage. For more information, see Lambda quotas.

Resolve timeout issues

The integration timeout is 29 seconds (a hard limit) for all API Gateway integrations. You might encounter two scenarios when you build an API Gateway API with Lambda integration. The scenarios are when the timeout is less than 29 seconds or greater than 29 seconds.

If your Lambda function timeout is less than 29 seconds, then check your Lambda logs to investigate this issue. If your Lambda function must run after 29 seconds, consider invoking the Lambda function asynchronously.

For Lambda custom integration, complete the following steps:

1.    Open the API Gateway console.

2.    From the navigation pane, choose APIs, and then choose your API.

3.    Choose Resources, and then choose your method.

4.    Choose Integration Request.

5.    Choose Method Request.

6.    Expand HTTP Request Headers.

7.    Choose Add header.

8.    For Name, enter a name for your header. For example: X-Amz-Invocation-Type

Important: You must map your header from 'Event' (single quotes are required).

For Lambda proxy integration:

Use two Lambda functions: function A and function B. API Gateway first invokes function A synchronously. Then, function A invokes function B asynchronously. Function A can return a successful response to API Gateway when function B is invoked asynchronously.

If you're using Lambda proxy integration, consider changing it to a custom integration. However, you must configure the mapping templates to transform the request/response to your desired format. For more information, see Set up asynchronous invocation of the backend Lambda function.

Note: Because an asynchronous Lambda function runs in the background, your client can't receive data from a Lambda function directly. You must have an intermediate database to store any persistent data.

Resolve function errors

If you receive a function error when you invoke your API, then check your Lambda function for any syntax errors. This error also appears if your Lambda function isn't returning a valid JSON object that API Gateway expects for proxy integrations.

To resolve this error, complete the steps in the Turn on logging for your API and stage section.


Related information

Handle standard Lambda errors in API Gateway

Handle custom Lambda errors in API Gateway

AWS OFFICIAL
AWS OFFICIALUpdated a year ago