Guided Lab: Creating a CloudWatch Alarm
Description
AWS CloudWatch is a monitoring service for AWS resources and applications. It provides data and actionable insights to monitor applications, understand and respond to system-wide performance changes, optimize resource utilization, and get a unified view of operational health.
A key feature of CloudWatch is the ability to set alarms. A CloudWatch Alarm watches a single metric over a specified time period, and performs one or more specified actions, based on the value of the metric relative to a threshold over time. These actions enable you to automatically react to changes in your AWS resources.
For example, you can set an alarm that sends an email message if the CPU usage of an EC2 instance exceeds a certain threshold for a specified number of periods. This allows you to automate responses, which can reduce the time that you spend on operational tasks.
Prerequisites
This lab assumes you have experience creating EC2 instances and are familiar with its basic components.
If you find any gaps in your knowledge, consider taking the following labs:
- Creating an Amazon EC2 instance (Linux)
- Setting up a Web server on an EC2 instance
Objectives
In this lab, you will learn how to:
- Understand the concept and purpose of CloudWatch Alarms in AWS.
- Learn how to create a CloudWatch Alarm.
- Learn how to configure actions for a CloudWatch Alarm.
Lab Steps
Create Amazon EC2 Instance
1. Search “ec2” in the AWS Management Console search bar. Click EC2 on the search results.
2. Launch EC2 instance with the following configurations.
- Name: CWAlarm
- AMI: Ubuntu
- Instance type: t2.micro
- Key pair: If you don’t already have a key pair, please create a new one.
- Key pair name: CWAlarm-ssh
- Key pair type: RSA
- Private key file format: .pem
- Network settings:
- Allow SSH traffic from: My IP
Take note of your EC2 instance ID.
Create a CloudWatch Alarm
1. In the AWS Management Console, navigate to the CloudWatch service.
2. To view all alarms, click on the “All Alarms” option available in the left navigation pane, then set an alarm by clicking on the “Create alarm” button.
3. Select metric: You’ll be asked to choose a metric for the alarm. This could be CPU utilization, network packets, or any other metric that AWS provides.
4. For this lab, go to EC2 → Per-Instance Metrics, then choose CPUUtilization metric.
5. Make sure to check that the instance ID is the same as the instance ID you copied earlier. If not, navigate to Graphed metrics and modify the instance ID accordingly.
6. After selecting the metric, you’ll need to set the conditions for the alarm. This includes setting the threshold value for the metric, choosing whether the alarm will trigger if the metric is greater than, less than, or equal to the threshold, and setting the period over which the metric is evaluated.
- Follow the configurations below:
- Click on ‘Next’.
7. Configure actions: Next, you’ll configure what actions should be taken when the alarm state changes. This could be sending a notification, stopping or terminating an instance, executing AWS Lambda functions, or auto-scaling.
- For this lab, we will choose EC2 action. Follow the configurations below:
- If you are not using the other actions, please make sure to remove them.
- Click on ‘Next’.
8. Add name and description.
- In the ‘Add name and description’ step, provide a unique name and a description for your alarm. For this lab let’s name it, “CPU Utilization”.
- Click on ‘Next’.
9. Preview and create.
- Review your alarm configuration in the ‘Preview and create’ step.
- Click on ‘Create alarm’.
After creating the alarm, you should see it listed in the ‘All Alarms’ section of the CloudWatch console. The state of the alarm will be ‘CPU Utilization’ until it has enough data to determine the alarm state.
Stress Testing your CloudWatch Alarm
1. Now let’s test our CloudWatch Alarm by simulating a high CPU load using a tool called ‘stress’. SSH into your EC2 instance and run the following commands.
#This updates the package list in your system
sudo apt update -y
#This installs stress
sudo apt install stress -y
#Spawns 50 workers for 5 minutes
stress --cpu 50 --timeout 5m
Your connection to the instance might close during the test since it won’t have enough resources to maintain the SSH connection.
2. Navigate to the Amazon EC2 instances console. You should see that the Alarm status of your instance shows 1 in alarm, confirming that your CloudWatch Alarm has been triggered. This means your alarm is working as expected.
Take your time to explore the CloudWatch alarm you’ve just set up. Make sure it’s as expected and that you understand all the settings and conditions you’ve configured. Remember, CloudWatch is a powerful tool for monitoring your AWS resources and applications. It provides data and actionable insights to monitor your applications, understand and respond to system-wide performance changes, optimize resource utilization, and get a unified view of operational health.
Happy exploring!