Amazon CloudWatch is a service provided by AWS, which can be seen as a metrics repository that allows users to monitor the resources they have created in real time. AWS resources, such as EC2 instances, put metrics inside the CloudWatch repository. These metrics can then be used to create alarms that can start, stop, or terminate EC2 instances if certain conditions are met. These alarms can also be configured to send notifications using
Let’s assume we have an EC2 instance and we want to monitor its CPU utilization. We’ll create an alarm that triggers when the CPU utilization of the instance exceeds 75% and configure the alarm to stop the instance when this happens. To do this, we’ll use the “CPU Utilization” metric of the instance.
Ensure that you have the IAM permissions required to create a CloudWatch alarm and SNS topic and then follow the steps given below to create a CloudWatch Alarm:
Note: Make sure you have an EC2 instance up and running before you start creating a CloudWatch alarm.
On the AWS console, search for “CloudWatch” and click the CloudWatch service from the search results. This takes us to the CloudWatch dashboard.
From the sidebar, click “All alarms” under “Alarms,” and then click the “Create alarm” button.
Now, follow these steps to create the alarm:
Click the “Select metric” button, and a pop-up will open.
In the “Metrics” search bar, type in the instance ID of the instance for which you want to create an alarm.
Click “EC2 > Per-Instance Metrics” in the search results.
Again, in the “Metrics” search bar, type CPUUtilization
and press “Enter.” Here, you can select any other metric over which you want to create a CloudWatch alarm.
Next, select only the metric that appears and click the “Select metric” button.
Under “Metric,” open the “period” drop-down menu and select “1 minute” to check the instance in one-minute periods.
Again, under “Metric,” remove Average
from statistics and enter Maximum
to monitor the maximum CPU utilization rather than the average.
An AWS CloudWatch metric is a monitoring service that collects performance and operational data for AWS resources like an AWS instance. There are different metrics for an EC2 instance, each monitoring a unique performance aspect. We’ll be using the CPU utilization metric for the alarm.
Under “Conditions,” select “Static” and then the “Greater/Equal” alarm condition. Next, enter 75
as the threshold value.
Click the “Next” button.
An AWS CloudWatch condition is a rule we can define to trigger an alarm when a particular condition is met. We’ll be configuring the condition to trigger when the metric (CPUUtilization
) is greater than or equal to 75%.
Under the “Notification” section, do the following:
Select “In alarm” as the alarm state trigger.
For “Send a notification,” select the “Create new topic” option.
Enter a name for your SNS topic. Setting up this topic will allow AWS to email an incident report to the subscribed email when the alarm is triggered.
Enter an email address under “Email endpoints…” to receive a notification on that email when the alarm is triggered.
Click the “Create topic” button.
Go to the email’s inbox, open the “AWS Notification” email, and confirm the AWS subscription.
Under “EC2 action,” do the following:
Click the “Add EC2 action” button.
Select “In alarm” as the alarm state trigger.
Select the “Stop this instance” option. Now, whenever our CPU utilization goes above 75%, our instance will automatically stop working.
Click the “Next” button.
Enter demo-cw-alarm
as the alarm name.
Enter Stop instance when CPU utilization exceeds 75%
as the alarm description.
Click the “Next” button.
Review the settings for the new alarm before clicking the “Create alarm” button to create the alarm.
After the alarm is created, notice that the state of the alarm changes from “Insufficient data” to “OK” after a minute. This happens because we have set a minute-by-minute periodic check on the instance, and its state will only change at the end of each period.
Congratulations! You’ve created a CloudWatch alarm. You can test this alarm by using the stress
library to overload and spike the CPU usage of our instance to trigger your alarm, and if it is correctly set up, it will stop the instance and send an incident report to the subscribed user’s email.
Note: Using the
stress
library in the production phase is not recommended as this library intentionally floods our EC2 instances with requests to check how it reponds in case our instances face a large traffic volume.
Through this CloudWatch alarm, we are easily notified if the CPU utilization of our EC2 instances exceeds a specific threshold. If this happens, we can manage our resources according to the traffic we receive.
Free Resources