Can I limit concurrent invocations of an AWS Lambda?

Amazon Web-ServicesLockingAws Lambda

Amazon Web-Services Problem Overview


I have a Lambda function that’s triggered by a PUT to an S3 bucket.

I want to limit this Lambda function so that it’s only running one instance at a time – I don’t want two instances running concurrently.

I’ve had a look through the Lambda configuration and docs, but I can’t see anything obvious. I can about writing my own locking system, but it would be nice if this was already a solved problem.

How can I limit the number of concurrent invocations of a Lambda?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

Solution 2 - Amazon Web-Services

I would suggest you to use Kinesis Streams (or alternatively DynamoDB + DynamoDB Streams, which essentially have the same behavior).

You can see Kinesis Streams as as queue. The good part is that you can use a Kinesis Stream as a Trigger to you Lambda function. So anything that gets inserted into this queue will automatically be passed over to your function, in order. So you will be able to process those S3 events one by one, one Lambda execution after the other (one instance at a time).

In order to do that, you'll need to create a Lambda function with the simple purpose of getting S3 Events and putting them into a Kinesis Stream. Then you'll configure that Kinesis Stream as your Lambda Trigger.

Event Flow

When you configure the Kinesis Stream as your Lambda Trigger I suggest you to use the following configuration:

  • Batch size: 1
  • This means that your Lambda will be called with only one event from Kinesis. You can select a higher number and you'll get a list of events of that size (for example, if you want to process the last 10 events in one Lambda execution instead of 10 consecutive Lambda executions).
  • Starting position: Trim horizon
  • This means it'll behave as a queue (FIFO)

A bit more info on AWS May Webinar Series - Streaming Data Processing with Amazon Kinesis and AWS Lambda.

I hope this helps anyone with a similar problem.

P.S. Bear in mind that Kinesis Streams have their own pricing. Using DynamoDB + DynamoDB Streams might be cheaper (or even free due to the non-expiring Free Tier of DynamoDB).

Solution 3 - Amazon Web-Services

No, this is one of the things I'd really like to see Lambda support, but currently it does not. One of the problems is that if there were a lot of S3 PUT operations happening AWS would have to queue up all the Lambda invocations somehow, and there is currently no support for that.

If you built a locking mechanism into your Lambda function, what would you do with the requests you don't process due to a lock? Would you just throw those S3 notifications away?

The solution most people recommend is to have S3 send the notifications to an SQS queue, and then have your Lambda function scheduled to run periodically, like once a minute, and check if there is an item in the queue that needs to be processed.

Alternatively, have S3 send the notifications to SQS and just have a t2.nano EC2 instance with a single-threaded service polling the queue.

Solution 4 - Amazon Web-Services

I know this is an old thread, but I ran across it trying to figure out how to make sure my time sequenced SQS messages were processed in order coming out of a FIFO queue and not getting processed simultaneously/out-of-order via multiple Lambda threads running.

Per the documentation:

> For FIFO queues, Lambda sends messages to your function in the order > that it receives them. When you send a message to a FIFO queue, you > specify a message group ID. Amazon SQS ensures that messages in the > same group are delivered to Lambda in order. Lambda sorts the messages > into groups and sends only one batch at a time for a group. If your > function returns an error, the function attempts all retries on the > affected messages before Lambda receives additional messages from the > same group. > > Your function can scale in concurrency to the number of active message > groups.

Link: https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html

So essentially, as long as you use a FIFO queue and submit your messages that need to stay in sequence with the same MessageGroupID, SQS/Lambda automatically handles the sequencing without any additional settings necessary.

Solution 5 - Amazon Web-Services

Have the S3 "Put events" cause a message to be placed on the queue (instead of involving a lambda function). The message should contain a reference to the S3 object. Then SCHEDULE a lambda to "SHORT POLL the entire queue".

PS: S3 events can not trigger a Kinesis Stream... only SQS, SMS, Lambda (see http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html#supported-notification-destinations). Kinesis Stream are expensive and used for real-time event handling.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexwlchanView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesRobert ChenView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesdsaiztcView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesMark BView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesN. WalkerView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesWilliam ChoyView Answer on Stackoverflow