Skip to content

AWS REST apigateway throws 502 even though integration timeout is set to 59000 ms

0

I have an apigateway -> lambda setup. I have raised a quota increase for api gateway integration timeout and the new limit is set to 59000ms.

Even then I see api gw throwing 502 gateway timeout errors when requests reach (~30 secs). The lambda has a timeout of 10 mins and keeps running in background even after api gw fails with 502. The lambda itself runs as expected without any errors. Is there additional I need to do to increase this limit ? I have made sure I "deployed" the api gw changes.

asked 2 years ago660 views

2 Answers
0

Intresting question.. Thinking outside of issue here, anything longer than 30 seconds seems too long for a response on a REST APi

A common practice is to set a connection timeout of around 5-10 seconds and a read timeout of 30 seconds. If your waiting longer than 30 seconds, I would suggest perhaps some redesign may be in order here.

Also, have you tried increasing the memory on the Lambda function if its resource constrained to respond faster?

EXPERT

answered 2 years ago

-2

It appears that you're encountering an issue with API Gateway timing out despite having increased the integration timeout quota. There are a few important points to consider:

  1. While you've successfully increased the quota for the API Gateway integration timeout to 59000 ms (59 seconds), it's crucial to note that this increased limit is only applicable to REST APIs. HTTP APIs and WebSocket APIs are still subject to the default 29-second timeout limit.

  2. If you're using a REST API and have properly applied and deployed the changes, but are still experiencing 502 Gateway Timeout errors around the 30-second mark, there might be other factors at play.

  3. API Gateway treats all invocation and function errors as internal errors. If your Lambda function is running for longer than the API Gateway timeout, even if the Lambda timeout is set to 10 minutes, API Gateway will return a 502 error with a generic "Internal server error" message.

  4. The fact that your Lambda function continues to run in the background after API Gateway returns a 502 error is expected behavior. API Gateway does not stop the Lambda execution; it simply stops waiting for a response after its timeout period.

To address this issue:

  1. Double-check that you're using a REST API, not an HTTP API or WebSocket API.

  2. Verify that the increased timeout has been correctly applied to your specific API and stage.

  3. Consider implementing a custom error handling mechanism in your Lambda function. You could catch errors and format responses in the required format to provide more meaningful error messages.

  4. If your Lambda function genuinely needs more than 59 seconds to complete, you might need to redesign your architecture. Consider using asynchronous processing patterns, such as having API Gateway trigger a Step Functions workflow or using SQS to decouple the request from the long-running process.

  5. Enable detailed CloudWatch logging for your API Gateway and Lambda function to get more insights into where exactly the timeout is occurring.

If you've verified all these points and are still experiencing issues, you may need to contact AWS support for further assistance, as there might be account-specific configurations or limitations at play.
Sources
Unable to increase AWS Timeout Duration past 29s | AWS re:Post
API getaway timeout limit | AWS re:Post
Handling Lambda errors with an API Gateway API - AWS Lambda

answered 2 years 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.