Cloudwatch is covered by the "Management & Governance" service of AWS (Amazon Web Services). Cloudwatch is a monitoring and management service that can be used to monitor services on AWS and manage them when a certain condition is met. Cloudwatch can be used to collect performance and operational data (CPU, RAM, Network_IN, Network_Out, etc) for the services available on AWS.
When it comes to billing, Cloudwartch comes with two different options, Free Tier and Paid Tier. Free Tier comes with basic 5-minute frequency monitoring metrics and Paid Tier lets you monitor at one-minute frequency. This is just a small difference between Free Tier and Paid Tier. To understand more about invoicing, click here.
Before we continue, let's understand some terms that we will see later in this article.
Alarm: Alarm is used to perform another measure based on the value of the metric or expression relative to a threshold over a number of time periods
- OK : When the metric is within that range as we specified, the alarm is in OK mode
- IN ALARM : When the metric crosses the threshold, the alarm transmits to the "IN ALARM" state
- INSUFFICIENT_DATA : If the data required to make the decision is missing or incomplete, the alarm goes into "INSUFFICIENT_DATA" mode.
SNS topic: It is a logical communication channel in AWS where messages are sent. Here we will use email as our SNS (Simple Notification Service) topic
EC2 Action: Cloudwatch alarms can be used to take a specific action. Here we will stop an EC2 instance when the alarm goes into "In alarm" state.
In this article we will see how to monitor an EC2 instance for its CPU load. We will create an alarm and see how it can be triggered when the CPU Load crosses the specified limit. When this limit reaches, we will turn off the EC2 instance to understand how to take action when the alarm is triggered.
- AWS Account (Create if you do not have one)
- EC2 instance (Click here to learn how to create an EC2 instance if you do not have one or if you want to learn) [1
- Login to AWS.
- Create an SNS topic.
- Create an alarm.
- Increase CPU load on EC2 instance.
Log in to AWS
- Click here to go to AWS login page.
When we press the above link, we will see a web page as follows where we must log in using our login credentials.
When we have successfully logged in to AWS, we will see the main console with all services listed as follows.
Create an SNS topic
To create an SNS topic to which we send a message, click on "Services" and search for SNS. Click on “Simple Notification Service”. Here we create a SNS topic and add e-mail where messages will be sent. Remember the name of the topic we will be giving, this will be required in the next step.
Click "Create Topic" to create a "Simple Notification Service" topic.
Give a name for the topic to be created, here is the "mid-sns topic" on which messages will be sent. Display name is optional and can be provided as is. Other settings like encryption, access policy etc are advanced and we will not see them in this article and can be ignored to create a simple alarm. Scroll down the page and click "Create Topic". This only creates one subject. We still have to add to the communication channel.
We have just created a topic and not specified and communication medium yet. We add email as our communication medium, in order to do that we have to create a subscription.
To create a subscription, click on "Create subscription".
Select an ARN for the topic we created in the previous step and select the protocol as email (medium) from the drop-down menu. Enter your email address where notifications must be sent. Click "Create Subscription" to complete this step. Once you have your subscription in place, you will receive an email from AWS to subscribe to the topic. If we do not subscribe, we will not receive notifications on our email. So don't forget to subscribe to the topic.
To subscribe to the topic you just created, open your email box and click on confirm subscription. Here, the process of creating a subject and subscription ends.
Create an alarm
To create an alarm, click on "Services" and search for "Cloudwatch", click on it to select.
You will see a screen as follows. Now click on "Alarms" on the left panel.
Click "Create Alarm".
Click "Select metrics"
Under "All metrics", select EC2.
Click "Pre-Instance Metrics.
Select" CPU Utilization "and then click" Select metric ".
Now select the instance ID on which the alarm must be set and change the period to 1 minute so that we can test the alarm without wasting much time.  You can try different measurement values and settings when you are familiar with this process.
Here comes the part that lets us set the condition, here we define the threshold value as "10". This means that if the CPU utilization exceeds 10% during a one minute period the alarm will be triggered.
When the alarm is triggered, we must send a message when the alarm goes into the "In alarm" mode on the subject we created in the previous state so choose the topic name.
Along with sending a message we will also take a special action on the EC2 instance, here we will stop the instance when the alarm goes off in "In Alarm" – state. You can choose which action you choose. Click "Next".
Add alarm names sent as email subject, further information can also be sent. Click "Next".
Review the configuration and click "Create Alarm".
And here ends the complete configuration of setting the alarm that sends a message to an SNS subject when the condition is met. Wait some time and the alarm is ready to monitor the EC2 instance.
Testing: Increase the CPU load on the EC2 instance
To activate the alarm manually, we will increase the load on the EC2 instance.  To do that, we must first install a mandatory, stress, package. Find the following steps to install 'stress' on ubuntu 18.04.
sudo apt-get update
apt-get install stress
We will now increase the load to 50% for 60 seconds in a row. This will trigger the alarm and disable the EC2 instance.
stress - cpu 50 - timeout 120
As soon as the condition is met, you will be notified and the instance will be removed.
Now you can in alarm see the state of the alarm that we created in the previous steps is "In alarm" and this ends the article.
As a result of this and because of the action we specified in the previous steps, the EC2 instance is terminated.
In this article we learned how to create an SNS topic, a subscription and an alarm. We also saw how CPU load can be increased manually to trigger the alarm which in return sends a message via email channel.