Configure Lambda Log Retention in AWS CDK

avatar
Borislav Hadzhiev

Last updated: Jan 26, 2024
3 min

banner

# Configuring Lambda Log Retention in AWS CDK

When a lambda function gets invoked for the first time, it creates a log group with log retention set to Never expire.

log group never expire

To configure a different log retention period for Lambda in CDK, we have to pass the logRetention prop to the Function construct.

The code for this article is available on GitHub
lib/cdk-starter-stack.ts
import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as logs from 'aws-cdk-lib/aws-logs'; import * as cdk from 'aws-cdk-lib'; import * as path from 'path'; export class CdkStarterStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); // ๐Ÿ‘‡ lambda function definition const lambdaFunction = new lambda.Function(this, 'lambda-function', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'index.main', code: lambda.Code.fromAsset(path.join(__dirname, '/../src/my-lambda')), // ๐Ÿ‘‡ set Log Retention in Days logRetention: logs.RetentionDays.ONE_DAY, }); } }

Let's go over what we did in the code sample.

  1. We created a lambda function by instantiating the Function class.
  2. We configured the logRetention property and set the log retention for the function's log group to be 1 day. The logRetention prop allows us to configure for how many days the logs of the function should be kept in CloudWatch.

For the purposes of this demo, the code of the Lambda function could be as simple as follows.

src/my-lambda/index.js
async function main(event) { return { body: JSON.stringify({message: 'SUCCESS ๐ŸŽ‰'}), statusCode: 200, }; } module.exports = {main};

Let's run the deploy command:

shell
npx aws-cdk deploy

In order to create the log group for a new lambda function, we have to invoke it once.

I'll invoke the function via the Lambda management console.

lambda function invoked

If we now open the Lambda function's log group, we can see that the log retention has been updated to 1 day.

lambda log retention updated

By setting the logRetention prop for our Lambda function, we created a custom CloudFormation resource.

The custom resource is responsible for:

  • Creating a log group for the function, if one doesn't already exist.
  • Updating the log retention period to the specified number of days.

If we look at the resources, our stack has provisioned, we can see that there are 2 lambda functions - ours and a custom resource, automatically created for us by CDK.

log retention custom resource

Removing the logRetention prop doesn't set the log retention period to the default of logs never getting deleted. In order to revert back to the default retention period, we have to set the logRetention prop of the function to Infinite.

In order to revert the log retention of a function to logs never expiring, set the logRetention prop to INFINITE.

lib/cdk-starter-stack.ts
import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as logs from 'aws-cdk-lib/aws-logs'; import * as cdk from 'aws-cdk-lib'; import * as path from 'path'; export class CdkStarterStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); const lambdaFunction = new lambda.Function(this, 'lambda-function', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'index.main', code: lambda.Code.fromAsset(path.join(__dirname, '/../src/my-lambda')), // ๐Ÿ‘‡ Revert the Log Retention to Infinite logRetention: logs.RetentionDays.INFINITE, }); } }

# Clean up

To delete the provisioned resources, issue the destroy command:

shell
npx aws-cdk destroy

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev