Last updated: Jan 26, 2024
Reading timeยท3 min
When a lambda function gets invoked for the first time, it creates a log group
with log retention set to Never expire
.
To configure a different log retention period for Lambda in CDK, we have to pass the logRetention prop to the Function construct.
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.
Function
class.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.
async function main(event) { return { body: JSON.stringify({message: 'SUCCESS ๐'}), statusCode: 200, }; } module.exports = {main};
Let's run the deploy
command:
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.
If we now open the Lambda function's log group, we can see that the log retention has been updated to 1 day.
By setting the logRetention
prop for our Lambda function, we created a custom
CloudFormation resource.
The custom resource is responsible for:
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.
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
.
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, }); } }
To delete the provisioned resources, issue the destroy
command:
npx aws-cdk destroy
You can learn more about the related topics by checking out the following tutorials: