Setting CloudWatch Rentention Policies in Serverless

Serverless Framework Logo
Sharing is Caring

Serverless Framework is a great way of abstracting away a lot of the complexity behind the various cloud providers. Check out my blog post What is the Serverless Framework? for more details about serverless.

I often use the Serverless Framework as a great way to package up my lambda functions and deploy them consistently through continuous deployment.

The serverless default for CloudWatch Logs is to keep the logs forever. CloudWatch Logs are pretty expensive to keep around indefinitely. In the serverless.yml file, we’re able to set how long the cloudwatch logs are kept for. I recommend keeping them for at least 7 days but 30 days is probably more ideal.

To set the logs, we need to do two things.

In the provider section, we need to add “cfLogs: true”.

Down in our resources section, we need to add details for a LogGroup and set “RetentionInDays” to a quoted integer value. In the example below, I’ve set it to 7 which is the bare minimum that I recommend doing.

I don’t think it matters what the LogGroup is called only that it exists and has a quoted integer value.

Wrapping It Up

In this blog post, you’ve seen how to set the retention policy for CloudWatchlogs in AWS using the serverless framework.

Sharing is Caring