DEV Community

Henry Williams
Henry Williams

Posted on

Creating a Serverless Cron Job in AWS

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)
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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

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')
}
Enter fullscreen mode Exit fullscreen mode

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
  2. Lambda function Lambda
  3. CloudWatch event to trigger the Lambda function CloudWatch trigger
  4. CloudWatch logs Logs

Conclusion

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

Top comments (3)

Collapse
 
valentinarodrigues profile image
Valentina Rodrigues

Can we use the same trigger to mulitple lambda function via serverless.yml configuration file

Collapse
 
cuongrep profile image
Kane Le

I think NO

Collapse
 
henryjw profile image
Henry Williams

Not sure I follow. Are you trying to trigger a chain of functions of on a schedule?