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.
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.
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?
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).
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)