DEV Community

mahpara jabbar
mahpara jabbar

Posted on

CloudWatch CPU Utilization Monitoring with SNS Alert

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.

👋 While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay