Skip to content

API Gateway timeouts when calling Lambda

0

I have integrated API Gateway with an AWS Lambda function written in Node.js (The Lambda function is not configured with a VPC, and the timeout is set to 50 seconds.). This Lambda only interacts with DynamoDB. The API generally works well, but occasionally, I encounter timeout errors.

According to the logs, API Gateway reports: "Execution failed due to a timeout error", and the Lambda logs indicate a timeout status as well.

The Lambda function is only utilizing 142MB out of the allocated 2048MB of memory, so it's unlikely that the issue is related to hardware or resource limitations. I currently suspect that the problem might be caused by a bottleneck or latency when querying DynamoDB.

Questions:

  1. What could be the potential cause of this intermittent timeout issue?
  2. Are there any tools or techniques available to trace the root cause of such requests, so I can monitor and diagnose problematic requests more effectively in the future?
  • Which API's are you calling for DynamoDB?

  • We're currently using the following DynamoDB APIs: GetItem, BatchGetItem, QueryItem.

2 Answers
0

Since the lambda & API is returning data correctly most of the time, I think your indexing in DynamoDB and Lambda configuration is correct. Is your DynamoDB also used by other systems and processes. If that is the case, it is possible that you do not have any throughputs left to service your query. Therefore, you can try enabling auto-scaling the DynamoDB as described here - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/AutoScaling.html . That way, the DynamoDB will always have capacity to service your request.

EXPERT
answered a year ago
  • Thank you for your feedback. Since my table is currently using the PAY_PER_REQUEST billing mode, I believe there's no need to enable auto-scaling.

0

When dealing with intermittent timeout issues between API Gateway and Lambda, there are several potential causes and diagnostic approaches to consider:

Potential Causes of Intermittent Timeouts

  1. Lambda Timeout Configuration: Your Lambda function might be timing out before completing its execution. Even though you have sufficient memory allocated (2048MB), the default timeout for Lambda functions is only 3 seconds, which might not be enough for some DynamoDB operations.

  2. API Gateway Timeout Mismatch: By default, API Gateway has a 29-second timeout limit for REST APIs (and can be increased up to 29 seconds for HTTP APIs). If your Lambda function takes longer than this limit, API Gateway will return a timeout error even if the Lambda function is still running.

  3. DynamoDB Throttling or Latency: DynamoDB might be throttling your requests due to exceeding provisioned throughput, or you might be experiencing high latency during peak usage times. This is especially likely if you're using on-demand capacity mode or if your provisioned capacity is insufficient.

  4. Cold Starts: If your Lambda function isn't invoked frequently, it might experience "cold starts" where the execution environment needs to be initialized, adding several seconds to the execution time.

Diagnostic Tools and Techniques

  1. Increase Lambda Timeout: Extend your Lambda function's timeout setting to give it more time to complete operations. This won't solve the underlying issue but will help determine if timeout is the root cause.

  2. CloudWatch Logs and Metrics: Add detailed logging in your Lambda function, especially around DynamoDB operations. Log timestamps before and after each significant operation to identify where the bottleneck occurs.

  3. X-Ray Tracing: Enable AWS X-Ray for both your API Gateway and Lambda function. X-Ray provides detailed tracing information, including latency for each component in your request path, helping you identify exactly where delays are occurring.

  4. DynamoDB Metrics: Monitor DynamoDB metrics like SuccessfulRequestLatency, ThrottledRequests, and ConsumedCapacity to identify if DynamoDB is the bottleneck.

  5. Optimize Lambda Initialization: Move any initialization code (like AWS SDK clients) outside the handler function so they persist between invocations, reducing cold start impact.

  6. Error Handling: Implement proper error handling in your Lambda function to catch and report specific errors rather than letting the function time out.

  7. Consider Asynchronous Processing: For operations that might take longer, consider implementing an asynchronous pattern where API Gateway returns immediately and the actual processing happens in the background.

By implementing these diagnostic approaches, you should be able to pinpoint the exact cause of your timeout issues and implement the appropriate solution.
Sources
Lambda API returns 200 on request timeout error | AWS re:Post
AWS REST apigateway throws 502 even though integration timeout is set to 59000 ms | AWS re:Post
Troubleshoot invocation issues in Lambda - AWS Lambda
REST API 504 Gateway Timeout Error triggering Lambda running on Flask | AWS re:Post

answered a year ago

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.