Multiple Uploads to S3 trigger the Lambda Multiple times

0

Hi, I want to run a script using lambda each time there is any change in s3 bucket. But I have noticed that when I upload multiple documents on s3, it triggers lambda that many times, but I want to trigger it once all the files are uploaded in that process.

Like if I am uploading 10 files to S3 at the same time I want the lambda to be triggered only after the 10th file is uploaded.

Is it possible !?

Thanks and Regards.

asked 4 months ago419 views
4 Answers
4

You can set an S3 event to generate messages on an SQS queue. Then use that queue to drive a Lambda function that uses it as its event source, specifying a batch time window. See https://aws.amazon.com/about-aws/whats-new/2020/11/aws-lambda-now-supports-batch-windows-of-up-to-5-minutes-for-functions/ for more information.

EXPERT
answered 4 months ago
AWS
EXPERT
reviewed 4 months ago
1

I agree with the SQS queue approach as well, you can buffer the S3 event notifications inside SQS queue and call lambda from SQS. Then somewhere in lambda you need to check the list of the files uploaded vs expected list then call business logic whatever you have.

profile pictureAWS
EXPERT
SriniV
answered 4 months ago
0

Hi,

The AWS Lambda function will be triggered for every object created in the Amazon S3 bucket. There is no capability to ask for the Lambda function to run only after 10 files (or any number) are uploaded.

You will need to add custom code to the Lambda function to determine whether it is 'ready' to trigger your work and the definition of 'ready' is up to you! Lambda trigger doesn't know your definition of ready. So, how could it wait for it?

Another way to go is the schedule the Lambda at regular time interval and have code in it to check if the files are to start working. If not, next schedule will do same until all files are here.

Best,

Didier

profile pictureAWS
EXPERT
answered 4 months ago
profile pictureAWS
EXPERT
iBehr
reviewed 4 months ago
0

First line of your function could count the number of objects with a particular prefix (~ files in a folder) and if it's fewer than 10 then exit.

profile picture
EXPERT
Steve_M
answered 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions