# How can we Monitor an EC2 Instance?

In this Demonstration we will set up an instance on Amazon Elastic Compute Cloud and create a Amazon CloudWatch alarm which will be triggered, when the CPU utilization of the EC2 instance exceeds a threshold and set up a subscription using Amazon Simple Notification Service (SNS) to receive email notifications.

**Logging and monitoring** are techniques used to achieve goals. They work together to ensure that system performance meets baselines and security guidelines consistently.

**Amazon SNS** is a managed messaging service that enables communication.

**CloudWatch** is a monitoring and observability service. It provides data and insights for monitoring applications responding to performance changes across systems and optimizing resource utilization. CloudWatch collects logs, metrics and events as part of its monitoring data collection process. By using CloudWatch you can have a view of your health and gain visibility into your AWS resources, applications and services running both on AWS and, on premises.

## Objectives:

After completing this Demonstration, you will be able to:

* Create an Amazon EC2 instance
    
* Create an Amazon SNS notification
    
* Configure a CloudWatch alarm
    
* Stress test an EC2 instance
    
* Confirm that an Amazon SNS email was sent
    
* Create a CloudWatch dashboard
    

### Let's begin the Demonstration:

### <mark>TASK 1:Create an Amazon EC2 Instance.</mark>

Step 1: Go to the management console and Search for Ec2 and click on it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426123718/995c19a1-e9cb-47c7-b513-ab9c3602c3ee.png align="center")

Step 2: Give a Name to the instance. for now, we name it "<mark>Stress Test</mark>".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426130172/6dae8734-faee-460c-908f-34912f2eebae.png align="center")

* choose an Amazon Machine Image for now we choose Amazon Linux 2 AMI (HVM). <mark>Recommend you choose (Free tier eligible)</mark>
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426132847/0065b03d-cd8d-41c0-a85e-9d0729b1f23e.png align="center")

Step 3: For the key pair(login) we proceed without a key pair. You could choose an existing or create new key pair.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426139620/9bf0f39d-6679-4186-b706-2124bc578f8e.png align="center")

Step 4: you could leave other settings default and click on Launch instance

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426144200/d8d8ca72-df09-47ca-b2fc-14cedc96a0cd.png align="center")

### <mark>TASK 2:Configure Amazon SNS</mark>

Step 3: Go to the management console and Search For SNS

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426149397/2d1cc38f-84f6-4717-9047-58e771cd8830.png align="center")

Step 4: On the left, under Dashboard choose the Topics, and then choose Create topic.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426274462/bfb9e479-b850-44ab-b804-f388260c13c8.png align="center")

Step 5: On the Create topic page in the Details section, configure the following options:

* Type: Choose Standard.
    
* Name: Enter the name. For now, we say <mark>"MyCwAlarm"</mark>
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426279543/e9bf5092-3410-47c4-9525-0d5bcddc274d.png align="center")

Step 6: Choose **Create topic**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426283012/0fc3557a-2710-440c-8667-e53641833fa6.png align="left")

Step 7: On the MyCwAlarm details page, choose the Subscriptions tab, and then choose <mark>Create subscription.</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426287755/0d17207b-b3c5-40c4-a7e1-c41f88c359c8.png align="center")

Step 8: In the **Details** section, configure the following options:

* **Topic ARN**: Leave the default option selected.
    
* **Protocol**: From the dropdown list, choose **Email**.
    
* **Endpoint**: Enter a valid email address that you have.
    

and Click on <mark>Create subscription.</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426292778/48efb518-c971-4bfb-b429-e1bf4ee239fe.png align="center")

* In the **Details** section, the **Status** should be **Pending confirmation**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426296414/9a9416ea-e7cd-4bf9-8afc-80f9abe3070d.png align="center")

* You should have received an **AWS Notification - Subscription Confirmation** email message at the email address that you provided in the previous step.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426300783/53995252-d08e-44f9-93b7-9b4ebbe5ae95.png align="center")

* After you click on the <mark>Confirm subscription</mark> from the mail, In the **Details** section, the **Status** should be <mark>Confirmed.</mark>
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426307550/abce8236-04da-4616-96ce-76946381ee6c.png align="center")

## <mark>Task 3: Create a CloudWatch alarm</mark>

Step 9: In the AWS Management Console, enter **Cloudwatch** in the search bar, and then choose it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426311421/f1935103-0a71-4666-b349-6efdee27d406.png align="center")

Step 10: In the left navigation pane, choose the **Metrics** dropdown list, and then choose **All metrics**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426360362/92f1fc02-571d-484c-9882-6a44bff50152.png align="left")

Step 11: On the **Metrics** page, choose **EC2**, and choose **Per-Instance Metrics**.From this page, you can view all the metrics being logged and the specific EC2 instance for the metrics.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426364825/adb8e9e5-148c-4601-97cd-dcd3ebc81dff.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426367691/9e8c597a-376c-4c67-8a83-ca765a3f973f.png align="center")

Step 12: Slect the ­check box with **CPUUtilization** as the **Metric name** for the **Stress Test** EC2 instance that we created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426371944/6f4bab41-deaa-496c-82f3-2bee5bbbd29e.png align="center")

Step 13: In the left navigation pane, choose the **Alarms** dropdown list, and then choose **All alarms.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426375722/86c71a24-af0d-4e82-8159-38b0a018d088.png align="left")

Step 14: Choose <mark>Create alarm.</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426379234/23cd2631-9ea7-4528-8e0a-2ab592ae4c2c.png align="center")

Step 15: Choose **Select metric and click on Next**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426382151/05c04442-b9d0-4bf2-9cdd-c560664cc93e.png align="center")

Step 16: choose **EC2**, and then choose **Per-Instance Metrics**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426386439/90856d31-e332-4b83-b8d8-4fcd58644b65.png align="center")

Step 17: Under the Search bar type **CPUUtilization** and hit Enter.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426389123/be593567-ec55-4d34-a0fb-86832245f4cd.png align="center")

Step 18: Select the Stress Test and Click on <mark>Select metric.</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426391684/b55d3e89-637f-4573-91cb-e1af34b007a5.png align="center")

Step 19: On the **Specify metric and conditions** page, configure the following options:

### Metric

* Metric name: Enter CPUUtilization
    
* InstanceId: Leave the default option selected.
    
* Statistic: Select Average
    
* Period: From the dropdown list, choose 5minute.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685545991617/9f931010-3744-4888-b6c4-c9263a8062f4.png align="center")

### Conditions

* Threshold type: Choose Static.
    
* Whenever CPUUtilization is...: Choose Greater &gt; threshold.
    
* than... Define the threshold value: Enter 60
    

and Click on <mark>Next.</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685546076513/0d3dbada-835d-4fa8-88ac-f8ee9342b69f.png align="center")

### Notification

* Alarm state trigger: Choose In alarm.
    
* Select an SNS topic: Choose Select an existing SNS topic.
    
* Send a notification to...: Choose the text box, and then choose MyCwAlarm.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685546094501/29670c94-3001-49e7-8063-415c65ad2bd5.png align="center")

### Name and description

* Alarm name: Enter CPUUtilizationAlarm
    
* Alarm description - optional: Enter CloudWatch alarm for Stress Test EC2 instance CPUUtilization
    

After that Choose <mark>Next.</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685546126914/52273ed9-db84-4aba-b0e4-39db1182ccb3.png align="center")

Step 20: Review the Preview and create page, and then choose <mark>Create alarm.</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426395282/c36d0aee-d386-4df5-abad-653c0b2b8e3a.png align="center")

## <mark>Task 4: Test the Cloudwatch alarm</mark>

Step 21: In the management Console search for **EC2** and click on it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685547136316/6495e371-0de6-4641-a00c-04e2a6421fc0.png align="center")

Step 22: Now, under the **Instances** Click on the **Stress Test** Instance that we created earlier and Click on **Connect** on the right side of Instances,

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685547144975/e0b5d96f-fb50-4a2c-a247-23e3979d7d49.png align="center")

Step 23: From the **Connect to instance** Choose **Session Manager** and Click on **Connect.**

(Note: you can connect from EC2 Instance Connect as the ultimate goal is to increase the Cpu utilization so that we get notified from the Amazon SNS through mail.)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685547360748/21ff7d78-3c1d-4424-9e99-0a5fb3762b05.png align="center")

Step 24: To manually increase the CPU load of the EC2 instance, run the following command:

```bash
sudo stress --cpu 10 -v --timeout 200s
```

* the output from the command should look similar to the following.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685547367524/e3e95659-bb99-41f5-82a7-8a676a0dad22.png align="center")

Step 25: Navigate back to the AWS console where you have the CloudWatch Alarms page open. Then Choose <mark>CPUUtilizationAlarm</mark> after that Monitor the graph while selecting the refresh button every 1 minute until the alarm status is <mark>In alarm.</mark>

On the graph, you can see where CPUUtilization has increased above the 60 percent threshold.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685548433876/163a8af9-20cc-4fd8-bce5-ba35be5c7ad0.png align="center")

Step 26: Go to your email inbox for the email address that you used to configure the Amazon SNS subscription. You should see a new email notification from AWS Notifications like this below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685547232242/637f24be-e02f-49fe-8ba7-233af07da65d.png align="center")

## <mark>Task 5: Create a CloudWatch dashboard</mark>

Step 27: Go to the **CloudWatch** in the AWS console and In the left navigation plane, choose **Dashboards** and Click on **<mark>Create dashboard.</mark>**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426429059/7ff70b4e-c424-44be-b3bc-308964f0e5ae.png align="center")

Step 28: Give a name to the Dashboard for now we say it **EC2Dashboard** and click on **Create dashboard.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426432288/3e1462a4-aa52-468c-a024-792bcf43df1e.png align="center")

Step 29: Under Add widget choose <mark>Line</mark> and click on **Next.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426438468/25dabe52-dcd7-4a7a-ad5b-781ab11aa805.png align="center")

* Choose **Metrics** and click on Next.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426445358/0e998e89-0541-4c56-8986-c349daca7693.png align="center")

Step 30: Now, Choose **EC2** and then **Per-instance Metrics.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426448214/3e9dcf71-13b0-448e-aa88-da6124371000.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426450602/895aa42f-531b-4146-978c-8fa223fd6fc7.png align="center")

Step 31: Under the search bar type **CPUUtilization** and select the **Stress Test,** and Click on **Create widget.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426453166/8a533b15-a726-43a3-b64d-3f5b8abe1d31.png align="center")

Step 32: Finally, you can see the CPUUtilization of an Instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685426476002/7d436f51-894b-483f-b3e4-d8e51af63647.png align="center")

### **Congratulations!**!!

You now have successfully:

* Created an Amazon SNS notification
    
* Configured a Cloudwatch alarm
    
* Stress-tested an EC2 instance
    
* Confirmed that an Amazon SNS email was sent.
    
* created a CloudWatch dashboard
