![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
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:
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.
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
exports.handler = async (event) => {
console.log('this is just a periodic reminder')
return {};
};
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.
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.
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.
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.