Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically schedule an AWS lambda function to run X amount of time after an event?

I am looking to schedule an AWS Lambda Function 24 hours after a specific event.

For example: if something is added to my database, 24 hours after that time, the AWS Lambda Function should run with specific parameters. This can be done with a function call to set the Lambda to run in 24 hours, or API call, (it does not have to be a webhook).

I have crawled the AWS documentation and cannot seem to find a solution.

I know it is possible to run the AWS Lambda Function on a cron schedule, but rather looking for something that can schedule it dynamically on a random basis.

Thanks so much!

like image 539
Ryan Avatar asked Dec 24 '22 00:12

Ryan


1 Answers

You can Schedule Amazon CloudWatch Events, such that it can run an AWS Lambda function at a particular time. However, I'm not sure whether you would be able to pass specific parameters to it.

A better approach is probably to keep a list of events in a database (eg DynamoDB) and trigger a Lambda function every n minutes. The Lambda function could consult the DynamoDB table for the earlier event that needs to be run. If that earliest even is in the future, the function can simply exit. If the earliest event is due now (or in the past), it can call another Lambda function to do as you wish (with parameters pulled from the DynamoDB table).

So, you'd be using CloudWatch Events as the timer and the Lambda function as the logical "when to do what" controller.

like image 135
John Rotenstein Avatar answered Jan 05 '23 14:01

John Rotenstein