Invoke AWS Lambda function only once, at a single

2020-05-26 15:02发布

I want to be able to set a time to invoke an AWS Lambda function, then have that function be invoked then and only then. For example, I want my Lambda function to run at 9:00pm on December 19th, 2017. I don't want it to repeat, I don't want it to invoke now, just at 9:00pm on the 19th.

I understand that CloudWatch provides Scheduled Events, and I was thinking that when a time to schedule this reminder for is inputted, a CloudWatch Scheduled Events is created to fire in that amount of time from now (so like if you schedule it at 8:22pm to run at 9pm, it’ll be 38 mins), then it invokes the Lambda function at 9pm which then deletes the CloudWatch Scheduled Event. My issue with this is that when a CloudWatch Scheduled Event is created, it executes right then, then at the specified interval.

Any other ideas would be appreciated, as I can't think of another solution. Thanks in advance!

4条回答
混吃等死
2楼-- · 2020-05-26 15:25

You can use DynamoDB TTL feature to implement this easily, simply do the following:

1- Put item with TTL, the exact time you want to execute or invoke a lambda function.

2- Configure DynamoDB Streams to trigger a lambda function on item's remove event.

Once the item/record is about to expire, your lambda will be invoked. you don't have to delete or cleanup anything as the item in dynamodb is already gone.

NOTE: However the approach is easy to implement and scales very well, but there's one precaution to mention; using DynamoDB TTL as a scheduling mechanism cannot guarantee exact time precision as there might be a delay. The scheduled tasks are executed couple of minutes behind.

查看更多
ら.Afraid
3楼-- · 2020-05-26 15:27

You can schedule lambda event using following syntax:

cron(Minutes Hours Day-of-month Month Day-of-week Year)

Note: All fields are required and time zone is UTC only

Please refer this AWS Documentation for Details.

Thanks

查看更多
一夜七次
4楼-- · 2020-05-26 15:36

If anyone gets here to trigger an event in a specific time and with specific data in the future, I found this very very helpful!!!

https://dev.to/byrro/how-to-schedule-any-task-with-aws-lambda-3617

查看更多
劳资没心,怎么记你
5楼-- · 2020-05-26 15:38

Invoking a lambda function via Events is asynchronous invocation option. By using CloudWatchEvent to trigger Lambda function we can use cron job but we are still facing issues as Lambda function triggered multiple times for the same cron schedule.PFB Link: https://cloudonaut.io/your-lambda-function-might-execute-twice-deal-with-it/

But this needs Dynamo DB to be implemented in your account and then make your Lambda function Idempotent.

查看更多
登录 后发表回答