How can we Monitor an EC2 Instance?

How can we Monitor an EC2 Instance?

In this Demonstration, we create an Amazon Elastic Compute Cloud (EC2) instance and then we create an Amazon CloudWatch alarm that initiates when the Amazon EC2 instance exceeds a specific central processing unit (CPU) utilization threshold. we create a subscription using Amazon Simple Notification Service (Amazon SNS) that sends an email to us.

Logging and monitoring are techniques implemented to achieve a common goal. They work together to help ensure that a system's performance baselines and security guidelines are always met.

Amazon SNS is a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication.

CloudWatch is a monitoring and observability service built for DevOps engineers, developers, site reliability engineers (SREs), IT managers, and product owners. CloudWatch provides you with data and actionable insights to monitor your applications, respond to system-wide performance changes, and optimize resource utilization. CloudWatch collects monitoring and operational data in the form of logs, metrics, and events. You get a unified view of operational health and gain visibility of your AWS resources, applications, and services running 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:

TASK 1:Create an Amazon EC2 Instance.

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

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

  • choose an Amazon Machine Image for now we choose Amazon Linux 2 AMI (HVM). Recommend you choose (Free tier eligible)

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

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

TASK 2:Configure Amazon SNS

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

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

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 "MyCwAlarm"

Step 6: Choose Create topic.

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

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 Create subscription.

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

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

  • After you click on the Confirm subscription from the mail, In the Details section, the Status should be Confirmed.

Task 3: Create a CloudWatch alarm

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

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

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.

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

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

Step 14: Choose Create alarm.

Step 15: Choose Select metric and click on Next

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

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

Step 18: Select the Stress Test and Click on Select metric.

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.

Conditions

  • Threshold type: Choose Static.

  • Whenever CPUUtilization is...: Choose Greater > threshold.

  • than... Define the threshold value: Enter 60

and Click on Next.

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.

Name and description

  • Alarm name: Enter CPUUtilizationAlarm

  • Alarm description - optional: Enter CloudWatch alarm for Stress Test EC2 instance CPUUtilization

After that Choose Next.

Step 20: Review the Preview and create page, and then choose Create alarm.

Task 4: Test the Cloudwatch alarm

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

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,

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.)

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

sudo stress --cpu 10 -v --timeout 200s
  • the output from the command should look similar to the following.

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

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

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.

Task 5: Create a CloudWatch dashboard

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

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

Step 29: Under Add widget choose Line and click on Next.

  • Choose Metrics and click on Next.

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

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

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

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