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
Go to logs, select log groups:
Click on the desired log group, then click on actions, and edit retention settings:
Then you can select from a list of different dates:
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.
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.
It will always be the camelcase name of your function with LogGroup
at the end
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.
You can confirm it on the AWS console as well.
Cleanup
Do not forget to delete the resources created if you deployed the serverless application sls remove
You can also use this pattern to edit other properties as you desire.
Top comments (1)
Thanks for sharing 🤝
Depending on use case doing this would be even easier, in serverless.yml