AWS cloudWatch: Monitoring Your Resources Made Easy
Source: Dev.to
Overview
Amazon CloudWatch acts as a watchdog for your AWS resources. It collects metrics such as CPU utilization, memory usage, and network traffic, displays them on dashboards, and can trigger notifications when thresholds are breached.
Launch an EC2 Instance
-
Sign in to the AWS Management Console and search for EC2.
-
Click Launch instance.
-
Enter a name for the instance (e.g.,
BravoEC2). -
Scroll down to Key pair, give it a name, and click Create key pair.
-
Click Launch instance to finish.
-
After the instance is running, select it and click Connect → Connect to open an SSH session.
-
Switch to the root user:
sudo su -
View EC2 Metrics in CloudWatch
- In the console search bar, type CloudWatch and select it.
- Choose Metrics → All metrics → EC2 → Per‑instance metrics.
- Check the box next to CPUUtilization to display the metric on the graph.
Create an SNS Topic and Subscription
- Search for SNS (Simple Notification Service) and open it.
- Click Topics → Create topic.
- Choose Standard, give the topic a name (e.g.,
EC2-Alerts), and click Create topic. - With the new topic selected, click Create subscription.
- Set Protocol to Email, enter your email address in Endpoint, and click Create subscription.
- Check your inbox for the confirmation email from AWS and click the confirmation link.
Create a CloudWatch Alarm
-
Return to CloudWatch, then select Alarms → All alarms.
-
Click Create alarm → Select metric.
-
Navigate to EC2 → Per‑instance metrics, check CPUUtilization, and click Select metric.
-
Configure the alarm condition:
- Threshold type: Static
- Whenever CPUUtilization is
≥ 75%for 3 consecutive periods of 1 minute (total 3 minutes)
-
Click Next and choose the SNS topic you created earlier.
-
Click Next, give the alarm a name (e.g.,
High-CPU-Alarm), and click Create alarm.
Test the Alarm
-
Install the
stresstool on the instance (Amazon Linux example):sudo yum install -y stress -
Run a CPU stress test that exceeds the alarm threshold:
stress --cpu 2 --timeout 300This command loads two CPU cores for 5 minutes, pushing CPU utilization above 75%.
-
Verify that you receive an email notification indicating high CPU usage.
You have now set up CloudWatch monitoring, an SNS notification channel, and an alarm that alerts you when your EC2 instance experiences high CPU utilization.