How to Create a Scheduled Lambda in AWS CDK

On a recent project, we needed to create AWS lambda to run on a  schedule. We were able to do so using AWS’s CloudWatch service. The reason was that we needed a scheduled lambda to resolve pending transactions in our project. Being new to AWS and their services, I found some of their documentation to be a bit scattered and the CDK a bit hard to wrap my head around.

If you’re interested in learning more about scheduling an AWS lambda, I hope this tutorial will assist you. Now, I’ll show you a simplified version of our scheduled lambda in AWS CDK.

 

1. Create the lambda.

To start, we will need to install some packages for our lambda and Cloud Event Rules.

yarn add aws-cdk-lib

Next, we’ll import some functions from the CDK.


import {
   aws_events,
   aws_events_targets,
   aws_lambda,
   Duration,
} from "aws-cdk-lib";

Finally, we’ll create a new lambda in your stack and set a few common properties. Let’s take a look at them.

  • code: the source of the lambda function
  • handler: the function your lambda calls when invoked
  • runtime: runtime environment for the Lambda function
const myLamdba = new aws_lambda.Function(this, "MyLambda", {
      handler: "index.handler",
      runtime: aws_lambda.Runtime.NODEJS_16_X,
      code: aws_lambda.Code.fromAsset(
        path.join(__dirname, "my-lambda-handler")
      ),
    });

2. Create an event rule.

Now that we have a lambda, we will need to create an event rule that will run our lambda on a schedule. For this rule, we will specify the description, target, and schedule. Here’s what the rule will look like once we’re finished.


new aws_events.Rule(this,'my-lambda-rule',
      {
        description: "Description of the rule",
        targets: [
          new aws_events_targets.LambdaFunction(myLamdba),
        ],
        schedule: aws_events.Schedule.rate(Duration.minutes(10)),
      }
    );

First, we’ll set up the description of the rule. This can help you identify your rule when viewing CloudWatch logs.


  description: "Description of the rule"

Next, we set the rule’s target as our lambda.

 
targets: [
          new aws_events_targets.LambdaFunction(myLambda),
        ],

Last, we set the lambda’s execution schedule. Here, we’ll set the lambda to run every 10 minutes.


schedule: aws_events.Schedule.rate(Duration.minutes(10)),

Or you can use cron expressions to set its schedule.


schedule: aws_events.Schedule.cron({ minute: "10"})

Put it all together.

Here’s the final product.


const myLamdba = new aws_lambda.Function(this, "MyLambda", {
   handler: "index.handler",
   runtime: aws_lambda.Runtime.NODEJS_16_X,
   code: aws_lambda.Code.fromAsset(
        path.join(__dirname, "my-lambda-handler")
      ),
    });

new aws_events.Rule(this,'my-lambda-rule',{
   description: "Description of the rule",'
   targets: [new aws_events_targets.LambdaFunction(myLamdba)],
   schedule: aws_events.Schedule.rate(Duration.minutes(10)),
      }
    );

Running a Lambda on a Schedule

With the rule and lambda set up, this should allow you to run a lambda on a schedule. While scheduled lambdas are powerful, they are best used in automatically executing repeated tasks. In our case, we used it to resolve overdue pending transactions in our project. Learning about AWS’s CDK can feel pretty intimidating, so I hope this helps. Good luck!

Conversation

Join the conversation

Your email address will not be published. Required fields are marked *