Skip to main content

Command Palette

Search for a command to run...

AWS Cloud Monitoring & Alerting:

EC2 Stress Test Simulation

Updated
4 min read
P

As a associate system administrator I worked on Redhat Linux servers, including user management, permissions, services, and performance monitoring Automated routine administrative tasks using Bash scripting and cron jobs, reducing manual effort by ~30% I am aws certified sysops administrator and Google Certified Cloud Engineer. Determined to transition my career into cloud architect /Cloud Support role

This project demonstrates how to monitor AWS infrastructure using Amazon CloudWatch and Amazon SNS. You will launch a Linux EC2 instance, install a stress utility to simulate high CPU utilization, and configure automated email alerts to trigger when performance thresholds are breached.

🚀 Project Overview

In a production environment, knowing when your resources are under pressure is critical. This hands-on guide covers:

  • Launching and configuring an AWS EC2 instance.

  • Generating artificial CPU load using the stress utility.

  • Monitoring real-time metrics in CloudWatch.

  • Configuring CloudWatch Alarms and SNS Notifications for automated alerting.

AWS Services Used

  • EC2 (Elastic Compute Cloud): To host the Linux environment.

  • CloudWatch: To monitor CPU utilization and host the alarm logic.

  • SNS (Simple Notification Service): To send email alerts when the alarm triggers.

  • Security Groups: To manage access to the instance.


🛠️ Step-by-Step Implementation

Phase 1: Launch the EC2 Instance

  1. Log in to the AWS Management Console.

  2. Navigate to EC2 > Launch Instance.

  3. Name: Monitoring-Demo-Server.

  4. AMI: Amazon Linux 2023 (Free Tier eligible).

  5. Instance Type: t2.micro.

  6. Key Pair: Select an existing one or create a new one to SSH into the instance.

  7. Security Group: Ensure SSH (Port 22) is allowed from your IP.

Phase 2: Create the SNS Topic for Alerts

Before creating the alarm, we need a way to receive notifications.

  1. Go to Amazon SNS > Topics > Create topic.

  2. Type: Standard. Name: CPU_Alert_Topic.

  3. Once created, click Create subscription.

  4. Protocol: Email.

  5. Endpoint: Enter your personal email address.

  6. Important: Check your email inbox and click Confirm Subscription.

Phase 3: Install Stress Utility and Generate Load

Connect to your instance via SSH or EC2 Instance Connect and run the following commands:

Bash

# Update the package index
sudo yum update -y

# Install the stress utility
# Note: For Amazon Linux 2023, you may need to install epel-release or use:
sudo amazon-linux-extras install epel -y  # (For AL2)
sudo yum install stress -y

# Verify installation
stress --version

To start the stress test (Run this later once the alarm is set):

Bash

# This command stresses 2 CPUs for 600 seconds
stress --cpu 2 --timeout 600

Phase 4: Configure CloudWatch Alarm

  1. Navigate to CloudWatch > Alarms > All alarms > Create alarm.

  2. Click Select metric > EC2 > Per-Instance Metrics.

  3. Search for your Instance ID and select CPUUtilization.

  4. Conditions:

    • Threshold type: Static.

    • Whenever CPUUtilization is...: Greater than 70%.

  5. Actions:

    • Select In alarm.

    • Send a notification to the SNS topic created in Phase 2 (CPU_Alert_Topic).

  6. Name the alarm: High_CPU_Utilization_Alarm.


📊 Monitoring the Simulation

Once you execute the stress command in your terminal, watch the CloudWatch dashboard.

  1. State: OK – CPU is below 70%.

  2. State: In Alarm – CPU has crossed the 70% threshold. You will receive an email notification.

  3. State: OK – Once the stress timeout expires, the CPU drops, and the alarm clears.


🧹 Cleanup (Avoid Unnecessary Costs)

To prevent unexpected AWS charges, ensure you delete the resources in this order:

  1. EC2: Terminate the Monitoring-Demo-Server instance.

  2. CloudWatch: Delete the High_CPU_Utilization_Alarm.

  3. SNS: Delete the CPU_Alert_Topic and its subscriptions.

  4. Security Groups: Remove the specific group if it’s no longer needed.


🧠 Key Learnings

  • Metric Intervals: Default EC2 monitoring is 5 minutes. (Detailed monitoring provides 1-minute intervals).

  • Alarm States: Understanding the transition from OK to ALARM and back to OK.

  • SNS Integration: How decoupled services communicate via Pub/Sub to keep admins informed.

#!/bin/bash
# 1. Update the system
dnf update -y

# 2. Install stress utility 
# (Amazon Linux 2023 uses dnf and has stress in the default repos/epel)
dnf install stress -y

# 3. Create a log file to track progress
echo "Stress test starting at $(date)" > /var/log/stress-test.log

# 4. Execute stress command
 --cpu 
 --timeout 600s: Run for 10 minutes (gives CloudWatch time to trigger)
stress --cpu 2 --timeout 600s >> /var/log/stress-test.log 2>&1

# 5. Log completion
echo "Stress test completed at $(date)" >> /var/log/stress-test.log