Set expiration of CloudWatch Log Group for Lambda Function

僤鯓⒐⒋嵵緔 提交于 2019-12-04 15:29:42

问题


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]]

回答1:


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.




回答2:


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.




回答3:


You can use Depends On in CloudFormation. Do something like this:

Resources:
LambdaFunction:
    Type: AWS::Serverless::Function
    DependsOn: LambdaLogGroup
    Properties:
      FunctionName: 'LambdaName'
      Handler: <handlerPath>
      Runtime: java8
      MemorySize: 512
LambdaLogGroup:
       Type: AWS::Logs::LogGroup
       Properties:
             LogGroupName: '/aws/lambda/LambdaLogGroup'
             RetentionInDays: 30


来源:https://stackoverflow.com/questions/45364967/set-expiration-of-cloudwatch-log-group-for-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!