Member-only story
Create an AWS Lambda Function that Dynamically Schedules its Next Runtime

Have you ever scheduled a meeting in Gmail, Outlook or Calendly? This is a classic example of a scheduled cron job.
Once you schedule a meeting and add participants it sends out invitations to everyone. Let’s say in this scenario some people accept the invite few don’t. People who accept the invitation of the meeting then get a reminder 30 minutes before the scheduled meeting time. They again get another reminder notification 5 minutes before the scheduled meeting time.
There are numerous applications of scheduled cron job such as the one above. In this article, we will build a scheduled cron job using AWS lambda and discuss how we can create a lambda function that can dynamically schedule its next runtime.
We will be using the following services provided by AWS for this exercise.
- Lambda function
- Cloudwatch Events
Lets create our lambda function
First of all, we will go ahead and create a simple lambda function (you can use a boilerplate or start from scratch).
The first order of business here is to figure out when we want to run the lambda next. Ideally we would write some business logic to determine this. However, for the sake of this tutorial we will assume that we want to run the lambda 30 minutes from its first runtime.
We first need to create a scheduled expression. This expression is a string value that can be passed into a cloud-watch event. To create an expression we will add the following code.
Once you add the code you can run your lambda function. To verify check the logs and see if it is printing out the correct…