DEV Community

Travis Mohr Blog
Travis Mohr Blog

Posted on

Building and Testing an AWS CloudWatch Monitoring Lab

Here I'll be setting up an alarm for CPU utilization on an EC2 instance (and taking it down once it worked).

First things first, I go to the EC2 tab in AWS and create an instance (name it, select the default Amazon Linux OS, and the default free t3.micro instance type).

1 Naming the Alarm

2 Selecting the OS

3 Select t3 micro

I scroll down a bit and make sure the VPC is set to default, that the "auto-assign IP" is enabled, and that the security group for this EC2 has an obvious name.

4 set VPC to default, make sure auto-assign public IP is enabled, give the security group a name

For this test, I won't be SSH'ing into the EC2 instance so I won't be needing a keypair.

5 no keypair needed

I launch the instance at this point, wait to see that it launched successfully, then head to my list of EC2 instances.

6 success and  view all instances

After a bit I can see that the EC2 instance is running and that it has passed all three status checks.

7 the instance i running and status checked

Now that the EC2 instance is set up and running, I go to CloudWatch and click on "create alarm" and then click on "metric" to find the "EC2" metrics list and the select the "per-instance metrics" category to see all of the metrics for my specific EC2 instances.

8 create alarm

9 select metric

10 go to EC2 metrics

11 select per-instance metrics

I go back to the EC2 instance page to take note of the test EC2 instance that I just created and take note of the instance ID (last for characters: "0fa4")...

11 take note of the EC2 instance ID

... and then find the "CPU Utilization" metric that coincides with this particular EC2 test instance in my CloudWatch tab back under "per-instance metrics" (for the instance ending in "0fa4", of course). I check the box next to the metric and then hit "select metric".

12 select the metric for this EC2 and hit  select metric

From there, I scroll down to set the metric that I want to test: Static (to monitor a specific, unchanging metric), "greater/equal" to "20" % CPU utilization.

13 set metric

After clicking on next, I go ahead and "remove" the SNS topic option (which would be useful in a production situation, but not needed for testing a CloudWatch alarm).

14 remove SNS option

Click on next again and then name the alarm something that makes sense.

14 name the metric

After clicking next again, I am presented with a summary of the metric that I just created, and then click "create alarm" after seeing that everything checks out.

15 create alarm

And I can then see that the alarm has been created.

16 the alarm has been created!

Now that the alarm for this test EC2 instance is in place, I want to test it by causing the EC2 instance to use some compute. In order to do that, I go to the EC2 instance, right click on it, and select "connect", leave everything as default, and click on "connect" again.

17  connect to the EC2 instance

18 click on  connect  again

This will take us to a new tab where I the the Amazon Linux 2023 CLI. Since I want to test my 20% CPU utilization alarm for my EC2 instance, I download an app called "stress" with the prompt "sudo yum install stress -y" (the "-y" confirming "yes, we want to install it" so we aren't prompted to confirm):

19 download  stress

Now that I have "stress" downloaded, I type in "stress --help" to see what my options are for bombarding my EC2 instance with compute. After taking a look at my options, I run the command "stress -c 1 -t 3600" to run the compute through one CPU ("-c 1") for a total of one hour or 3600 seconds ("-t 3600").

20 get a list of commands then set the stress function

Now that I have set the stress test to hit the EC2 instance with compute load, I go back to the alarm in CloudWatch and select it to watch the CPU utilization for the EC2 instance start to tick up...

21 CPU utilization going up

... and after a few moments I hit "refresh" to see that the compute used by this EC2 instance (thanks to it being bombarded by the stress app) has exceeded 20% and that the alarm that I created has in fact been set off:

22 the alarm has been set off

Since my test was successful, I go ahead and turn off the stress test by typing "control+C" in the CLI.

23 turn off the stress test with  control+C

And at that point, I go back to my test alarm and see that the compute has dropped below my 20% threshold and that the alarm has shut off:

24 CPU usage goes down and alarm shuts off

This confirms that the CloudWatch alarm did in fact function as intended. So at this point, I want to be sure to delete: the CloudWatch Alarm, the EC2 instance, and the EC2 Instance Security Group:

25 delete the alarm

26 delete the EC2 instance

27 delete the EC2 security group

After all has been deleted from the successful test, my AWS account is back to the way it was (and not charging me for services that I don't need to use).

Top comments (0)