Игорь Кирей
Игорь Кирей

Reputation: 923

Amazon API gateway timeout

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!

Upvotes: 92

Views: 188327

Answers (16)

PIoneer_2
PIoneer_2

Reputation: 496

As for the question, "a timeout error in 10 seconds" is a Lambda authorizer response timeout. API GW tries to call your authorizer in another lambda and hits the timeout. There is probably a point in optimizing the authorizer. Please, don't forget that "cold starts" could be applied to the authorizer as a regular Lambda.

There are ways to handle the request timeout at 30+ seconds. Please, keep in mind browsers do not expect such a timeout by default.

REST API GW (API GW v1)

As of Jan 2025, the task could be handled by the API GW reconfiguration.

You can't set the integration timeout to less than 50 milliseconds. You can raise the integration timeout to greater than 29 seconds for Regional APIs and private APIs, but this might require a reduction in your Region-level throttle quota limit for your account.

https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-execution-service-limits-table

HTTP API GW (API GW v2)

In these conditions, the task can not be solved in the API GW configuration. The timeout limit is 30 seconds and it can not be increased.

The solution requires another proxy (service) than API GW. For example, you could use an Application Load Balancer with your custom idle timeout value.

Hope, this will help.

Upvotes: 0

James Broad
James Broad

Reputation: 1280

To save you the time searching around, the service quota page to request a higher timeout is here: https://us-west-1.console.aws.amazon.com/servicequotas/home/services/apigateway/quotas/L-E5AE38E3.

Upvotes: 1

Ran
Ran

Reputation: 7709

The API Gateway timeout can, as of June 2024, be increased:

You can raise the integration timeout to greater than 29 seconds for Regional REST APIs and private REST APIs, but this might require a reduction in your account-level throttle quota limit

The default limit for Lambda invocation or HTTP integration is 29s according to https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html.

Upvotes: 46

Petar Nikov
Petar Nikov

Reputation: 1547

As of Jun 4 2024, it is now possible to raise the integration timeout for REST APIs /API Gateway v1/ to greater than 29 seconds for regional REST APIs and private REST APIs, but this might require a reduction in your account-level throttle quota limit ( 10000 requests p/s ) as per this AWS News.

To request a quota increase, you can use Service Quotas or contact the AWS Support Center

As of today, Jun 13 2024, the limit for "HTTP APIs" /API Gateway v2/ cannot be changed.

Upvotes: 2

node_saini
node_saini

Reputation: 812

30 seconds is a hard limit but if you are not relying too much on APIG features then another solution would be to use CloudFront with Lambda. In this case, you can get a timeout limit of up to 5 minutes.

  • You can use CloudFront functions as your custom authorizer or inbuilt the logic in your main lambda.

  • You can use the express framework for path-based routing inside your lambda.

  • You can also have path-based routing using CloudFront path behavior. In this case, you can either use the same lambda or a different
    lambda for each behavior.

  • You will be protected by WAF on CloudFront.

The last solution would be to switch to ECS.

Update 2024 - You can raise the integration timeout to greater than 29 seconds for Regional REST APIs and private REST APIs, but this might require a reduction in your account-level throttle quota limit.

https://aws.amazon.com/about-aws/whats-new/2024/06/amazon-api-gateway-integration-timeout-limit-29-seconds/

Upvotes: 2

joarleymoraes
joarleymoraes

Reputation: 1949

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://web.archive.org/web/20231006022031/https://joarleymoraes.com/serverless-long-running-http-requests/

Upvotes: 10

Capaj
Capaj

Reputation: 4216

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

For example this is how you define a function with the http endpoint using aws-cdk and typescript:

 const backendApi = new lambda.Function(this, 'backend-api', {
      memorySize: 512,
      timeout: cdk.Duration.seconds(40),
      runtime: lambda.Runtime.NODEJS_16_X,
      architecture: Architecture.ARM_64,
      handler: 'lambda.handler',
      code: lambda.Code.fromAsset(path.join(__dirname, '../dist')),
      environment: {
        ...parsedDotenv
      }
    })

    backendApi.addFunctionUrl({
      authType: lambda.FunctionUrlAuthType.NONE,
      cors: {
        // Allow this to be called from websites on https://example.com.
        // Can also be ['*'] to allow all domain.
        allowedOrigins: ['*']
      }
    })

Upvotes: 29

ObjectMatrix
ObjectMatrix

Reputation: 335

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

Upvotes: 0

varad_s
varad_s

Reputation: 1192

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.

Upvotes: 4

Ghamgui Khaled
Ghamgui Khaled

Reputation: 434

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

Upvotes: 2

vishal
vishal

Reputation: 1874

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

Upvotes: 3

NoPathInParticular
NoPathInParticular

Reputation: 383

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.

Upvotes: 5

Mitayshh Dhaggai
Mitayshh Dhaggai

Reputation: 89

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

Upvotes: 4

myouji
myouji

Reputation: 366

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.

Upvotes: 29

CamHart
CamHart

Reputation: 4335

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: Can an AWS Lambda function call another AWS Documentation: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html

Upvotes: 2

E.J. Brennan
E.J. Brennan

Reputation: 46879

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

Upvotes: 14

Related Questions