Ludo
Ludo

Reputation: 2527

Workaround aws apigateway timeout with lambda - asynchronous processing

I have a serverless backend running on lambda. The runtime usually varies betweeen 40-250s which is over the apigateway max allowed runtime (29s). As such I think my only option is to resort to asynchronous processing. I get the idea behind it, but help online seems sparse and I'd like to know if there are any best practices out there? Or what would be the simplest way for me to get around this timeout problem–using asynchronous processing or other?

Upvotes: 0

Views: 1627

Answers (1)

Razvan
Razvan

Reputation: 2596

It really depends on your use case. But probably an asynchronous approach is best fitted for this scenario given that it's not usually a good idea from the calling side of your API to wait 250 seconds to get the reply back (probably that's why the 29s limitation on API Gateway).

Asynchronous simply means that you will be replying back from Lambda saying that you received the request and you are going to work on it but it will be available only later.

Then, you will be changing the logic on the client side, too, to check back after some time or perform some checks in a loop until the requested resource is ready.

Depending on what work needs to be done you could create an S3 bucket on the fly and reply back to the client with an S3 presigned URL. Then your worker will upload their results to the S3 bucket and the client will poll that bucket for the results until they are present.

Upvotes: 3

Related Questions