94

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!

3
  • have you resolve this issue? Commented Jan 2, 2019 at 5:57
  • API Gateway and Lambda are designed for short runtimes. This is an intended limit by AWS. If you have a long runtime, such as running a scraper and having to send a response to client, then use ECS or EKS (Kubernetes) with an ALB. Commented Aug 1, 2020 at 4:02
  • @Donato can we use AWS SDK instead of API ? Commented Jan 27, 2021 at 13:53

16 Answers 16

47

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Shouldn't this be 29s?
@Ran why do you say it's not configurable? The document you linked to only says it cannot be increased, not that it cannot be configured. I assumed that means you can decrease it?
The answer is from 2016, a lot has chanced since. At the time it wasn't..
Unfortunately still 30s and unconfigurable in 2024.
This is now configurable! You can request an for an increase beyond the 29s default quota. 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 account-level throttle quota limit.
|
30

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.

6 Comments

Just FYI, the value may be set lower than 29s, not higher, unfortunately
Just to confirm, 29s is still the max value as of now.
@Abhishek Upadhyaya where is the documentation for this? According to docs.aws.amazon.com/apigateway/latest/developerguide/… Maximum integration timeout is 30 seconds.
@totsubo , you can search for "Integration timeout" - "50 milliseconds - 29 seconds for all integration types, including Lambda, Lambda proxy, HTTP, HTTP proxy, and AWS integrations." in the link (document) you shared. Its under the heading "API Gateway quotas for configuring and running a REST API".
@Abhishek Upadhyaya Even the HTTP quota says 30 seconds. docs.aws.amazon.com/apigateway/latest/developerguide/…
|
29

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: ['*'] } }) 

2 Comments

The problem with this approach is you lose all apig features like validation, request and response transformation, authorizer etc. Also, you cannot have WAF on these URLs making it vulnerable.
Yes if you rely on the api gateway ATM then sure. This is only applicable if you plan to handle this on your side.
14

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

6 Comments

Considering the limitations with lambda and the time it takes to spinup (which can easily exceed this timeout), I'm a bit surprised that this isn't configurable.
@CasualT: I know this is kind of old, but have you tried upping the amount of memory your Lambda function is configured for? Lambda functions scale their CPU based on the value you choose for memory. Counter-intuitive I know, but it works. Try bumping up to at least 1024MB and see if it doesn't help your startup lag.
The cold start behavior should be limited to the first few responses. Even if you're invoking the function once a minute or even once every 5 minutes, the function should stay warm.
It's 30 seconds now, not 10 -- shows 30 on the page you provided.
I for one am astonished by the tone-deaf response coming from AWS on this limitation. How many folks are moving their databases to cloud and take their first foray into AWS stack, write a VPC-lambda API only to run into this fine-print limitation well into development phase? Back to the drawing board because AWS refuses to allow longer requests. Its ridiculous if you ask me.
|
10

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/

1 Comment

As of 10/2018 lambda functions now have 15 min. aws.amazon.com/about-aws/whats-new/2018/10/…
5

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.

2 Comments

Retries are not a viable solution if your backend is already working the request. If this is a POST for example, retrying would send duplicate requests, without ever knowing the disposition of the initial one.
@BillGale You are not incorrect. The context being that any request you send should return a descriptive error if it fails. In which case one can trap and implement the appropriate logic on the client side to retry or not based on the error message/nature of the failure. This was speaking strictly from the sycn processing aspect as I was mentiong that refactor for async was not required specifically. For async processing of course you would have to reconsider your approach.
4

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

3 Comments

This answer is just a mix of information from other answers. Please avoid restating what has already been said.
Can you elaborate how this would work? How do you return a value to the API user?
When your backend being invoked to handle requests takes > 29s to spin up, the approach you advocate will not work, as the API Gateway will timeout at 29s and return a 504.
4

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.

Comments

3

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

Comments

2

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

Comments

2

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

Comments

2

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/

2 Comments

I've tried, and it's not clear what needs to be done. Just choosing 59000ms doesn't work. Do you understand what they mean by "but this might require a reduction in your account-level throttle quota limit. With this launch"??
@HenriqueMattos You have to submit a support request for the timeout increase. Your account level quote of 10000 requests p/s would go down.
2

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.

2 Comments

Anyone know the upper limit for this value?
@bmul From what AWS support answered me on a quota increase request, the hard limit (which is the limit that can be automatically approved is 180000 seconds or 3 minutes) for a quota beyond that, the Request Per Second at your account level will be affected, you can follow this formula assuming that you'd like a 300000s timeout New RPS = 10,000 / (Request Timeout / 29000) => 10000 / (300000/29000) => 966 RPS
1

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.

Comments

0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.