DEV Community

Cover image for Amazon CloudWatch: Monitoring Your EC2 Instances
 Ganiyat Olagoke Adebayo
Ganiyat Olagoke Adebayo

Posted on

Amazon CloudWatch: Monitoring Your EC2 Instances

Step 1: Recap – Creating an Amazon EC2 Instance

Before we get into Amazon CloudWatch, let’s quickly recap how we created an EC2 instance, since CloudWatch monitors resources like EC2.

Step 1.1: Open the EC2 Dashboard

  • Log in to the AWS Management Console
  • Search for EC2
  • Click EC2 to open the dashboard


Step 1.2: Launch an EC2 Instance

  • Click Launch instance

  • Enter an instance name
  • Choose an Amazon Machine Image (AMI) (for example, Amazon Linux)
  • Select an instance type (e.g., t2.micro for free tier)

Step 1.3: Configure Key Pair and Security Group

  • Create or select a key pair
  • Configure a security group

  • Allow SSH (port 22)
  • Allow HTTP (port 80) if running a web app

Step 1.4: Launch and Verify

  • Click Launch instance

  • Wait for the instance status to show Running

At this point, your application is running on EC2.


Why Monitoring Is Important After Creating EC2

Once an EC2 instance is running, it’s important to understand how it performs. Without monitoring:

  • You won’t know when CPU usage is high
  • Performance issues can go unnoticed
  • Auto Scaling cannot work effectively

This leads us to Amazon CloudWatch.


Step 2: Introduction to Amazon CloudWatch

Amazon CloudWatch is a monitoring service that collects metrics, logs, and events from AWS resources. It helps you monitor EC2 performance and respond to changes automatically.


Step 3: Viewing EC2 Metrics in CloudWatch

Step 3.1: Open CloudWatch

  • Search for CloudWatch in the AWS console
  • Click CloudWatch


Step 3.2: View EC2 Metrics

  • Click Metrics

  • Select EC2

  • Choose Per-Instance Metrics

  • Select CPUUtilization

You’ll see a graph showing how your EC2 instance is performing.


Step 4: Create a CloudWatch Alarm for EC2

Step 4.1: Create Alarm

  • Click Alarms

  • Click Create alarm

  • Select specify metric and condition

  • Select EC2 CPU utilization


Step 4.2: Set Threshold

  • Condition: CPU ≥ 75%

  • Evaluation period: as preferred
  • Click Next

Step 4.4: Create Alarm

  • Name the alarm

  • Click Create alarm


Step 4.3: Configure Notifications

  • Create or select an SNS topic

  • Create or select an Subscription

  • Enter your email

  • Confirm the email subscription


Step: Testing the CloudWatch Alarm Using Stress

After creating the CloudWatch alarm, it’s important to confirm that it works correctly. To do this, we will intentionally increase the CPU usage on our EC2 instance and observe whether the alarm sends an email notification.


Step 1: Connect to Your EC2 Instance

  • Go to EC2 → Instances
  • Select your running instance
  • Click Connect
  • Connect using EC2 Instance Connect or SSH


You should now be logged into your EC2 instance terminal.


Step 2: Install the Stress Tool

The stress tool is used to simulate high CPU usage.

Run the command below:

sudo yum install stress -y

This installs the stress utility on Amazon Linux.


Step 3: Generate High CPU Load

Once installation is complete, run the following command:

stress --cpu 2 --timeout 300


What this command does:

  • --cpu 2 → Uses 2 CPU workers to increase CPU usage
  • --timeout 300 → Runs the stress test for 300 seconds (5 minutes)

This will push the CPU utilization above the threshold set in your CloudWatch alarm.

Step 4: Monitor CPU Usage in CloudWatch

  • Open Amazon CloudWatch
  • Go to Metrics → EC2 → Per-Instance Metrics
  • Select CPUUtilization

You should see a noticeable spike in CPU usage.

Step 5: Confirm the Alarm State

  • Go to CloudWatch → Alarms
  • Select your alarm
  • The alarm status should change from OK to In alarm


Step 6: Confirm Email Notification

  • Check the email address linked to your SNS topic
  • You should receive an alarm notification indicating high CPU usage

This confirms that your CloudWatch alarm is working correctly.


Step 7: Alarm Returns to OK State

Once the stress command finishes (after 300 seconds):

  • CPU usage will drop
  • The alarm will automatically return to the OK state
  • You may receive another email notification (depending on your alarm settings)

Why This Test Is Important

Testing the alarm confirms that:

  • CloudWatch is correctly monitoring your EC2 instance
  • Notifications are properly configured
  • Auto Scaling (if enabled) can respond to real workload changes

Tip

Always test your alarms after creating them. An untested alarm is as risky as having no alarm at all.


How CloudWatch Works with Auto Scaling

CloudWatch alarms can trigger Auto Scaling actions when thresholds are met. This allows AWS to automatically add or remove EC2 instances based on demand.


Summary

  • You created an EC2 instance
  • CloudWatch monitors its performance
  • Alarms notify you when something changes
  • Auto Scaling uses CloudWatch data to scale automatically

Conclusion

Amazon CloudWatch is essential for monitoring EC2 instances and supporting Auto Scaling decisions. By combining EC2, Auto Scaling, and CloudWatch, you create a reliable and scalable AWS environment.


You can’t scale what you can’t monitor.

Top comments (0)