By using AWS re:Post, you agree to the Terms of Use

How to set CloudWatch logs retention policy for Lambda in AWS Serverless Application Model (SAM)?



I am using the AWS Serverless Application Model (SAM) to create an API consisting of an API gateway and a lambda function. By default, my lambda function is attached with an AWS managed policy to send logs to CloudWatch with no expiry date. How can I perform the following:

  1. Replace the AWS managed policy with my own customer managed policy on sending logs to CloudWatch?
  2. Implement a log retention policy of 30 days?

I noticed that AWS SAM comes with some policy templates that I can use but I did not find how I can answer my two questions.


1 Answers
Accepted Answer

Hi, @learning

You can simply create a LogGroup with the function name as shown below. Then you can set the log retention freely.

    Type: AWS::Serverless::Function
      FunctionName: test-func
      CodeUri: src/handlers/test_func
      Handler: index.handler
      Runtime: python3.6
      AutoPublishAlias: live
      Timeout: 10
      MemorySize: 128
    Type: AWS::Logs::LogGroup
      LogGroupName: !Sub /aws/lambda/${TestFunc}
      RetentionInDays: 14

profile picture
answered a month ago
  • Hi @iwasa, this worked like a charm! I knew it was something like this though I had to fix my indentation to get it working properly. Thanks!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions