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.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay