How to reduce Lambda log retention duration with a simple script for CloudWatch logs and save cost?
CloudWatch Logs is a managed service provided by AWS to monitor, store and access logs from various resources within the AWS ecosystem. The service provides a way to retain and manage logs over a specified period to comply with compliance requirements or manage storage costs.
By default, CloudWatch Logs retain logs indefinitely, which may result in high storage costs. To address this, you can set a retention policy for CloudWatch Logs to reduce the duration of log retention.
In this article, we will discuss how to reduce CloudWatch Logs retention duration created by Lambda using a bash script.
The Script
The script is a bash script that uses the AWS CLI to update the retention period for all log groups created by AWS Lambda to seven days and delete all logs older than seven days. You can customize the retention period to your desired duration by changing the value of the “retention-in-days” parameter.
How to use the script?
Install AWS CLI
The script uses the AWS CLI to interact with CloudWatch Logs. You need to have the AWS CLI installed on your machine before running the script.
You can install the AWS CLI using pip, the package installer for Python, by running the following command:
pip install awscli
Configure AWS CLI
After installing the AWS CLI, you need to configure it with your AWS credentials. You can configure the AWS CLI by running the following command and following the prompts:
aws configure
Save the script
Save the script to a file, for example, “reduce-logs-retention.sh”.
Make the script executable
Make the script executable by running the following command:
chmod +x reduce-logs-retention.sh
Run the script
Run the script by executing the following command:
./reduce-logs-retention.sh
Bonus Tip
Here’s a bonus tip on how to add the script to crontab to run once a day at midnight on a server.
Crontab is a utility in Unix-like operating systems used to schedule commands or scripts to run periodically at specified intervals.
To add the script to crontab to run once a day at midnight, follow these steps:
Open the crontab editor
To open the crontab editor, enter the following command in the terminal:
crontab -e
This will open the crontab file in the default text editor.
Add the script to crontab
Add the following line to the crontab file:
0 0 * * * /path/to/reduce-logs-retention.sh
This will run the script at midnight (0 hours and 0 minutes) every day. Replace “/path/to/reduce-logs-retention.sh” with the actual path to the script.
Save and exit crontab editor
Save and exit the crontab editor. The changes will take effect immediately.
Now the script will run automatically at midnight every day, reducing the retention duration of CloudWatch Logs and deleting old logs to manage storage costs and comply with compliance requirements.
Shameless Plug 🤪:
Hey everyone!
I’m excited to announce the launch of my first book, “Building Serverless Applications on AWS: A Step-by-Step Guide !”
You can get my book in multiple formats, including Kindle, paperback (both in India and internationally), and Gumroad.
So, whether you prefer a physical book or a digital copy, you can choose the format that works best for you.
If you wish to buy it, please use the link below:
About the book : https://bit.ly/about-my-book
Kindle : https://bit.ly/buy-kindle-version
Paperback India : https://bit.ly/buy-paperback-version-india
Gumroad : https://bit.ly/buy-on-gumroad
Paperback International : https://bit.ly/buy-paperback-version-international
I’m excited to hear what you think!