Creating a Serverless Cron Job in AWS
Tags: serverless, aws
Published: 2019-03-28 23:45:21

---
title: Creating a Serverless Cron Job in AWS
published: true
description: This article provides a recipe for creating a cron job on AWS using the Serverless framework.
tags: serverless,aws
---

Setting up a cron job in AWS is easy with the [Serverless framework](https://serverless.com/). 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](https://github.com/henryjw/aws-serverless-cron-job-example) for the complete example.**


## Serverless configuration
Your `serverless.yml` configuration file should look something like the one below.

```yml
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](https://en.wikipedia.org/wiki/Cron#CRON_expression) for more complex scheduling; for instance, setting a job that runs every Tuesday at 4:03 AM. Check out [this AWS documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html) 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.
![Logs](https://github.com/henryjw/aws-serverless-cron-job-example/blob/master/docs/images/log-entries.png?raw=true)

If you look at the code for the Lambda function in the [example repo](https://github.com/henryjw/aws-serverless-cron-job-example), 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 :)

```javascript
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.

1. CloudFormation stack
![CloudFormation](https://github.com/henryjw/aws-serverless-cron-job-example/blob/master/docs/images/cloudformation.png?raw=true)
2. Lambda function
![Lambda](https://github.com/henryjw/aws-serverless-cron-job-example/blob/master/docs/images/lambda.png?raw=true)
3. CloudWatch event to trigger the Lambda function
![CloudWatch trigger](https://github.com/henryjw/aws-serverless-cron-job-example/blob/master/docs/images/cloudwatch-trigger.png?raw=true)
4. CloudWatch logs
![Logs](https://github.com/henryjw/aws-serverless-cron-job-example/blob/master/docs/images/logs.png?raw=true)

## Conclusion
There you have it. With a simple Serverless setup you can have a scheduled job running in AWS in no time!