42

I read here that I can set my Lambda function timeout for 15 minutes (https://aws.amazon.com/about-aws/whats-new/2018/10/aws-lambda-supports-functions-that-can-run-up-to-15-minutes/)

However, when I try to set API Gateway inside the Integration Request settings, it does not allow me to set it higher than 29seconds:

Timeout max is 29 seconds

How can I have a function that lasts 15 minutes, but a Gateway that time outs after 30 seconds?

Kamilski81
  • 14,409
  • 33
  • 108
  • 161
  • 1
    Just a note here, even if your lambda "times out" it will still finish whatever code is running. So your frontend user will get a timeout error but it will continue running. – Leander Feb 18 '22 at 13:30

6 Answers6

65

Unfortunately there isn't a way to increase the API Gateway timeout to longer than 29 seconds. This is a limitation of the gateway. The reason you can set the lambda function longer is because this can be plugged into other AWS resources that allow a higher threshold for timeout processing.

Here's some options you could explore to get around this and/or work with the limitation:

  1. Split your function out into smaller functions and chain those together to see if you get a performance increase. Before doing so you could use AWS X-Ray to debug the function and see what part is taking the most time to target what needs to be split out.

  2. Increase the memory used by the function. Higher memory allocation could result in faster execution. I have used this option before and was able to work around timeout limits.

  3. Instead of using API Gateway you could just use AWS SDK to call 'invoke()' which will invoke your lambda function. This will bypass the timeout threshold.

Hopefully one or a mix of those will help out :)

bspeagle
  • 827
  • 7
  • 7
33

API Gateway now has support to WebSocket APIs. Just:

  1. Create a websocket,
  2. call a lambda function through this socket, passing the connectionid to it. that lambda will then put a message in a SQS queue passing the connectionid
  3. the final lambda called by sqs to process the queue in an async event will perform its duties (up to 15min of processing), and then will use the connectionid to communicate to the client (browser) the results of the processing (just like a regular lambda would do through api gateway).

you can event check if the connection is still alive to interrupt processing (or send a message of progress back to the client).

Emerson Lopes
  • 331
  • 3
  • 2
  • 2
    Yo, thanks for the reply. Seems like a solution that could fit my use case. Got any guide that goes a bit more in depth? it's my first time using API gateway + websocket and it's quite confusing – wtfzambo Jul 06 '21 at 13:34
4

There's now a good alternative, which is to use AWS function URLs, which have no timeout (other than the lamdba itself). To quote from the AWS announcement (April 2022),

...sometimes all you need is a simple way to configure an HTTPS endpoint in front of your function without having to learn, configure, and operate additional services besides Lambda. For example, you might need to implement a webhook handler or a simple form validator that runs within an individual Lambda function.

Sure, it's simple - but its big value here is that it doesn't time out.

You just mark your lambda as requiring a function URL - either through the console or CLI or other build process - and AWS creates one, which then stays constant even as you update the function.

Here's the documentation: https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html

jfrank
  • 83
  • 4
  • 1
    This works for the most part, except when you need to make at least 7 requests to your lambda function from your browser. Chrome has a [6 concurrent request limit](https://stackoverflow.com/a/985704/4228422) for HTTP 1.1 and since Lamba function URLs use HTTP 1.1 the 6 request limit applies to it. – nirinsanity Feb 15 '23 at 07:33
2

Currently there is no way to increase the API timeout seconds. Another options is to create an ALB and forward request to Lambda.

Xing-Wei Lin
  • 329
  • 3
  • 5
  • 2
    https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html – Peter Mølgaard Pallesen Jan 27 '21 at 13:24
  • can you kindly clarify: what does ALB mean? could you share more details about this solution? for instance, what's the max timeout you achieved with this approach. thanks for your help! – Crashalot Mar 20 '21 at 00:24
  • ALB stands for Application Load Balancers. The max timeout can be found on this page https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html#connection-idle-timeout. – Xing-Wei Lin Mar 23 '21 at 07:04
  • @Xing-WeiLin I tried this, but didn't succeed. As ALB has payload limit of 1MB if lambda as target. https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html. Alternate solution we implemented is used EC2 as target, and hosted a flask app on it. Which invoke lambda function using boto SDK. – knownUnknown Aug 09 '22 at 09:48
0

Another option is to create an EventBridge timer with a quick API call and have the Lambda executed from the timer.

0

Just a quick note, to provide another possible workout :

Create a step function that will handle the job, then run this step function in your lambda, and provide a return url where you can check the state of the function (IN_PROGRESS, DONE, ERROR).

Than you can call back the url on the browser (ex : every 10 seconds) and when it's finished, get the data back.

Tibo
  • 621
  • 1
  • 8
  • 24