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

Agent 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

CWAgent Metrics


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

CPU Alarm


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

Monitoring Dashboard


๐Ÿ’ก 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!


0
Subscribe to my newsletter

Read articles from pradeep mahadevaiah directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

pradeep mahadevaiah
pradeep mahadevaiah