Creating a Lambda Warmer with AWS CDK and TypeScript

In this guide, we’ll create an AWS Lambda function that acts as a “warmer” to keep another Lambda function warm, reducing cold start times. We’ll use the AWS Cloud Development Kit (CDK) and TypeScript.

Prerequisites

Ensure you have the following installed:

  • AWS CLI
  • AWS CDK
  • Node.js and npm
  • Typescript

Step 1: Set Up Your AWS CDK Project

First, create a new directory for your project and navigate into it:

mkdir lambda-warmer
cd lambda-warmer

Initialize a new CDK project:

cdk init app --language typescript

Step 2: Install Dependencies

Install the necessary AWS CDK packages:

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

Step 3: Create the Lambda Functions

Let’s create two Lambda functions: one for the main function and one for the warmer.

Create the Main Lambda Function

In the lib directory, create a new directory named lambda and add a file called main-function.ts:

export const handler = async (event: any = {}): Promise<any> => {
console.log('Main Lambda function invoked!');
return {
statusCode: 200,
body: JSON.stringify({
message: 'Main Lambda function response',
}),
};
};

Create the Warmer Lambda Function

Create another file in the lib/lambda directory called warmer-function.ts:

import * as AWS from 'aws-sdk';

const lambda = new AWS.Lambda();

export const handler = async (event: any = {}): Promise<any> => {
console.log('Warmer Lambda function invoked!');

const params = {
FunctionName: 'MainFunction', // The name of the main Lambda function
InvocationType: 'Event', // Asynchronous invocation
};

await lambda.invoke(params).promise();

return {
statusCode: 200,
body: JSON.stringify({
message: 'Warmer Lambda function invoked the main function',
}),
};
};

Step 4: Define the Lambda Functions in CDK

Modify the lib/lambda-warmer-stack.ts file to include the definition of the Lambda functions and a CloudWatch Events rule to trigger the warmer function periodically.

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
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 { join } from 'path';

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

// Main Lambda function
const mainFunction = new lambda.Function(this, 'MainFunction', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'main-function.handler',
code: lambda.Code.fromAsset(join(__dirname, 'lambda')),
functionName: 'MainFunction',
});

// Warmer Lambda function
const warmerFunction = new lambda.Function(this, 'WarmerFunction', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'warmer-function.handler',
code: lambda.Code.fromAsset(join(__dirname, 'lambda')),
});

// Schedule to run the warmer function every 5 minutes
const rule = new events.Rule(this, 'Rule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(5)),
});

rule.addTarget(new targets.LambdaFunction(warmerFunction));
}
}

Step 5: Deploy the Stack

Deploy your stack using the following command:

cdk deploy

The CDK will synthesize your stack into CloudFormation templates and deploy the necessary resources to AWS. This will set up two Lambda functions and a CloudWatch Events rule to trigger the warmer function every 5 minutes.

Conclusion

You now have a Lambda warmer setup using AWS CDK and TypeScript. The warmer function will invoke the main Lambda function periodically, helping to keep it warm and reducing cold start latency. You can adjust the frequency of the warmer function by changing the CloudWatch Events rule schedule.



Categories: AWS

Tags: , , , , , , ,

Leave a comment