Amazon API gateway timeout

Amazon Web-ServicesAws Api-Gateway

Amazon Web-Services Problem Overview


I have some issue with API gateway. I made a few API methods, sometimes they work longer than 10 seconds and Amazon returns 504 error. Here is screenshot below:

enter image description here

Please help! How can I increase timeout?

Thanks!

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

Right now the default limit for Lambda invocation or HTTP integration is 30s according to http://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html and this limit is not configurable.

Solution 2 - Amazon Web-Services

As of Dec/2017, the maximum value is still 29 seconds, but should be able to customize the timeout value.

https://aws.amazon.com/about-aws/whats-new/2017/11/customize-integration-timeouts-in-amazon-api-gateway/

This can be set in "Integration Request" of each method in APIGateway.

Solution 3 - Amazon Web-Services

You can't increase the timeout, at least not now. Your endpoints must complete in 10 seconds or less. You need to work on improving the speed of your endpoints.

http://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html

Solution 4 - Amazon Web-Services

Lambda functions will timeout after a max. of 5 min; API Gateway requests will timeout after 29 sec. You can't change that, but you can workaround it with asynchronous execution pattern, I wrote I blog post about:

https://joarleymoraes.com/serverless-long-running-http-requests/

Solution 5 - Amazon Web-Services

Finally in 2022 we have a workaround. Unfortunately AWS did not change the API Gateway so that's still 29 seconds but, you can use a built-in HTTPs endpoint in the lambda itself: Built-in HTTPS Endpoints for Single-Function Microservices which is confirmed to have no timeout-so essentially you can have the full 15 minute window of lambda timeout: https://twitter.com/alex_casalboni/status/1511973229740666883

Solution 6 - Amazon Web-Services

I wanted to comment on "joarleymoraes" post but don't have enough reputation. The only thing to add to that is that you don't HAVE to refactor to use async, it just depends on your backend and how you can split it up + your client side retries.

If you aren't seeing a high percentage of 504's and you aren't ready for async processing, you can implement client side retries with exponential backoff on them so they aren't permanent failures.

The AWS SDK automatically implements retries with backoff, so it can help to make it easier, especially since Lambda Layers will allow you to maintain the SDK for your functions without having to constantly update your deployment packages.

Once you do that it will result in less visibility into those timeouts, since they are no longer permanent failures. This can buy you some time to deal with the core problem, which is that you are seeing 504's in the first place. That certainly can mean refactoring your code to be more response, splitting up large functions into more "micro service" type concepts and reducing external network calls.

The other benefit to retries is that if you retry all 5xx responses from an application, it can cover a lot of different issues which you might see during normal execution. It is generally considered in all applications that these issues are never 100% avoidable so it's best practice to go ahead and plan for the worst!

All of that being said, you should still work on reducing the lambda execution time or going async. This will allow you to set your timeout values to a much smaller number, which allows you to fail faster. This helps a lot for reducing the impact on the front end, since it doesn't have to wait 29 seconds to retry a failed request.

Solution 7 - Amazon Web-Services

Timeouts can be decreased but cannot be increased more than 29 seconds. The backend on your method should return a response before 29 seconds else API gateway will throw 504 timeout error.

Alternatively, as suggested in some answers above, you can change the backend to send status code 202 (Accepted) meaning the request has been received successfully and the backend then continues further processing. Of course, we need to consider the use case and it's requirements before implementing the workaround

Solution 8 - Amazon Web-Services

While you cannot increase the timeout, you can link lambda's together if the work is something that could be split up.

Using the aws sdk:

var aws = require('aws-sdk');
var lambda = new aws.Lambda({
  region: 'us-west-2' //change to your region
});

lambda.invoke({
  FunctionName: 'name_of_your_lambda_function',
  Payload: JSON.stringify(event, null, 2) // pass params
}, function(error, data) {
  if (error) {
    context.done('error', error);
  }
  if(data.Payload){
   context.succeed(data.Payload)
  }
});

Source: https://stackoverflow.com/questions/31714788/can-an-aws-lambda-function-call-another#answer-31745774 AWS Documentation: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html

Solution 9 - Amazon Web-Services

As of May 21, 2021 This is still the same. The hard limit for the maximum time is 30 seconds. Below is the official document on quotas for API gateway. https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#http-api-quotas

Solution 10 - Amazon Web-Services

The timeout limits cannot be increased so a response should be returned within 30 seconds. The workaround I usually do :

  • Send the result in an async way. The lambda function should trigger another process and sends a response to the client saying Successfully started process X and the other process should notify the client in async way once it finishes (Hit an endpoint, Send a slack notification or an email..). You can found a lot of interesting resources concerning this topic
  • Utilize the full potential of the multiprocessing in your lambda function and increase the memory for a faster computing time
  • Eventually, if you need to return the result in a sync way and one lambda function cannot do the job, you could integrate API gateway directly with step function so you would have multiple lambda function working in parallel. It may seem complicated but in fact it is quite simple

Solution 11 - Amazon Web-Services

Lambda functions have 15 mins of max execution time, but since APIGateway has strict 29 second timeout policy, you can do following things to over come this.

> For an immediate fix, try increasing your lambda function size. Eg.: If your lambda function has 128 MB memory, you can increase it to 256 MB. More memory helps function to execute faster.

OR

> You can use lambdaInvoke() function which is part of the "aws-sdk". With lambdaInvoke() instead of going through APIGateway you can directly call that function. But this is useful on server side only.

OR > The best method to tackle this is -> Make request to APIGateway -> Inside the function push the received data into an SQS Queue -> Immediately return the response -> Have a lambda function ready which triggers when data available in this SQS Queue -> Inside this triggered function do your actual time complex executions -> Save the data to a data store -> If call is comes from client side(browser/mobile app) then implement long-polling to get the final processed result from the same data store.

Now since api is immediately returning the response after pushing data to SQS, your main function execution time will be much less now, and will resolve the APIGateway timeout issue.

There are other methods like using WebSockets, Writing event driven code etc. But above methods are much simpler to implement and manage.

Solution 12 - Amazon Web-Services

Custom timeout between 50 and 29,000 milliseconds for WebSocket APIs and between 50 and 30,000 milliseconds for HTTP APIs. The default timeout is 29 seconds for WebSocket APIs and 30 seconds for HTTP APIs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionИгорь КирейView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesRanView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesmyoujiView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesE.J. BrennanView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesjoarleymoraesView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesCapajView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicesNoPathInParticularView Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesMitayshh DhaggaiView Answer on Stackoverflow
Solution 8 - Amazon Web-ServicesCamHartView Answer on Stackoverflow
Solution 9 - Amazon Web-ServicesvishalView Answer on Stackoverflow
Solution 10 - Amazon Web-ServicesGhamgui KhaledView Answer on Stackoverflow
Solution 11 - Amazon Web-Servicesvarad11View Answer on Stackoverflow
Solution 12 - Amazon Web-ServicesObjectMatrixView Answer on Stackoverflow