Back top top

Motivation

Running code at a regular interval is a pretty common task in software development. With such common use cases as:

  • manual backup of a database
  • cleaning up files on a regular interval
  • implementing a time to live (TTL) on resources
  • polling an API on a regular interval
  • re-subscribing to expired event notifications
  • sending emails on a regular basis

The first impulse for many developers that need to accomplish such tasks is to start writing a script of a language of their choice and then scheduling execution via the command-line tool, cron. Then, once the script gains uptime requirements, web hosting, daemonization, scaling, and continuous deployment all become concerns. These supporting tasks may end up becoming more work than coding up the actual work you are trying to get done in the first place.

Fortunately, the big players in cloud computing services have solutions aimed at reducing the boilerplate involved in running code in the cloud. These solutions are commonly referred to as Serverless. Serverless is one step beyond traditional PaaS offerings. With Serverless your code takes minimal time to "spin up" for the first time, only runs when invoked, is typically cheaper than persistent servers when those servers are underutilized, auto-scales, and requires less deployment environment configuration than with most PaaS setups.  

Scheduling Recurring Code Execution Through Serverless Framework

As Serverless services are a relatively new avenue of cloud code execution, there is not a widely used standard for shaping your serverless project. There are, however, a few frameworks focused on abstracting away the differences between Serverless cloud providers. Using one of these frameworks helps mitigate vendor lock-in.

Setup

This post will use the Serverless Framework to deploy an AWS Lambda function that executes Node.js code on a schedule.

Make sure you install Node.js and set up AWS credentials for your development environment. Commands in this tutorial will use the npx command-line utility which was introduced with npm version 5.2.0.

Code

Feel free to download and modify the git hub project for this tutorial.

All serverless framework projects start with a simple serverless.yml file that contains the basic configuration for your project.

After creating this configuration you'll want to create the entry point of your function, I named mine entrypoint.js. Note, exported scheduleFunction whose name matches the handler function name in our serverless.yml

Seververless2

Here is the ScheduleWorker class, with logging in every method to demonstrate that the method is called and in what order:

If you have downloaded the github project for this tutorial you will want to run npm install to install dependencies. Otherwise, at a minimum you will need to setup the serverless npm package as a dev-dependency by running npm install serverless --save-dev. In the github project I have setup linting and testing boiler plate by way of eslint and jest. I won't go into the intricacies of these setups, but you should be able to verify that files in the repo pass linting and test requirements by executing npm run lint and npm run test. You can test running our function locally through the serverless npm package by executing npx sls invoke local -f scheduleFunction. This should produce the following output:

Serverless4

Now we are ready to deploy our function, which can be done with npx sls deploy, which will print something similar to:

Serverless5

We can see the logs of our function execution in AWS by npx sls logs -f "scheduleFunction". However, if you execute this command before the first scheduled invocation of your function (as dictated by our cron expression on our serverless.yml file) you will get the error: No existing streams for the function. This is because AWS waits for the function to invoke once before creating the CloudWatch log streams that your function logs will be available on.

Finally, once you are done with your function you can delete all AWS resources allocated for the service you specified in your serverless.yml file by executing npx sls remove

Subscribe to our newsletter
Subscribe to our newsletter

Become a connected home expert.

Subscribe to our newsletter and get news and insights on the latest trends in residential IoT delivered to your inbox.

No spam!