Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent an AWS lambda function from running more than once simultaneously?

I've got a lambda function and I would like to make sure that it's never being called a second time if it's already running. Is there any option to force this behavior?

like image 925
laurent Avatar asked Mar 31 '16 15:03

laurent


People also ask

Why is Lambda running multiple times?

Any event Executing Lambda several times is due to retry behavior of Lambda as specified in AWS document. Your code might raise an exception, time out, or run out of memory. The runtime executing your code might encounter an error and stop. You might run out concurrency and be throttled.

How do you avoid Lambda Retry?

To stop it from retrying, you should make sure that any error is handled and you tell Lambda that your invocation finished successfully by returning a non-error (or in Node, calling callback(null, <any>) . To do this you can enclose the entire body of your handler function with a try-catch.


1 Answers

AWS Lambda currently doesn't support this behavior. You could try using DynamoDB to create a lock, but that's obviously not the most ideal scenario. Right now this use case isn't a very good fit for Lambda.

I have a job that I need to ensure only one is ever running, and I wasn't happy with any Lambda solution for that. I ended up just running it on a t2.nano instance.

like image 170
Mark B Avatar answered Sep 28 '22 09:09

Mark B