TNS
VOXPOP
How has the recent turmoil within the OpenAI offices changed your plans to use GPT in a business process or product in 2024?
Increased uncertainty means we are more likely to evaluate alternative AI chatbots and LLMs.
0%
No change in plans, though we will keep an eye on the situation.
0%
With Sam Altman back in charge, we are more likely to go all-in with GPT and LLMs.
0%
What recent turmoil?
0%
Serverless

Move Your Cron Jobs to Serverless in 3 Steps

How to use the cron scheduler to manage your serverless jobs.
Sep 11th, 2018 9:00am by
Featued image for: Move Your Cron Jobs to Serverless in 3 Steps
Featured image via Pixabay.

Toby Fee
Toby is a community developer at Stackery. Her roles and experience combine working as a software engineer, writer and technology instructor, building interesting projects with emerging tools and sharing her findings with the world. Prior to joining Stackery, Toby was an engineer at NWEA, Vacasa and New Relic.

Cron jobs are often the night shift of our web applications. Some tasks just don’t make sense to do in real time or during peak usage, and need to be scheduled to happen periodically. Cron jobs have two notorious problems:

  • Relatively Opaque Configuration: Like Regex or SQL, cron has a compact and consistent syntax that, if you don’t know it, is almost impossible to read.
  • Observability: Once your team has multiple jobs scheduled across multiple servers, it can be very difficult to make sure that everyone knows which cron jobs need to be run, where they are, and how to move them to new systems.

Using Amazon Web Services (AWS) we could move all of our cron jobs inside an EC2 Linux server. This would at least make it easier to backup and clone the machine with all of our tasks, but we’re still in the position of having to know how to read a crontab file, and maintain a whole server just to run some code periodically.

This tutorial will show you how to schedule a Lambda function to run periodically, moving your scheduled tasks to serverless. You can use this technique to kick off DB maintenance, run nightly tasks or send regular reports to your team.

1. Make a Lambda Function

If you’d like to do this directly in AWS Start by creating a simple Lambda from within Amazon Web Services.

Our Lambda will be the simplest possible thing: a static log message. Scroll down to the code editor and enter the following few lines


After that, save your Lambda.

2. Recurring Cloudwatch Triggers

The next step is creating a new “rule” for Cloudwatch to regularly trigger our Lambda. From the Cloudwatch console, select “Rules>Create Rule.”

You can enter a crontab line here or select a simple interval from the drop-down.

Note that crontab offers a lot more than “every X minutes/hours/days,” and if you’re not familiar with the syntax, the crontab generator can help you write a rule such as “every Thursday every other month.” Select the Lambda we just created as our target. Once the rule is saved, we should see the Lambda invoked every X interval.

3. Seeing It in Action

To see this Lambda being exercised, we’ll need to check the logs since our Lambda sent no outbound traffic. Go to the AWS Lambda dashboard, and select the pertinent Lambda.

 

From there, select “Monitoring” and go to the log display.

 

The Fast and Simple Way

Here I must note: if you’d like to save yourself a great deal of time and clicking around AWS’ menus, Stackery greatly improves and simplifies the process down to two steps. Full disclosure: I work for Stackery and find this product feature to be amazing.

  1. Drag in a Lambda resource.
  2. Connect a timer. That’s all.

Further Reading

There in an official AWS tutorial on this topic covers similar material, and how to trigger the Lambda-based on other events. The AWS developer guide has additional use case resources if you’d like to learn more about getting started with serverless.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.