Controlling CloudWatch Log Retention Period When Using Serverless Framework on AWS

Controlling CloudWatch Log Retention Period When Using Serverless Framework on AWS

·

3 min read

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 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.png

Go to logs, select log groups: image.png

Click on the desired log group, then click on actions, and edit retention settings: image.png

Then you can select from a list of different dates: image.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: github.com/serverless/examples/tree/v3/aws-...

Run serverless deploy

image.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.png

It will always be the camelcase name of your function with LogGroup at the end image.png

You can add a log retention period by adding a resources section to your serverless.yml file:

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.png

You can confirm it on the AWS console as well.

image.png

Cleanup

Do not forget to delete the resources created if you deployed the serverless application sls remove

image.png

You can also use this pattern to edit other properties as you desire.