Working with cron jobs are very important for most applications if it’s sending weekly reports or compiling daily analytics. If you are unfamiliar with autoscaling application and have a background with applications on a dedicated instance, your first thought is to add a cron to the server. Sadly, when you have autoscaling instances that does not work, any more than one instance will cause crons to run multiple times and cause havoc on your system.

Lambda Saves the Day

This is where AWS Lambda comes in. Rather than spinning up a dedicated server for only running crons, you can use a serverless product like Lambda, which will save you time and money. Lambda is an AWS product which lets you run code without provisioning or managing any servers. All that’s required is deploying a blob of code to Lambda and let AWS handle the rest! That’s great but how do you schedule the code to be run? Luckily AWS has Amazon CloudWatch Events which lets you create recurring events that will trigger your Lambda function.

How do I Actually Do This?

You can implement this by going to the Lambda dashboard and click the “Create function” button.

Once you’ve done that you will see two options to continue with, either create a function from scratch or use one of the blueprints AWS provides. For this example, let’s use a blueprint. Click the blueprint button.

You are now on the final page in the process. You will see a section with the heading “cloudwatch-events.” This is where we give the lambda function a schedule to run on! Click on the dropdown and select “Create a new rule.”

This will open up a form to create a schedule for your lambda function. Change the rule type to schedule expression then enter a schedule expression. Some documentation on schedule expressions is here.

Going to the Next Level

Creating the lambda and setting up the schedule from the AWS console this is great, but to take this to the next level, you can automate this whole process, and do this via various tools such as the AWS command line. The documentation related to lambda is here and for Amazon CloudWatch Events here. I hope this was informative for you and if you have any related questions feel free to leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *