Schedule AWS Lambda Invocations With EventBridge and AWS CDK

Nick Ramkissoon
Geek Culture
Published in
4 min readSep 8, 2021
Photo by ian dooley on Unsplash

AWS Lambda is great for running jobs that do not require a server running 24/7. A Lambda function can be invoked by events, other AWS services, the AWS CLI, and much more, making it incredibly versatile and easy to integrate with.

A common use case for Lambda is needing to run a function every x period of time. For example, suppose we want to scrape a website every 24 hours for new content, we can schedule a Lambda function to do exactly that using AWS EventBridge Rules. Scheduling Lambdas can run any regular, periodic workloads. In fact, this is how I implement website uptime monitors for my monitoring and alerting service, Komonitor.

In this tutorial, we’ll use AWS CDK to define a simple Lambda function that will be invoked every 5 minutes by an AWS EventBridge Rule.

This tutorial is for AWS/AWS CDK beginners/intermediates.

Prerequisites

  1. An AWS account
  2. Have the AWS CDK CLI installed and configured. Follow these setup instructions from AWS
  3. A basic understanding of CDK Stacks and Constructs.

Project Setup + Follow-Along Repository

First, we need to setup a CDK project.

mkdir <PROJECT_NAME>cd <PROJECT_NAME>cdk initnpm install @aws-cdk/aws-events-targets @aws-cdk/aws-events @aws-cdk/aws-lambdarm -rf test/mkdir lib/lambda-code && touch lib/lambda-code/code.jsmkdir lib/constructs && cd lib/constructs && touch lambda-construct.ts event-construct.ts

The above commands will initialize a new CDK project in the PROJECT_NAME directory and add a few files we will be using to write our Lambda and Event constructs and Lambda code.

I have created a public GitHub repository with all of the code we will be writing here so that you can clone it or follow along.

GitHub repository

Creating a Simple Lambda Function

Let’s define our Lambda function code in code.js:

This handler simply logs the event that the function receives and then returns a ‘Hello World’ message.

Now, let’s define the Lambda function that will use this code in lambda-construct.ts.

The above code creates a new CDK Construct that contains our Lambda function. In the class constructor, we create a new Lambda function that simple prints ‘Hello World’ on every invocation. The SimpleLambdaConstruct class exposes the Lambda function to the outside world in the class variable this.lambda.

Creating an AWS EventBridge Rule and Integrating Lambda

Now, let’s implement our Event Rule Construct in event-rule.ts.

The above code defines an Event Rule that will trigger an event every 5 minutes. However, we need to send this event to our Lambda function, which currently does not know our Event Rule exists. This is the last step we need to do.

In lambda-schedule-stack.ts, we instantiate our lambda and event rule constructs:

In order to have our Event Rule send events and invoke our Lambda, we need to add the Lambda function as a target to the rule. Then, we give the rule permission to invoke the Lambda function.

CDK Deploy + Verifying Everything Works

We can now try to deploy our CDK Stack to AWS.

cdk synth
cdk deploy

It will take a few minutes for all of the AWS resources to be created and ready to use. Once that is over and you see no errors, you can log in to your AWS account and view the resources created to verify everything is working.

First, let’s check out the EventBridge console to view our new Rule:

Event Rule

If we go the Lambda console we will see our lambda function, with our EventBridge trigger and handler code:

Lambda function

Finally, let’s see if our Lambda is getting invoked every 5 minutes. If you go to the **Monitor** tab in the Lambda console, you will see links to CloudWatch logs for the function’s invocations. Of course, you’ll have to wait at least 5 minutes for anything to show up in the logs, however, once the function has been invoked, the logs will look something like this:

Lambda invocation logs in CloudWatch

We see EventBridge sending “Hello Lambda” to the function every 5 minutes. Success!

Cleanup + Conclusion

To avoid leaving around unused resources in our AWS account, we can remove the CDK stack and associated resources with:

cdk destroy

That’s it! Now you know how to run any Lambda function on a set schedule using AWS EventBridge Rules. Better yet, you can now use AWS CDK to define you resources instead of manually created them in the console.

--

--

Nick Ramkissoon
Geek Culture

Software engineer that loves to educate and help people.