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
- Sign in to the AWS Management Console and confirm you’re in the correct region.
- Navigate to EC2 → Instances and click Launch Instance.
- Under Name, enter
demo-instance
. Select Amazon Linux 2023 as your AMI. - 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.
- Keep the default VPC and subnet. Under Security groups, select Create security group with default rules.
- Click Launch instances, then view your instance on the Instances page. Wait until its status changes from Pending to Running.
2. Connect to the Instance
- Select your running instance and click Connect.
- Choose EC2 Instance Connect and then Connect again to open the browser-based shell.
- Switch to root:
[ec2-user@ip-172-31-16-22 ~]$ sudo su -
[root@ip-172-31-16-22 ~]#
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
Flag | Description | Example |
---|---|---|
--cpu | Number of CPU workers | --cpu 4 |
--io | Number of I/O workers | --io 4 |
--vm-bytes | Memory per VM worker | --vm-bytes 1G |
--hdd | Number of HDD workers | --hdd 2 |
4. View Metrics in CloudWatch
- Open the CloudWatch console in a new tab.
- Go to Alarms → Create an alarm.
- Click Select metric, navigate to EC2 → Per-Instance Metrics, and search your instance ID.
- Initially select CPU Credit Balance, CPU Credit Usage, and CPU Utilization, then set the graph to a 1-hour view to spot spikes.
- Because alarms support only one metric at a time, uncheck the credit metrics and keep CPU Utilization selected, then click Select metric.
5. Configure Alarm Conditions
- Under Threshold type, choose Static.
- Set Greater than or equal to 65 (%).
- Click Next.
Note
You can also use Anomaly detection for adaptive thresholds that adjust based on historical data.
6. Set Alarm Actions
- Under Select an SNS topic, choose an existing topic—e.g.,
CloudWatch-course-topic
. - This topic delivers email alerts to its subscribers.
- Click Next.
- Provide an alarm name (e.g.,
cpu-utilization-alarm
) and an optional message. Preview the email content. - Click Next, then Create alarm. Initially, the alarm shows Insufficient data; once metrics arrive and CPU ≥ 65%, it shifts to In alarm.
7. Verify Notifications
Check your email inbox for an alert:
Email contents include:
- Alarm name
- Instance details
- Custom message
- CloudWatch console link
- Triggering metric graph
8. Resolve the Alarm
- On the EC2 instance, stop
stress
withCtrl+C
. - 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