Setting CloudWatch Rentention Policies in Serverless

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

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.