Lambda Scheduled Events in CDK

05 August 2020 — Written by Edwin
#CDK#AWS#Typescript#lambda

Cron jobs can be really helpful in scheduling your Lambda function in a simple way. In this case I was looking for a batch job that is manipulating data in a DynamoDB table.

This is how you implement a Scheduled Event for Lambda in the aws-cdk.

Copy/Paste

import * as lambda from '@aws-cdk/aws-lambda'
import * as targets from '@aws-cdk/aws-events-targets'
import * as events from '@aws-cdk/aws-events'
import * as cdk from '@aws-cdk/core';

export class LambdaStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const newLambda = new lambda.Function(this, 'newLambda',{
      runtime: lambda.Runtime.PYTHON_3_8,
      code: lambda.Code.fromAsset('functions'),
      handler: 'index.handler',
    });
    const eventRule = new events.Rule(this, 'scheduleRule', {
      schedule: events.Schedule.cron({ minute: '0', hour: '1' }),
    });
    eventRule.addTarget(new targets.LambdaFunction(newLambda))
  }
}

Step by Step

In order for our Lambda to be triggered we need to configure a cron job that has access to our Lambda.

Lets install the two AWS-CDK modules that we need, make sure you install the same version as your other packages:

npm install @aws-cdk/aws-events-targets @aws-cdk/aws-events

Import the new events-targets & events packages into your stack.ts:

import * as targets from '@aws-cdk/aws-events-targets'
import * as events from '@aws-cdk/aws-events'

Next create the Lambda function, you don't have to specify anything special here.

    const newLambda = new lambda.Function(this, 'newLambda',{
      runtime: lambda.Runtime.PYTHON_3_8,
      code: lambda.Code.fromAsset('functions'),
      handler: 'index.handler',
    })

Now configure your cron rule with the frequency that you would like to execute your Lambda. In the next chapter you will find some tips on how to configure your schedule. This schedule starts at 1:00am UTC every day.

    const eventRule = new events.Rule(this, 'scheduleRule', {
      schedule: events.Schedule.cron({ minute: '0', hour: '1' }),
    });

Lastly we have to connect the Lambda function and the rule.

    eventRule.addTarget(new targets.LambdaFunction(newLambda))

Next I'll show you second way to do this. It just comes down to your preference what you use. Here you create the lambdaTarget and add it as a property in the evenRule.

    const newLambda = new lambda.Function(this, 'newLambda',{
      runtime: lambda.Runtime.PYTHON_3_8,
      code: lambda.Code.fromAsset('functions'),
      handler: 'index.handler',
    });
    const lambdaTarget = new targets.LambdaFunction(newLambda)    const eventRule = new events.Rule(this, 'scheduleRule', {
      schedule: events.Schedule.cron({ minute: '0', hour: '1' }),
      targets: [lambdaTarget]
    });

CRON

These cron jobs can trigger Lambdas with a minimum rate of once per minute and can be fine tuned to create expressions like "Run every week MON-FRI at 9:15pm".

Cron jobs are created in the UTC timezone and are using military time so 8pm is 20.

Here is the AWS documentation on the schedule expressions.

But If you want to use a generator for your expression, there are tons of online services help you to generate this. Online CronMaker.

Hope this helps and works for you! 🚀

© 2020 Built with ❤️