Creating a Serverless Cron Job in AWS
Henry Williams
Posted on March 28, 2019
Setting up a cron job in AWS is easy with the Serverless framework. For those unfamiliar with cron jobs, they're essentially scheduled jobs. For instance, you might have a cron job to remove old logs every 3 days.
For this quick tutorial, I'm going to assume you're at least somewhat familiar with Serverless and AWS lambdas. If you aren't, then stay tuned for a post on those :)
Please refer to this repo for the complete example.
Serverless configuration
Your serverless.yml
configuration file should look something like the one below.
service: cron-job-example
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: us-east-1
profile: serverless
functions:
hello-cron:
description: Example lambda function
handler: hello-cron.handler
events:
- schedule: rate(1 minute)
The magic happens under the events
definition. There we define a task to run every minute. You can change this depending on your needs.
You also have the option to set up a cron expression for more complex scheduling; for instance, setting a job that runs every Tuesday at 4:03 AM. Check out this AWS documentation if you want to explore these kinds of cron expressions. Note that they're a bit different than a traditional cron expression.
Setting up your environment
The environment setup is simple. All you need is a valid set of AWS credentials in your AWS config file (~/.aws/credentials
in *nix). Should look something like below
[serverless]
aws_access_key_id = <access key>
aws_secret_access_key = <secret access key>
The serverless
profile (the name between the brackets) is configured in serverless.yml
. This can be anything, but it must match the name specified in provider.profile
in your serverless.yml
config. For this example, I called it serverless
. Also, the profile you select should have sufficient access on your AWS account to deploy this stack.
After configuring the profile to use for the deployment, you'll need to install the dependencies by running npm install
.
Deploying the job
This is the easiest part. Once you've set everything up, simply run npm run sls:deploy
and Serverless will take care of the rest.
After deploying, you should be able to go to your CloudWatch logs and see the log entries generated by the Lambda function.
If you look at the code for the Lambda function in the example repo, you'll notice that the job itself doesn't do much. It just logs "hello cron" whenever executed. I'll leave it up to you to make it do something more useful :)
module.exports.handler = () => {
console.log('hello cron')
}
Wait, what?
There's no magic to how this all works. Although, Serverless does create a few AWS resources behind-the-scenes to support this functionality.
- CloudFormation stack
- Lambda function
- CloudWatch event to trigger the Lambda function
- CloudWatch logs
Conclusion
There you have it. With a simple Serverless setup you can have a scheduled job running in AWS in no time!
Posted on March 28, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 21, 2024