How to Define a Log Retention in Lambda Using CDK Without Creating a Custom Resource — It can help you save 💲Money💲

Sahil Gupta
3 min readMay 29, 2023

--

Why Log Retention is necessary?

  • Log retention is the process of keeping logs for a certain period of time. Whenever a service is utilized / consumed / invoked (Lambda function), it generates logs that provide valuable insights into the execution flow and any potential errors. By default, these logs are stored indefinitely, which can quickly consume storage space and increase costs.

Defining a Log Retention in Lambda construct Vs Defining a Log Retention as a method

Defining a Log Retention in Lambda Function construct

  • When you create a Lambda function using the AWS CDK, you can define a log retention policy by setting the logRetention property on the Function construct. The logRetention property takes a value in days/weeks/months/years. For example, to set the log retention period to 1 day, you would use the following code:
const myFunction = new lambda.Function(this, 'My-Function', {
functionName: "Hello-World",
runtime: lambda.Runtime.NODEJS_16_X,
handler: 'index.main',
code: lambda.Code.fromAsset(path.join(__dirname, '/../src')),
// 👇 set Log Retention in Days
logRetention: logs.RetentionDays.ONE_DAY
})

If we try deploying the above code, it will result in creating two lambda functions, one for our main function and another for managing log retention.

Here’s an overview of it.

With Log Retention Policy in Lambda Function Construct

This default behavior of creating a custom resource for log retention introduces additional costs.

Defining a Log Retention as a method

  • The AWS CDK now provides a way to define a log retention policy without creating a custom resource. To do this, you can use the logRetention property on the LogGroup construct. For example, to set the log retention period to 1 day, you would use the following steps:
  1. Setting Up CDK: Install and configure CDK in your development environment.
  2. Defining the Log Group: Create a separate method to define the log group with the desired retention period, using the AWS CloudWatch Logs library available in CDK.
  3. Associating the Log Group: Associate the log group with your Lambda function, ensuring that the logs generated by the function are stored in the specified log group.
  4. Deploying the Stack: Use CDK commands to deploy the stack and apply the log retention configuration to your Lambda function.
export class LogRetentionStack extends cdk.Stack 
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const myFunctiontwo = new lambda.Function(this, 'My-Function-two', {
functionName: "Hello-World-two",
runtime: lambda.Runtime.NODEJS_16_X,
handler: 'index.main',
code: lambda.Code.fromAsset(path.join(__dirname, '/../src')),
})
// Call the static method to create a Log group for the second Lambda function
LogRetentionStack.createLogGroup(this, myFunctiontwo)
}

private static createLogGroup(
scope: Construct,
myFunctiontwo: lambda.Function
) {
new logs.LogGroup(scope, 'LogGroup', {
logGroupName: `/aws/lambda/${myFunctiontwo.functionName}`,
retention: logs.RetentionDays.ONE_DAY,
removalPolicy: cdk.RemovalPolicy.DESTROY
});
}
}

After deploying the above stack, the results are below.

With Log Retention method

The Log group created by both the functions are shown.

PS: I am always eager to learn and grow. If you have any suggestions or improvements, please share them with me. I am always open to new ideas and perspectives.

--

--

Sahil Gupta
Sahil Gupta

Written by Sahil Gupta

DevOps Engineer - AWS | Cloud | SRE I help people DEVELOP their AWS Project for FREE.

Responses (1)