Serverless cronjobs with AWS Lambda and Events Rule

To setup the cron based job on AWS using serverless framework we need to create the events rule and give the permission to execute the AWS lambda function.

In many cases we may need to run the lambda function on a specific schedule. To achieve this on AWS cloud we need the following resources.

  • Lambda Function
  • CloudWatch Events Rule
  • Lambda Permission

As we will be using the serverless framework to configure the cron based lambda function we need to have basic knowledge of how serverless framework works. When we deploy the resources via serverless framework it creates the AWS CloudFormation template and create a zip file and sends it to the AWS S3 and then starts the deployment.

Note: AWS user need to have access to resources S3 Bucket, IAM Role

S3 Bucket

  • It will be used by the serverless framework to upload the deployment package .zip file

IAM Role

  • As with almost everything on AWS we’d need to manage permissions for our Lambda function with IAM. At the very least it should be able to write logs, so it needs access to CloudWatch Logs.

Lambda Function

  • It will execute the required functionality when it get’s triggered by the events rule or any other AWS resource.

CloudWatch Events Rule

  • CloudWatch Events supports cron-like expressions which we can use to define how often an event is created.

Lambda Permission

  • Unfortunately, we can’t trigger the lambda function from CloudWatch Events Rule unless we give lambda execution permission to CloudWatch Events Rule
  • Anything that wants to invoke a Lambda function needs to have explicit permission to do that.

Setting things up

Let’s make our hands dirty by creating the above use case that we discussed.

create the lambda function - helloWorld

handler.js

const helloWorld = (event, context, callback) => {
  console.log("Hello, world!")
  callback(null)
}
module.exports = {helloWorld}

configure the serverless.yml

Let’s create the serverless.yml and write the configuration.

serverless.yml

service: cronjob-service
provider:
  name: aws
  region: us-west-2
  runtime: nodejs14.10
functions:
  hello:
    handler: handler.helloWorld
    events:
      - schedule:
          rate: cron(*/5 * * * ? *)
          enabled: true

Other way to define it manually is like below

serverless.yml

service: cronjob-service
provider:
  name: aws
  region: us-west-2
  runtime: nodejs14.10
functions:
  helloWorld:
    name: helloWorld
    handler: handler.helloWorld
resources:
  Resources:
    CronJobTrigger:
      Type: AWS::Events::Rule
      Properties:
        Name: CronJobTrigger
        Description: trigger every 5 min
        ScheduleExpression: cron(*/5 * * * ? *)
        State: ENABLED
        Targets:
        - Id: CronJobTrigger
          Arn: arn:aws:lambda:us-west-2:${aws:accountId}:function:helloWorld
    LambdaInvokePermission:
      Type: AWS::Lambda::Permission
      Properties:
        FunctionName: helloWorld
        Action: lambda:InvokeFunction
        Principal: events.amazonaws.com
        SourceArn:
          Fn::GetAtt: [CronJobTrigger, Arn]
      DependsOn:
        - HelloWorldLambdaFunction # {title case of lambda function name}LambdaFunction

Above configuration will trigger the lambda for every 5 min. Now, let’s deploy it with serverless framework. To deploy it run the below command

serverless deploy

After successful deployment. Let’s debug the lambda function by checking it’s logs. Let’s see the logs with command serverless logs --function helloWorld --tail

It will give the logs something like below.

START RequestId: bb2cc533-11e7-a86d-a3f3-5ba627dcc6d6 Version: $LATEST
2022-02-21 14:00:22.173 (+02:00)        bb2cc533-11e7-a86d-a3f3-5ba627dcc6d6    Hello, world!
END RequestId: bb2cc533-11e7-a86d-a3f3-5ba627dcc6d6
REPORT RequestId: bb2cc533-11e7-a86d-a3f3-5ba627dcc6d6  Duration: 2.50 ms       Billed Duration: 100 ms

After verification remove the cloudformation stack with command serverless remove.

References:

  • https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
  • https://www.serverless.com/framework/docs/providers/aws/events/schedule

Thank you for reading the Agiliq blog. This article was written by Anjaneyulu Batta on Feb 21, 2022 in Serverless FrameworkAWS Lambda .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here