Articles

Serverless and Crontabless: Welcome CloudWatch Events and Lambdas


It's really common to have different jobs setup in cron for our applications. These are always needed in order to perform different tasks, either for cleaning up stuff, run reports, gather metrics, send alerts, and other types of periodic tasks.

The downside is that you need a server to run cron, and the associated cost of hosting and maintaining it. In this article we are going to see how we can replace cron jobs with AWS Lambda functions and CloudWatch.

CloudWatch events

CloudWatch is a monitoring service built in the AWS cloud. But it also offers a great tool, called CloudWatch Events. The cool thing is that these events can also be scheduled either to be triggered periodically at a given rate, or on specific days or hours of a week, or month, etc.

To trigger an event periodically, one can use the rate syntax, for example rate(5 minutes), and to specify exact hours, minutes, days, or months, the cron-like syntax can be used, like: cron(0 6 * * ? *)

Starting and Stopping EC2 instances with Lambda and CloudWatch Scheduled Events

As an example, we can use the lambda created in another article that can be used to start and stop EC2 instances.

To set this up, go to your CloudWatch console, and click on "Event" and then on "Create Rule". Then choose "Schedule".

At this point you can choose a fixed rate for execution (let's say, every 5 minutes), or enter a cron-like expression where you can specify for example specific hours (e.g: wednesdays, at 10am UTC).

You can find the syntax for both of these options at official Amazon documentation for Schedule Expressions for Rules

Then in the Targets section select "Lambda Function" and then in the drop down your lambda function. In this particular example, you can choose "Constant (JSON text)" in the "Configure Input" section, and enter something like

The JSON payload will be recogniced by the Lambda function and will start or stop the given instance.

Note that CloudWatch will need permissions to invoke your function, so your Lambda will need a Trust Policy like this one in its role, that allows execution to the events.amazonaws.com, like explained in Troubleshooting CloudWatch Events:

You can also setup CloudWatch alarms for your Lambda so you can monitor its activity.

That's it! Now your Lambda function can be executed by a scheduled CloudWatch event, and you can start thinking about replacing those cron jobs :)