πΉ 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)