In this tutorial, we will demonstrate how to monitor CPU utilization using CloudWatch and set up alarms with SNS for alerts.
By following this tutorial, we will have a practical understanding of how to monitor CPU utilization in AWS using CloudWatch, configure alarms for proactive alerting, and set up notifications through SNS to effectively manage our AWS resources.
Let's get started.
First, go to the EC2 console and click on Launch instances
.
Provide a name for your instance.
On Quick Start, choose Ubuntu.
Instance type as:
Proceed without a key pair as we'll be using EC2 connect.
Leave all other options as default and click on Advanced details
.
Select Enable on the Detailed CloudWatch monitoring
section.
Now, click on Launch instance
.
In the Services tab, search for CloudWatch and select it.
In the left panel In alarm
, select Create alarm
.
Click on Select metric
.
Click on EC2
.
Click on Per-instance Metrics.
Search for your instance, select having the metric CPUUtilization and click on Select metric
.
Scroll a bit and in the Conditions, select and make values as below:
When CPU utilization is equal to or greater than 50% of the total capacity, then the alarm will be invoked.
Click on Next
.
On Configure actions,
Create a new topic,
Provide a name for your topic,
Provide an email address to deliver the notifications.
Click on Create topic.
Click on Next
.
Provide an alarm name and description.
Click on Next
.
Proceed to Create alarm
.
Select Any state
.
We can see the Warning.
We have to confirm the subscription to receive the notifications in our mail address.
Go to your mailbox and check on Spam,
Click on Confirm subscription
.
Click on refresh and you'll see Actions enabled.
Now head up to the EC2 console and select your instance.
Click on Connect
.
We're into our EC2 instance now.
Now, to spike the CPU utilization on our Linux instance, we can use the stress
utility. stress
is a simple command-line tool designed to impose various types of workload stress on a Linux system, including CPU, memory, I/O, and more.
In this case, we want to spike the CPU utilization, so we'll use stress
to create a CPU load.
Install stress:
sudo apt update
sudo apt install stress
Create CPU Load:
Once
stress
is installed, you can use it to create a CPU load.The basic syntax is as follows:
stress --cpu <number_of_CPU_threads> --timeout <duration_in_seconds>
For example, to stress all available CPU threads for 2 minutes (120 seconds), you can run:
stress --cpu $(nproc) --timeout 120
The nproc
command is used to determine the number of CPU cores or processing units on your system.
Run the above command to create a spike in CPU utilization.
Stop the Stress Test:
- You can stop the stress test at any time by pressing
Ctrl+C
.
Go to the CloudWatch to see the spike in CPU Utilization. [Click on Refresh to see changes]
We can also see the alarm is in In alarm
state.
We're notified through SNS as we'd set up earlier.
This way, it can be very helpful to take proactive actions to mitigate the possible effects.
We had given time for 2 minutes. So, after 2 minutes, the stress is removed automatically.
We can also remove stress anytime using Ctrl+C
.
Go to your EC2 instance, select the Monitoring section, choose CPU Utilization and Enlarge it.
Turn it to 1 minute.
You can observe the CPU utilization spiking
So, that was all for this lab.
Now, time to clean up the used resources.
Go to the CloudWatch and In All alarms, select your alarm to delete.
Now, head up to the EC2 console and terminate your EC2 instance.
This concludes the lab.
And I'll see you in the next one.
Thank you.