- Published on
How to create a scheduled AWS Lambda Function using the AWS CDK
- Authors
- Name
- Andreas Caldewei
In this blog post we will learn on how to create scheduled AWS Lambda Function using the AWS CDK.
Overview
What is a scheduled Lambda Function?
A Scheduled Lambda Function is as Lambda Function that is triggered at a specific time interval. Any AWS Lambda Function can be used for this purpose.
How does it work?
In addition to the Lambda Function, AWS Eventbridge is used to trigger the Lambda at specific time intervals. In this event bridge rule you can define the behavior when the Lambda Function should be triggered.
More information on how to configure such a rule can be found here:
What can you do with it?
This allows you to execute logic at specific times. To give some examples:
- Send an email every morning to the customer
- Web scraping
Example
This example gives an overview on how to implement a Scheduled Lambda Function in a CDK project.
Let´s start with a new CDK project.
// /lib/lambda-cron-stack.ts
import * as cdk from 'aws-cdk-lib'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as events from 'aws-cdk-lib/aws-events'
import * as targets from 'aws-cdk-lib/aws-events-targets'
import { Construct } from 'constructs'
import * as path from 'path'
export class CdkLambdaUrlsStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props)
}
}
In the code snippet above we see a new CDK project with all the necessary imports for this demo. Next, let´s write the code for a simple Lambda Function.
// /lib/lambda-cron-stack.ts
...
export class CdkLambdaUrlsStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props)
// 👇 Create lambda function
const lambdaFunction = new lambda.Function(this, 'lambda-function', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.main',
code: lambda.Code.fromAsset(path.join(__dirname, '/../lambda')),
})
}
}
In the code snippet above, a Lambda Function is instantiated and the location for the handler we specified. Next, let`s add the code for the handler.
// /lambda/index.js
async function main(event) {
console.log('Hello there 👋🏻')
}
module.exports = { main }
So the lambda is set up. Let´s set up the code for the scheduler.
// /lib/lambda-cron-stack.ts
...
export class LambdaCronStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
...
const cronRule = new events.Rule(this, 'CronRule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1))
})
cronRule.addTarget(new targets.LambdaFunction(cronLambdaFunction));
}
}
The code above creates a rule that triggers a Lambda Function every minute. In the example, a rate is used for fixed time intervals. For more dynamic time intervals a CRON expression can be used.
// /lib/lambda-cron-stack.ts
const cronRule = new events.Rule(this, 'CronRule', {
schedule: events.Schedule.cron({ minute: '0', hour: '1' }),
})
More documentation on that can be found here Schedule Expressions for Rules.
That´s everything we need to create scheduled AWS lambda Functions.
// /lib/lambda-cron-stack.ts
import * as cdk from 'aws-cdk-lib'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as events from 'aws-cdk-lib/aws-events'
import * as targets from 'aws-cdk-lib/aws-events-targets'
import { Construct } from 'constructs'
import * as path from 'path'
export class LambdaCronStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props)
const cronLambdaFunction = new lambda.Function(this, 'CronLambdaFunction', {
code: lambda.Code.fromAsset(path.join(__dirname, '/../lambda')),
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.main',
})
const cronRule = new events.Rule(this, 'CronRule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
})
cronRule.addTarget(new targets.LambdaFunction(cronLambdaFunction))
}
}