简体   繁体   中英

Invoke AWS-Lambda function only the first time that it's triggered in that day

I have an AWS lambda function that is triggered everytime someone uploads a file in a specified S3 Bucket, then it sends an e-mail notifying people that there are new files in the bucket, however, this can only happen at max one time everyday to avoid e-mail spawn when a lot of files are uploaded at the same time. Is there a way to limit the requisitions in the lambda function to ignore all new files uploads after the first one in that day?

It's in the nature of lambda functions that they do not store their own state, of course.

So you need to figure out how to save the state "do not send email until 2021-03-01 12:22 " or whatever timestamp is relevant.

You can write a small file with a name like .email-next-send-time into your S3 bucket along with your uploaded files. Into that file you can write one line, the value of

Date.now() + 24 * 60 * 60 * 1000  /* one day in milliseconds */

Then before you send the email from your lambda task, read that file from your S3 bucket. If it doesn't exist or if its contents are before Date.now() , send the email.

When sending the email, first write the file with a new value in it. (Doing it first will prevent almost every race condition if files are uploaded concurrently.)

You could also use a tiny redis.io instance (AWS calls it Elasticache), But that costs money.

If you only want to trigger an email once per day, in response to the first uploaded object of that day, then record the fact that you sent that day's email so you can test it later.

It would be trivial to do this to DynamoDB. The item's partition key would be the date. If you have multiple buckets where you need to do this then the key could be a composite of the bucket name and date.

This is probably better than other solutions like S3 because you can guarantee that only one Lambda invocation writes that item to DynamoDB (and hence only one email is sent).

All that said, is it acceptable that your users get an email when the first file is uploaded and then have no idea about subsequent uploads that happen later?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM