๐น Introduction :
Managing CloudWatch log groups is a critical part of maintaining operational efficiency and cost control in AWS. However, it's easy to overlook retention settings โ especially when log groups are created automatically by various AWS services. Without a defined retention period, logs accumulate indefinitely, leading to increased storage costs and unnecessary clutter.
In this blog, Iโll walk through streamlined approach to automatically detect CloudWatch log groups without a retention policy, update them to a 30-day retention period, and generate an HTML report delivered straight to your inbox.
The solution is powered by a simple Bash script that leverages the AWS CLI and standard Linux utilities โ making it easy to integrate into any DevOps workflow.
Whether you're a cloud engineer trying to stay compliant or just looking to reduce AWS costs, this automated approach will save time, improve visibility, and ensure consistent log management across your environment.
๐น Challenges Faced in Manual Process:
Manually managing log retention policies in AWS is like trying to clean every file cabinet in a skyscraperโpainful, slow, and error-prone. Some of the common problems:
โ You can't visually identify which logs lack retention
โ You have to click through each log group in the AWS Console
โ Thereโs no built-in notification when retention is missing
โ Risk of accumulating terabytes of unused logs
So I thought โ โWhy not automate the boring stuff?โ
๐น Benefits of Automating CloudWatch Retention Updates
Automating retention policies brings a whole bouquet of benefits:
๐ Cost Control โ Say goodbye to ever-growing log storage bills
๐ Audit Friendly โ Track what's changed, when, and how
๐ง Proactive Alerting โ Get email summaries with detailed tables
๐งน Cleaner Environment โ Consistent retention policies = better hygiene
โฑ๏ธ Time Saved โ No more manual clicking or forgetfulness
๐น Prerequisites
Before I dive in, make sure you have the following:
- - An AWS account with access to CloudWatch
- - IAM permissions to read and update log groups
- - AWS CLI configured on your machine
- - Bash shell environment (Linux or macOS)
- - Tools like jq, sendmail, mailutils installed
๐น Step 1: Install AWS CLI
If you havenโt installed the AWS CLI yet, follow the steps below:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
Then configure your credentials:
aws configure
๐น Step 2: Install Dependencies
Youโll also need jq and sendmail for parsing and email delivery:
sudo apt install jq mailutils -y
๐น Step 3: Create IAM Policy as per below , attached to IAM role and assign that role to EC2 instance.
Youโll need the following IAM permissions to make it work:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DescribeLogGroups",
"Effect": "Allow",
"Action": [
"logs:DescribeLogGroups",
"logs:DescribeLogStreams"
],
"Resource": "*"
},
{
"Sid": "PutRetentionPolicy",
"Effect": "Allow",
"Action": "logs:PutRetentionPolicy",
"Resource": "*"
},
{
"Sid": "CloudWatchMetricsAccess",
"Effect": "Allow",
"Action": "cloudwatch:GetMetricStatistics",
"Resource": "*"
}
]
}
Permissions include:
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:PutRetentionPolicy
- cloudwatch:GetMetricStatistics
๐น Step 4: Clone the GitHub Repository
Instead of writing the script manually, you can simply clone the prebuilt GitHub repository that includes the script, required IAM policy, and a README.
git clone https://github.com/alokshanhbti/cloudwatch-retention-update.git
cd cloudwatch-retention-update
Inside the folder, youโll find:
- cloudwatch-retention-update.sh โ The automation script
- iam-policy.json โ IAM policy required for permissions
- README.md โ Full documentation and usage instructions
๐น Step 5: Make the Script Executable
After saving the script, make it executable with:
chmod +x cloudwatch-retention-update.sh
๐น Step 6: Run the Script
Simply execute:
./cloudwatch-retention-update.sh
The script will log activity to a file, apply changes, and email the report to the address you specify.
๐น Step 7: Script Flow
Hereโs how the script works behind the scenes:
๐ Scan CloudWatch for log groups with no retention
๐ง Fetch metadata: log group name, retention, last event, service name, and storage
โ๏ธ Update retention to 30 days using put-retention-policy
๐จ Generate HTML email with two colorful tables:
Before update
After update
๐ฌ Send email via sendmail with all details
๐น Step 8: Screen shots of email and logs
Email part Before update :
Email part After update :
Logs :
๐น Conclusion
Automating CloudWatch log retention is a simple yet highly effective way to maintain a clean, cost-efficient, and compliant cloud environment. With this Bash script, you can easily identify log groups without retention settings, apply a consistent 30-day policy, and receive a well-formatted email report โ all with minimal effort and zero manual intervention.
This solution not only improves visibility and governance but also frees up your time to focus on higher-value tasks.
Thank you for reading!
If this script helps improve your cloud hygiene, feel free to share it with your team or contribute to the project.
๐ Access the GitHub Repository Here:
alokshanhbti
/
cloudwatch-retention-update
cloudwatch-retention-update repo that audits AWS CloudWatch log groups with no retention period and updates them to 30-day retention and sends email
๐ CloudWatch Log Retention Manager
cloudwatch-retention-update.sh
is a Bash script that audits AWS CloudWatch log groups with no retention period set, updates them to a 30-day retention, and sends a HTML email report containing color-coded tables.
๐ง Features
โ
Identifies log groups without retention
โ
Fetches last log date, associated AWS service, and storage usage (in GB)
โ
Applies a 30-day retention policy
โ
Sends an HTML email via sendmail
with:
- ๐ Before Update Table
- โ After Update Table
๐ Script Overview
- ๐ Log Group Scan โ Uses
aws logs describe-log-groups
andjq
to filter targets - โณ Retention Status โ Detects
null
retention policies - ๐
Last Log Timestamp โ Uses
describe-log-streams
- ๐พ Storage Usage (GB) โ Uses
cloudwatch:GetMetricStatistics
forStoredBytes
- ๐ง HTML Email Report โ Sends two HTML tables (before & after) with colors
๐ Usage
Step 1: Make it executable
chmod +x cloudwatch-retention-update.sh
Step
โฆHappy automating! ๐
Top comments (0)