AMAZON CLOUDWATCH Pt.2(Hands-on)

Tolulope Olawuni - Feb 20 - - Dev Community

For this exercise we shall be using Ubuntu. We will be installing just one tool in that ec2 instance which is called a STRESS. STRESS is used to stimulate high CPU, memory or disk usage to test Amazon cloudwatch metrics and Alarm systems. We can't run the stress without having computer/server. So, I will spin up EC2 instance(Ubuntu).

Monitoring tab
After spinning up EC2 instance(Ubuntu), click on the monitoring Tab of the instance, the graphs above were showing NO DATA for some reason. So Wait for few minutes for the readings to begin. It is important to know that these graphs in the monitoring tab comes from CLOUDWATCH service. The graphs under this monitoring Tab are the METRICS we talked about in the previous article. CPU usage which measures the percentage of CPU resources used by an instance, Network IN refers to the amount of data received by the instance.
note These are metics that are created automatically when we launch an EC2 instance, if we want custom metrics like RAM utilization we will need to create our own metrics. Our focus here is CPU utilization, which is the most important metrics.
Cloudwatch by default at every 5mins will be monitoring data to this graph if we want to reduce the number to every minute then we will have to enable DETAILED MONITORNG so we are going to enable detailed monitoring.

Managing detailed monitoring

sudo apt update

Select our instance, go to the monitoring tab to see the instance Metrics, click on monitoring, enable detailed monitoring and Voila! your monitoring has been set to every 1 minute.WE will log into this instance and stress the CPU of this instance. Here, I will use gitbash to ssh into it.

stress command
note for Ubuntu, always remember to update your package list before installing anything.
After installation, execute the STRESS command to show options and example of how to stress a particular metric.
cpu stressing
This is an example which shows how we can stress on the RAM, CPU, the IO metric but our focus here is the CPU.

top command
The nohup -c 4 -t 300 & command creates a 4 processes for us which will run for 300secs. The "nohup" and "&" put this command in the background.

CPU utilization
Now we'll run command called TOP which shows the processes running on our computer realtime and dynamically. Here we see the four stress processes and the utilization to be 99 at that point in time.
This is to intentionally stress this instance, Cloud Watch will check it every minute and put that in the graph.
So do we do this like few times, run STRESS just for 100 seconds, close it, run it for 200 seconds, wait for a few minutes, and now we will get a pattern in the graph.
if we don't want to do this manually, we can create a script like this:

sleep 60 && stress -c 4 -t 30 && sleep 60 && stress -c 4 -t 30 && sleep 60 && stress -c 4 -t 30. 
Enter fullscreen mode Exit fullscreen mode

Now, we can go back to our AWS to check the graph again. Hover on the CPU metric and click on the enlarge icon to zoom.

cloudwatch
This graph shows the usage of the CPU in the last 5mins and other minute you'd like to see. Amazing right! we can see the difference compared to when we just created the instance without stressing it.

CLOUDWATCH SERVICE
CLOUDWATCH SERVICE
We are prepared now! Let's go to CloudWatch Service where we will be setting up an alarm for CPU utilization for this instance. So come to all alarms section here and click on Create Alarm. Select a metric which is our CPU utilization.
create alarm

conditions

To select a metric which in our case,is our CPU utilization for EC2, we first we click on the ec2 service, we see per instance metrics(click), find your instance and if you don't see your instance, wait for a few minutes till it loads information, find CPU utilization metric for your instance. In our case, the instance name is Cloudwatch-test so select CPU utilization for Cloudwatch test.
we can see the flow numbered from 1-4 above
Next, In the next screen we will be setting conditions for Alarm. The condition states that if the CPU usage exceed 60% in the period of 5 minutes then it should be in Alarm.

specifying conditions for metric
condition 1: ****Conditions set for a 5mins period.

conditions 2
condition 2: If usage is greater than 60, that is if it exceeds threshold.

Cloudwatch Alarm confirmation
In this screen we are just configuring the alarm notification which are the name, the title of the notification and what email should the notification be sent. For this article, we leave the other options, scroll down and click on next.

Additional config
Additional configuration involves setting the alarm's name. Note: It's recommended to choose a name that grabs attention or emphasizes the urgency of the situation.

Config preview
The next screen is to Preview our configurations and Create the ALARM

Alarm creation
Our Alarm has been created!
What this means is we when have CPU usage above 60, Cloudwatch will trigger an Alarm warning us about our usage. There are other actions we can modify on our instance like when the usage crosses the threshold we set, EC2 can reboot itself or create another EC2.

Finally, I will be running the stress again to trigger Alarm notification for crossing the threshold(set at 60).

sns
graphs
For an email notification to be sent, the condition must persist for 5 minutes. Alright, the notification appeared in my inbox.

That’s the alert! You can now see the instance state change from "Insufficient Data" to "Alarm"
email.

In summary, Amazon CloudWatch is a powerful monitoring and observability tool that helps businesses track performance, troubleshoot issues, and optimize their AWS environment. With features like Metrics, Logs, and Alarms, CloudWatch provides real-time insights that improve efficiency and reliability

. . . .