Controlling CloudWatch Log Retention Period When Using Serverless Framework on AWS
Tags: aws, serverless, lambda, cloud
Published: 2022-07-02 11:49:38

# The problem
When creating Lambda functions with the serverless framework, CloudWatch logs are created and have a log retention period of "never expire" by default. This can get very expensive as the months go by, and you are paying to store these CloudWatch logs. The price is about $0.03 per GB. For more details on CloudWatch pricing, please consult the documentation https://aws.amazon.com/cloudwatch/pricing/.

This can get very expensive for functions that receive a lot of traffic, and do you actually need gigabytes of lambda function log data from the last year? Depending on your use case, you probably do not.

# What can you do about it?
Some solutions to reduce the amount of money you're spending on logs are:

### Reduce the amount of data logged into CloudWatch
If your Lambda function has a bunch of unnecessary logging statements used for debugging, those are log entries that do not need to exist in production. You do not need console.log/print statements all over your function code to track execution. Only log when necessary

### Reduce the log retention period
#### Manual approach
If this is a log group for something created outside the serverless framework or CloudFormation, this can be done manually in the AWS console by navigating to CloudWatch
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4xmz5wz0zxck74mp285.png)
  
Go to logs, select log groups:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebf4u08rp9xrsu3m93kv.png)
 

Click on the desired log group, then click on actions, and edit retention settings:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12tyfmwpt31b8mtqucod.png) 


Then you can select from a list of different dates:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffk2x74xoyl5rl1l7244.png) 

#### Serverless framework approach
If you are using Serverless Framework, it becomes slightly less obvious because these are logs being created for your function automatically. They are not defined in your Serverless.yml file.
Let's run through an example using a simple HTTP serverless function source code: https://github.com/serverless/examples/tree/v3/aws-node-http-api.

Run `serverless deploy`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5q0n4o4dggt2tma43xz0.png) 

A hidden serverless folder is created, and if you check the `cloudformation-template-update-stack.json` file, you will notice there is a log group created for the function automatically.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wtibwkdwo6nmli7hfig.png) 
 
It will always be the camelcase name of your function with `LogGroup` at the end
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dbsofq6sas5820f3z6f.png) 
 
You can add a log retention period by adding a resources section to your serverless.yml file:

```yaml
service: aws-node-rest-api

frameworkVersion: "2"

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: "20201221"

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /
          method: get

resources:
  Resources:
    HelloLogGroup:
      Type: "AWS::Logs::LogGroup"
      Properties:
        RetentionInDays: "7"
```

After rerunning serverless deploy, you will notice a log retention period is added to the CloudFormation template.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ia5erxam17111pcua63.png) 

You can confirm it on the AWS console as well.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8qp75ezromqd9y6jbuu.png) 

# Cleanup
Do not forget to delete the resources created if you deployed the serverless application `sls remove`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xa8qufjhnnyforherxxr.png)
 
You can also use this pattern to edit other properties as you desire.