AWS CloudWatch

Anatomy of Alarms

Demo Creating and Configuring Alarms with alerting

In this hands-on tutorial, you’ll learn how to launch an EC2 instance, generate system load, and set up a CloudWatch alarm with SNS notifications. By the end, you’ll have:

  • A running EC2 instance under stress
  • A CPU utilization alarm
  • Email notifications on alarm trigger

1. Launch an EC2 Instance

  1. Sign in to the AWS Management Console and confirm you’re in the correct region.
  2. Navigate to EC2 → Instances and click Launch Instance.
  3. Under Name, enter demo-instance. Select Amazon Linux 2023 as your AMI.
  4. In Key pairs, choose Proceed without a key pair.

Warning

Launching without a key pair prevents SSH login via key-based authentication. You’ll connect using EC2 Instance Connect or Session Manager.

  1. Keep the default VPC and subnet. Under Security groups, select Create security group with default rules.
  2. Click Launch instances, then view your instance on the Instances page. Wait until its status changes from Pending to Running.

The image shows the AWS EC2 console interface for launching an instance, with options to configure the instance name, application and OS images, and other settings. The summary section on the right provides details about the selected configuration.

The image shows an AWS EC2 instance launch configuration page, detailing options for key pair login, network settings, and instance summary. The "Launch instance" button is visible at the bottom.

The image shows an AWS EC2 dashboard with an instance listed as "pending" in the Frankfurt region. The instance type is t2.micro, and there are no alarms set.

2. Connect to the Instance

  1. Select your running instance and click Connect.
  2. Choose EC2 Instance Connect and then Connect again to open the browser-based shell.
  3. Switch to root:
[ec2-user@ip-172-31-16-22 ~]$ sudo su -
[root@ip-172-31-16-22 ~]#

The image shows an AWS EC2 console interface for connecting to an instance, with options for EC2 Instance Connect, Session Manager, SSH client, and EC2 serial console. The instance ID and connection details are displayed.

3. Install and Run the stress Utility

CloudWatch doesn’t collect detailed CPU or memory load by default. To simulate load:

[root@ip-172-31-16-22 ~]# yum install stress -y

Once installed, launch stress:

[root@ip-172-31-16-22 ~]# stress --cpu 4 --io 4 --vm-bytes 1G --hdd 2
stress: info: [25930] dispatching hogs: 4 cpu, 4 io, 1 vm, 2 hdd

Use Ctrl+C to stop the load.

stress Command Options

FlagDescriptionExample
--cpuNumber of CPU workers--cpu 4
--ioNumber of I/O workers--io 4
--vm-bytesMemory per VM worker--vm-bytes 1G
--hddNumber of HDD workers--hdd 2

4. View Metrics in CloudWatch

  1. Open the CloudWatch console in a new tab.
  2. Go to Alarms → Create an alarm.

The image shows an AWS EC2 management console with details of a running instance, including monitoring metrics like CPU utilization and network activity.

The image shows the AWS CloudWatch dashboard, displaying options for setting alarms, creating dashboards, and monitoring logs and events. It includes navigation links on the left for various CloudWatch features.

  1. Click Select metric, navigate to EC2 → Per-Instance Metrics, and search your instance ID.
  2. Initially select CPU Credit Balance, CPU Credit Usage, and CPU Utilization, then set the graph to a 1-hour view to spot spikes.
  3. Because alarms support only one metric at a time, uncheck the credit metrics and keep CPU Utilization selected, then click Select metric.

The image shows an AWS CloudWatch interface displaying a graph of CPU utilization metrics over time, with options to select and view different metrics related to CPU performance.

5. Configure Alarm Conditions

  1. Under Threshold type, choose Static.
  2. Set Greater than or equal to 65 (%).
  3. Click Next.

Note

You can also use Anomaly detection for adaptive thresholds that adjust based on historical data.

The image shows an AWS CloudWatch interface for setting up an alarm condition based on CPU utilization. It includes options for threshold type, condition settings, and a graph displaying CPU usage data.

The image shows an AWS CloudWatch interface for setting up an alarm with anomaly detection for CPU utilization. It includes options for threshold types and conditions for triggering the alarm.

6. Set Alarm Actions

  1. Under Select an SNS topic, choose an existing topic—e.g., CloudWatch-course-topic.
  2. This topic delivers email alerts to its subscribers.
  3. Click Next.
  4. Provide an alarm name (e.g., cpu-utilization-alarm) and an optional message. Preview the email content.
  5. Click Next, then Create alarm. Initially, the alarm shows Insufficient data; once metrics arrive and CPU ≥ 65%, it shifts to In alarm.

The image shows an AWS CloudWatch interface where a user is configuring actions for an alarm, including setting notifications via SNS topics.

The image shows an AWS CloudWatch interface for creating an alarm, displaying a graph of CPU utilization with specified metric conditions. The alarm is set to trigger when the CPU utilization exceeds a certain threshold.

The image shows an AWS CloudWatch dashboard with an alarm named "cloudwatch-alarm-demo" in an "In alarm" state, indicating high CPU utilization. The graph displays CPU utilization over time, exceeding the threshold.

7. Verify Notifications

Check your email inbox for an alert:

The image shows an email notification from AWS CloudWatch indicating that a CPU utilization alarm for an EC2 instance in Frankfurt has been triggered due to a threshold breach.

Email contents include:

  • Alarm name
  • Instance details
  • Custom message
  • CloudWatch console link
  • Triggering metric graph

8. Resolve the Alarm

  1. On the EC2 instance, stop stress with Ctrl+C.
  2. After CPU drops below 65%, the alarm transitions to OK.

Note that CloudWatch doesn’t notify on resolution by default. To get recovery alerts, edit the alarm’s actions or create a second alarm for the OK state.

Summary

You’ve successfully:

  • Launched and connected to an EC2 instance
  • Installed and ran stress to generate CPU load
  • Navigated CloudWatch and created a CPU utilization alarm
  • Configured SNS notifications and verified delivery
  • Resolved and monitored alarm recovery

References

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Alarm States