Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set expiration of CloudWatch Log Group for Lambda Function

By default when I create a Lambda function, the CloudWatch Log Group is set to Never Expire. Is it possible to set the expiration (saying 14 days) so I don't have to set it manually from the console after creation?


Updated#1

Thanks to @jens walter answer this is a code snippet of how to solve the problem

Resources:   LambdaFunction:     Type: AWS::Serverless::Function     Properties:       Handler: index.handler       Runtime: nodejs6.10       CodeUri: <your code uri>       Policies: <your policies>      LambdaFunctionLogGroup:     Type: "AWS::Logs::LogGroup"     DependsOn: "LambdaFunction"     Properties:        RetentionInDays: 14       LogGroupName: !Join ["", ["/aws/lambda/", !Ref LambdaFunction]] 
like image 219
niqui Avatar asked Jul 28 '17 04:07

niqui


People also ask

How do you change the retention period in CloudWatch logs?

You can change the log data retention setting for CloudWatch logs. By default, logs are kept indefinitely and never expire. You can adjust the retention policy for each log group, keeping the indefinite retention, or choosing a retention period between 10 years and one day.

Can CloudWatch logs trigger Lambda?

You can use a Lambda function to monitor and analyze logs from an Amazon CloudWatch Logs log stream. Create subscriptions for one or more log streams to invoke a function when logs are created or match an optional pattern. Use the function to send a notification or persist the log to a database or storage.

How do I integrate Cloudwatch Logs with Lambda?

Lambda automatically integrates with CloudWatch Logs and pushes all logs from your code to a CloudWatch Logs group associated with a Lambda function, which is named /aws/lambda/ <function name> .

How to change the expiration date of Cloudwatch Logs?

If you go to the CloudWatch console and view the Logs (CloudWatch > Log Groups), you will notice that data in the Expire Events After column are links. By clicking on one of those, you can change the expiration. I know that you can change the retention time after creation with the console.

How do I change the log retention time after creating Lambda?

You can actually change the log retention time after creating your Lambda in the console, but you need to do it from the CloudWatch console. If you go to the CloudWatch console and view the Logs (CloudWatch > Log Groups), you will notice that data in the Expire Events After column are links.

How do I view logs for lambda functions in AWS?

You can view logs for Lambda functions using the Lambda console, the CloudWatch console, the AWS Command Line Interface (AWS CLI), or the CloudWatch API. This page describes how to view logs using the Lambda console.


2 Answers

If you are creating your Lambda through the console, it is not possible to set the log retention accordingly. It is also not possible to set a default retention for all CloudWatch Logs.

The only way you can influence the log retention is through CloudFormation. In that case, you need to deploy you Lambda through CloudFormation and then you can define a matching LogGroup with a custom retention within that template.

like image 68
jens walter Avatar answered Sep 18 '22 04:09

jens walter


You can actually change the log retention time after creating your Lambda in the console, but you need to do it from the CloudWatch console.

If you go to the CloudWatch console and view the Logs (CloudWatch > Log Groups), you will notice that data in the Expire Events After column are links. By clicking on one of those, you can change the expiration.

like image 44
skresge Avatar answered Sep 18 '22 04:09

skresge