EC2 Monitoring and Alerts with CloudWatch


EC2 Monitoring and Alerts with AWS CloudWatch Agent, Alarms & Dashboards
Learn how to monitor an EC2 instance in real-time using Amazon CloudWatch Agent, create alarms for high CPU usage, and visualize everything with a custom dashboard.
๐งญ Introduction
For my fourth DevOps project, I focused on observability โ a crucial aspect of production readiness. This project demonstrates how to monitor an EC2 instance using CloudWatch Agent, create alarms, and build dashboards in AWS.
The result: a powerful, real-time monitoring setup with no third-party tools.
๐ GitHub Repo: EC2 CloudWatch Monitoring Project
๐ฏ Objectives
Monitor EC2 metrics: CPU, memory, disk usage
Use CloudWatch Agent to push system-level data
Create a CloudWatch Alarm for high CPU usage
Visualize with a CloudWatch Dashboard
(Optional) Send alarm notifications using SNS
๐งฑ Architecture
[EC2 Instance] ---> [CloudWatch Agent] ---> [CloudWatch Metrics]
โ---> [Logs / Alarms / Dashboards]
|
v
[Optional: SNS Alert]
โ๏ธ Setup and Configuration
Step 1: Launch EC2 Instance
Use Amazon Linux 2 AMI
Assign IAM Role:
CloudWatchAgentServerPolicy
Step 2: Install & Configure CloudWatch Agent
# SSH into the instance
scp cloudwatch-agent-config.json ec2-user@<EC2_PUBLIC_IP>:
scp cloudwatch-setup.sh ec2-user@<EC2_PUBLIC_IP>:
ssh ec2-user@<EC2_PUBLIC_IP>
chmod +x cloudwatch-setup.sh
sudo ./cloudwatch-setup.sh
๐ธ Screenshot: Agent installed and running
Step 3: Verify Metrics in CloudWatch
Go to CloudWatch โ Metrics โ CWAgent โ InstanceId
Check graphs for:
cpu_usage_idle
,cpu_usage_user
mem_used_percent
disk_used_percent
๐ธ Screenshot: CWAgent metrics showing live system data
Step 4: Create a CPU Utilization Alarm
Update your alarm-cpu.json
with the EC2 instance ID:
"Value": "i-xxxxxxxxxxxxxxxxx"
Then run:
aws cloudwatch put-metric-alarm --cli-input-json file://alarm-cpu.json
๐ธ Screenshot: Alarm creation and active state
Step 5: Build a Custom Dashboard
Go to CloudWatch โ Dashboards โ Create new
Add widgets:
Line chart: CPU % over time
Gauge: Memory used %
Text widget: alarm state
๐ธ Screenshot: Dashboard with live metrics
๐ก Optional Enhancements
Add SNS notification to the alarm (email/slack)
Stream logs to CloudWatch Logs for auditing
Export the whole setup as CloudFormation or Terraform
๐ GitHub Repository
๐ github.com/PradeepMahadevaiah/EC2_CloudWatch_Monitoring
Includes:
cloudwatch-agent-config.json
cloudwatch-setup.sh
alarm-cpu.json
README with full instructions
๐ง Key Learnings
โ
Hands-on with CloudWatch Agent configuration and metrics
โ
Created actionable alarms and visual dashboards
โ
Practiced AWS CLI for metric and alarm management
๐ Conclusion
Monitoring is a critical DevOps skill โ and CloudWatch makes it fully native to AWS. This project helped me solidify my understanding of infrastructure health, real-time metrics, and operational visibility.
Let me know your thoughts or feedback โ or fork the repo to try it yourself!
Subscribe to my newsletter
Read articles from pradeep mahadevaiah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
