🚀 Automating EC2 Underutilization Reports – Save Cloud Costs with a Bash Script

🔍 Introduction: In the world of cloud computing, it's easy to spin up resources—and just as easy to forget about them. One of the most common silent budget killers? Underutilized EC2 instances. You might be paying for compute power that's barely being used.
As part of a cloud operations or DevOps team, you've likely encountered EC2 instances running at 5–10% CPU and memory for weeks (or even months), quietly draining your AWS budget. Manually checking these instances is not just time-consuming, it’s also error-prone.
To tackle this, I created a Bash script that does all the heavy lifting: It fetches CPU and memory utilization from CloudWatch, analyzes your EC2 fleet, and emails you a color-coded HTML report with downsizing suggestions. It even includes a CSV attachment for spreadsheet fans.
Let me walk you through the problem, the benefits of automation, and how you can use this script to keep your AWS bills lean.
🧱 Challenges Faced in Manual Process:
Manually identifying underutilized instances is tedious and error-prone. Here's why:
Data Overload: CloudWatch has tons of metrics—you need to query the right ones and interpret them correctly.
Multiple Accounts & Regions: If you're managing resources across teams or environments, visibility becomes fragmented.
No Cost Context: Just seeing "CPU is low" isn't enough—you also need to map it to possible downsizing actions.
Tedious Reporting: Compiling instance IDs, types, metrics, and tags into a readable format every week is a time sink.
It was tedious, repetitive, and inefficient—especially at scale.
🌟 Benefits of the Script
Once I automated the process with ‘ec2_underutilized_report.sh‘, everything changed. Here’s what the script does:
✅ Fetches CPU and memory usage from CloudWatch for the last 15 days.
✅ Analyzes instances based on usage thresholds.
✅ Recommends smaller EC2 instance types (like moving from
m5.large
tom5.medium
).✅ Generates an HTML email with a color-coded table for easy reading.
✅ Attaches a CSV for detailed analysis or archival.
✅ Logs all operations for troubleshooting or audits.
Now, I get the full picture of EC2 health and waste, directly in my inbox—daily, weekly, or on demand.
📋 Prerequisites
To run the script, you need:
AWS CLI installed and configured (
aws configure
)CloudWatch Agent installed on EC2 instances (for memory usage)
IAM role or user with permissions:
ec2:DescribeInstances
ec2:DescribeTags
cloudwatch:GetMetricStatistics
Sendmail installed (for emailing HTML with attachments)
Basic Linux tools:
awk
,bc
,sed
, etc.
🛠 Implementation Steps
Here’s how to set it up:
Clone the GitHub Repo:
git clone https://github.com/alokshanhbti/ec2-underutilized-report cd ec2-underutilized-report
Make Script Executable:
chmod +x ec2_underutilized_report.sh
Configure Email Recipient and Region:
Inside the script:EMAIL_TO="your_email@example.com" AWS_REGION="ap-south-1"
Run the Script:
./ec2_underutilized_report.sh
Check Your Inbox 🎉
You'll receive a professional HTML report with:Instance IDs
CPU/Memory usage with visual badges
Instance types
Name tags
Downsizing recommendations
Log will look like below :
Automate It (Optional):
Add a cron job to run it weekly:0 8 * * 1 /path/to/ec2_underutilized_report.sh
✅ Conclusion
This script has been a game-changer for our DevOps and FinOps workflows. It gives us visibility into idle compute resources, actionable recommendations, and a smooth reporting experience—all with zero manual intervention.
By regularly monitoring your EC2 usage and acting on this report, you can significantly reduce your AWS spend and improve overall infrastructure efficiency.
Whether you're a cloud engineer, DevOps lead, or IT manager, this script can help you bring visibility, accountability, and savings to your AWS environment.
Feel free to check out the GitHub repository and try it in your own setup. I’d love feedback or pull requests to improve it further.
Subscribe to my newsletter
Read articles from Alok Shankar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Alok Shankar
Alok Shankar
Dedicated and highly skilled AWS DevOps and Linux professional with over 10+ years of experience in designing, implementing, and maintaining cloud infrastructure and CICD pipelines. Proficient in optimizing processes, automating workflows, and ensuring the reliability and scalability of cloud-based systems. Demonstrated expertise in Kubernetes and containerization technologies. Proven ability to understand and execute the complete deployment lifecycle. Proven expertise in real-time troubleshooting and leading cross functional teams to success.