You are here
Home > Tech >

How to automatically trigger AWS Lambda functions using CloudWatch

If you have AWS Lambda functions which need to be triggered periodically, like CRON jobs, there are many ways to achieve this. But I recently discovered a very easy and AWS-way of doing this, which makes life a lot easier.

So, there are a lot of ways you can trigger Lambda functions periodically. One of the most common ways I’ve see people doing this is adding an API Gateway to the Lambda function, and then calling that API periodically as a CRON job from one of the machines in the setup. I actually thought this is how you’re supposed do to that. Okay, let me make this clear. I’m not a DevOps guy. I just learn these things as and when the job requires me to. So the CloudWatch feature that I recently discovered is by no means a new feature introduced recently. It just so happens that I learned about it recently. So as always, I’m documenting the same here.

CloudWatch Rules with Schedules and Targets

So, to start off, let’s head over to CloudWatch and try to find the Events section in the left navigation pane. Once you find that, find the Rules sub-menu under that, because that’s where the magic happens.

Here, you can create a rule. This is something like the IFTTT (If This Then That) app. When a certain event happens, you can trigger some other event to happen in your AWS setup. For this example, we’re going to generate that source event periodically, using the built-in scheduler. So in the Event Source section, select the Schedule option, like this:

As you can see from the screenshot above, you have option to set the scheduler trigger an event at a fixed rate, in minutes, hours, or days; or provide an actual CRON expression, which is just awesome. For this example, you can select the option that suits your needs.

Next, to the right of the page, you can see the Targets section. Click the Add target button to add a target. You should see something like the following. But note that the selected function is one of mine, so you’ll not see that in your list.

One more thing to note is that the target Lambda function is a drop-down. Open up the menu and you’ll realise you can trigger a host of different services within AWS. You can pretty much do anything ranging from sending a notification using SNS to completely terminating one of your EC2 instances. For this example though, we’ll keep the default option, which is Lambda function.

From the next drop down menu, the Function menu, select the function which you want to trigger. If you want to trigger a particular version of the lambda, you can do that as well. Also, what’s more interesting, is the fact that you can configure the input as well. This comes in very handy sometimes. Maybe I’ll give an example of this in a future post.

But once you do this, you’re pretty much done. The function will be triggered at whatever frequency you specified earlier.

Sunny Srinidhi
Coding, reading, sleeping, listening, watching, potato. INDIAN. "If you don't have time to do it right, when will you have time to do it over?" - John Wooden
https://blog.contactsunny.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top