How to Keep AWS Lambda Warm using AWS CDK

Learn how to periodically trigger AWS Lambda to avoid cold starts

Utkarsha Bakshi
Geek Culture

--

In this short post, we will learn how to keep AWS Lambda warm by periodically triggering it. We will create an event rule to trigger the Lambda every minute. Keeping the Lambda warm will avoid cold starts and will drastically reduce the response times.

Let us get right into it and learn how it can be done.

TLDR

Here’s the short answer

import {
aws_events as events,
aws_events_targets as targets,
Duration
} from 'aws-cdk-lib';
const eventRule = new events.Rule(this, 'LambdaSchedule', {
schedule: events.Schedule.rate(Duration.minutes(1)),
});
eventRule.addTarget(new targets.LambdaFunction(myLambda))

Follow along, for the detailed tutorial.

Step 1: Set up a Lambda construct

In a previous post, we learned how to deploy a Python based Lambda function using AWS CDK.

Define a Lambda construct in your CDK stack.

import {
aws_lambda as lambda,
} from 'aws-cdk-lib';
const myLambda = new lambda.Function(this, 'MyLambda', {
runtime…

--

--