CloudWatch CPU Utilization Monitoring with SNS Alert

mahpara jabbar - Sep 4 - - Dev Community

This lab focuses on setting up a CloudWatch alarm to monitor CPU utilization on an EC2 instance, configuring an SNS topic to receive email alerts when CPU utilization exceeds a defined threshold, and creating a CloudWatch dashboard for real-time monitoring. Additionally, it involves installing the stress tool on the EC2 instance to simulate high CPU usage for testing the alarm and notification system.

Summary

  • Launch EC2 Instance.
  • Create SNS Topic and Subscription.
  • Create a CloudWatch Alarm.
  • Create a CloudWatch Dashboard.
  • Connect to EC2 and Install Stress Tool.

Launch EC2 Instance:

Launch an EC2 instance with the following specifications:

  • Instance Type: t2.nano
  • Network: Assign a public IP.
  • Security Group: Allow SSH (port 22) from your IP.

Image description

Create SNS Topic and Subscription:

  • Go to the SNS (Simple Notification Service) section in the AWS Management Console.
  • Click on Topics > Create Topic.
  • Choose Standard as the type.
  • Provide a Name for the topic (e.g., EC2-CPU-Alerts).
  • Click Create Topic.

Image description

  • In the newly created topic, click on Create Subscription.
  • Set the protocol to Email.
  • Enter your email address and click Create Subscription.
  • Confirm the subscription by checking your email and clicking on the verification link.

Create a CloudWatch Alarm:

  • Go to the CloudWatch section in the AWS Management Console.
  • Select Alarms from the sidebar, then click Create Alarm.
  • Click Select Metric.
  • Navigate to EC2 > Per-Instance Metrics > CPUUtilization.
  • Choose the Instance ID of your EC2 instance.
  • Set the Statistic to Average and the Period to 1 minute.
  • Set the Threshold Type to Static and enter 60 as the value (for greater than 60% CPU utilization).
  • Click Next.
  • Under Actions, choose Select an existing SNS topic and pick the topic you created earlier.
  • Click Next.
  • Provide an Alarm Name (e.g., High-CPU-Utilization-Alarm).
  • Click Create Alarm.

Image description

Create a CloudWatch Dashboard:

  • In the CloudWatch console, navigate to Dashboards.
  • Click Create Dashboard.
  • Provide a Name for the dashboard (e.g., EC2-Monitoring-Dashboard).
  • Click Add Widget.
  • Choose Line and click Configure.
  • Select the same metric used in your alarm (EC2 CPU Utilization).
  • Click Create Widget.
  • Save the dashboard.

Image description

Connect to EC2 and Install Stress Tool:

  • Connect your EC2 instance.
  • Run the following commands to update the instance and install the stress tool:
  • sudo yum update -y
  • sudo yum install stress -y
  • Use the stress tool to generate CPU load:
  • stress --cpu 1 --timeout 300

Image description

CPU utilization crosses the threshold:

Image description

Alarm Generated:

Image description

This lab should give you a hands-on understanding of setting up CloudWatch alarms with SNS and monitoring an EC2 instance's CPU utilization.

. . . . . . . .