Tutorial: Serverless Scheduled Tasks

Level: Beginner / Intermediate

Not all logic in web applications needs to be executed when the user hits the page. If you’d like to execute a task in your web application on a recurring basis, e.g. cancellation emails, recurring billing & archiving files, then you might want to use a ‘crontab’ — they’re invaluable for scheduling tasks.

In this tutorial we’ll be writing and deploying a function that runs every minute, without the need to provision a server. We’ll be using Node.js, AWS Lambda, CloudWatch Events, and orchestrate them using the Serverless Framework. You’ll need either a Mac, or a Linux machine, or even a Windows machine if you have bash installed.

What is Lambda?

Lambda is Amazon’s FaaS (Function-as-a-Service) offering. Code is sent to Amazon and it is executed when triggered. You’re billed in 100ms increments, and you get 1 million requests completely free each month. Triggers can be many different things, including HTTP requests (via API Gateway), S3 file events, and — as we’ll see in this example — scheduled CloudWatch Events.

Getting started

It’s assumed you have a basic understanding of the web to follow this tutorial. If you’re unfamiliar with Node.js or AWS, don’t worry!

You’ll need to download Node.js if you haven’t already, and have a suitable text editor — we recommend Atom as it’s free and easy to get started with.

See http://12factor.net/ for more info.

Why Serverless?

If you’ve ever used a crontab, you’ll know how useful they are for scheduling tasks. Some good usage examples in web applications are things like sending out cancellation emails, recurring billing, and archiving files.

Crontabs work alright when you just have the one box, but as you try and follow the principles of a 12-factor app, you start adding more web nodes. There’s often no obvious place to run your scheduled tasks.

In a 12-factor app boxes can be killed off or scaled up at any time. What ends up happening is you spin up this single ‘clock’ node that deals with the scheduling of tasks. Or you plonk it on another server which then accesses one of the load-balanced web nodes to do your bidding for you.

There’s a better way.

Serverless scheduling is an ideal solution and involves zero server management. You simply write some code, hand it over to Amazon programmatically, then they spin up a container for you on the specified schedule, run the code, and kill the container.

Amazon deal with everything from executing the code, right through to the hardware. There’s no single point of failure. Plus it’s just as easy to deploy to production — if not easier — than a crontab.

If you’d like to follow along with the code, it’s all available on GitHub.