AWS lambda function that dynamically schedules its next runtime

Photo by Black Chitsulo on Unsplash

Have you ever scheduled a meeting in Gmail, Outlook or Calendly? I am sure you did 😀. 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. This is a classic example of a scheduled cron job. 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.

  1. Lambda function

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).

Okay, 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 expression.

Next we can create a new cloud-watch events with AWS SDK. We then can create a new cloud-watch rule with our expression that we created in the previous step.

You can also find all the available cloud-watch events method in the official API docs here

Before we run this we need to give our lambda function permission to Cloud-Watch. We can go into the lambda IAM manager and add permission to Cloud-Watch like shown below.

IAM

Now, once we run our lambda we will see that it creates a Cloud Watch event. Go to services > cloudwatch > events and you will see that it created a new event.

Cloudwatch events

The event is created and it will tick off exactly 30 minutes from now. However, we still have to add a target to this event. I am going to add our current lambda as a target so that the same lambda fires 30 minutes from now. We ca do this with the following code.

code difference

You can add any other lambda as a target as well. You can even add multiple AWS resources as a target.

Finally, we need to trust cloud watch service principle to invoke lambda functions. To do this I made the following changes to the code base.

So as you can see above are calling a function on line 32 to give our function a new permission. Observe the `attachPermission` function and you will notice that we are trusting our cloud-watch event to run this lambda function.

You can run more about this in the official SDK docs here.

In the IAM policy of lambda we need to add the following permissions. This will allow us to dynamically add permissions to lambda with using the aws cli.

And that’s about it. You have created a lambda that can self assign its next runtime.

One minor detail that we must keep in mind is that in our `attachTarget` function we should always generate a random string for `Id`. If our targets have same ids then it will cause unexpected behaviour.

With that let’s wrap this tutorial. I hope this article was helpful and gave you some new insights about lambda functions. Happy hacking 😀

Software Craftsman | Entrepreneur | Freelancer. Always on the look for opportunities to make the world a little bit better with technology.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store