How many times Lambda will be triggered by S3 object creations?

I have created a lambda function that is triggered on object creation in a bucket. If multiple files are being uploaded, will the lambda be reinvoked or another instance of the lambda be created for each file uploaded.

Will this be any different if each file creation in the bucket is by another lambda and it takes some time to create each file?

Each event that occurs (or object created into the bucket in this example), the Lambda function will trigger. If multiple files are uploaded, then multiple separate instances of the lambda function will trigger and run in parallel. They are triggered asynchronously and will scale to the demand of objects being created.

Check out the documentation: https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html

Disclaimer: I am an employee of A Cloud Guru

Caveat: I believe AWS mention that if your bucket is versioned, uploads might cause S3 triggers to occur more than once per object.

Thanks. That was helpful.
I was trying to process all the files created, after the last file was created (they are sequential and number of files is variable) kind of like stitching a video back frame by frame. My notion was that in a batch upload, the last file is the only trigger.

“it depends”

after a Lambda invocation ends, the Lambda ‘container’ will likely remain running ‘for a while’ in order to let new invocations to run on a ‘pre-warmed’ Lambda…this eliminating the ‘cold-start penalty’

An individual Lambda ‘container’ will only be used by one active function at a time…so if you have a long-running Lambda, another invocation cannot also run on the same ‘container’, it will find another ‘idle’ container or spin up a new one

You’re trying to solve a “hard problem” in distributed systems - trying to process events in a certain order. Usually I get around this by only processing a batch of files once a manifest file is received. The manifest file has a list of the other files in a batch.

A lot of AWS services create manifest files by default when sending a batch of files to S3 - Redshift, Aurora, Athena, etc.

Be careful with your assumptions here. As the sibling comment here alludes to, there is no guarantee that lambdas are invoked in a particular order. If you upload files A, B, C, you the triggered lambdas could come through as A, C, B, so taking some kind of finalizer action when the C trigger comes through could be incorrect, but only sometimes.

You can add a filter to the object created events if you wanted to, are you able to indicate in the filename that it’s the last of the batch? Alternatively what if you had the events stream to dynamo, perhaps you could do something there to determine which file is last, perhaps something to do with timing?

Can you not use multipart uploads? Or do you need access to the blocks before all the blocks are uploaded?

You have the ability to set your lambda reserved concurrency as well if you wanted to limit the number on lambda instances that can be invoked. This reserved concurrency can be the remaining concurrency limit on your account less 100 (functions without reserved concurrency are allocated 100).

https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html

Do you know if the creation event is fired at the moment of first write or at the moment of full consistency/last write?

S3 is now immediately consistent as of December 2020, so once an object is written or updated you’re guaranteed to get the latest version on GETs

S3 is now immediately consistent as of December 2020, so once an object is written or updated you’re guaranteed to get the latest version on GETs

In addition to the other points, it’s important to note that S3 events are guaranteed at least once, not exactly once. So you should be looking to solve some of the issues mentioned in other replies regardless. You may well get events double delivered (I’ve had them delivered up to 30 minutes after the first delivery)

Is there a tool that schedules these types of operations?

You can use sqs to queue and batch upload notification events if the number of invokations is an issue. I haven’t done it myself but it looks doable. https://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html

This doesn’t work in practice. If S3 fires an event and it can’t trigger the Lambda, after a number of retries, your event isn’t processed.

Rather than worry about whether the same Lambda instance is called again or another instance is started, it’s more useful to think about

- the total number of invocations because this is what you’re charged for (according to mb and execution time for each invocation)

- whether the number of invocations at one lime will exceed the standard limit (default is 1000)

Let AWS handle the scaling for you.

unlike reddit :slight_smile:

Don’t forget making the script idempotent*