https://github.com/moshdev2213/cloudwatch
This project demonstrates how to simulate a CPU spike on an AWS EC2 instance π₯οΈ, monitor it using CloudWatch π, and trigger email alerts via SNS π§ when CPU utilization exceeds 50%. A simple hands-on introduction to AWS monitoring and alerting services! π
https://github.com/moshdev2213/cloudwatch
aws cloudwatch ec2 pyhton sns
Last synced: about 2 months ago
JSON representation
This project demonstrates how to simulate a CPU spike on an AWS EC2 instance π₯οΈ, monitor it using CloudWatch π, and trigger email alerts via SNS π§ when CPU utilization exceeds 50%. A simple hands-on introduction to AWS monitoring and alerting services! π
- Host: GitHub
- URL: https://github.com/moshdev2213/cloudwatch
- Owner: moshdev2213
- Created: 2024-10-21T12:46:02.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2024-10-21T15:56:42.000Z (7 months ago)
- Last Synced: 2025-02-04T22:24:15.012Z (4 months ago)
- Topics: aws, cloudwatch, ec2, pyhton, sns
- Language: Python
- Homepage:
- Size: 255 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AWS CloudWatch π₯οΈβ
## π Project Overview
This project demonstrates how to simulate a CPU spike in an AWS EC2 instance, monitor it using AWS CloudWatch, and set up alarms to trigger notifications using AWS SNS. The goal is to track CPU utilization in real-time and automatically trigger an email alert when CPU utilization exceeds a certain threshold.
In this project, I:
1. Launched an EC2 instance on AWS.
2. Simulated CPU spikes with a Python script.
3. Monitored CPU utilization using AWS CloudWatch.
4. Set up a CloudWatch Alarm to notify via email when CPU utilization exceeds 50% over a 1-minute average.## π§ AWS Services Used
- **Amazon EC2**: A virtual server to run the CPU simulation.
- **AWS CloudWatch**: Used to monitor the CPU utilization.
- **CloudWatch Alarms**: To trigger alerts based on CPU utilization metrics.
- **Amazon SNS (Simple Notification Service)**: Sends an email notification when the CloudWatch alarm is triggered.## π Setup Instructions
### 1. Launch an EC2 Instance
1. Go to the **EC2 Dashboard** on AWS.
2. Click on **Launch Instance**.
3. Choose your desired **AMI** (Amazon Machine Image) and **Instance Type** (e.g., `t2.micro` for testing).
4. Configure **Security Groups** to allow SSH (port 22) access.
5. Launch the instance and connect to it via SSH.### 2. Install Python on EC2
After connecting to your instance via SSH, ensure Python is installed. If not, install it with:
```bash
sudo yum update -y
sudo yum install python3 -y
```### 3. Upload and Run the CPU Spike Script
1. Use the following Python script to simulate a CPU spike:
```python
import timedef simulate_cpu_spike(duration=30, cpu_percent=80):
print(f"Simulating CPU spike at {cpu_percent}%...")
start_time = time.time()# Calculate the number of iterations needed to achieve the desired CPU utilization
target_percent = cpu_percent / 100
total_iterations = int(target_percent * 5_000_000) # Adjust the number as needed# Perform simple arithmetic operations to spike CPU utilization
for _ in range(total_iterations):
result = 0
for i in range(1, 1001):
result += i# Wait for the rest of the time interval
elapsed_time = time.time() - start_time
remaining_time = max(0, duration - elapsed_time)
time.sleep(remaining_time)print("CPU spike simulation completed.")
if __name__ == '__main__':
# Simulate a CPU spike for 30 seconds with 80% CPU utilization
simulate_cpu_spike(duration=30, cpu_percent=80)
```2. Save this script as `cpu_spike.py` on the EC2 instance.
3. Run the script to simulate CPU load:```bash
python3 cpu_spike.py
```### 4. Set Up CloudWatch Monitoring
1. Navigate to **CloudWatch** in the AWS Console.
2. Under **Metrics**, select **EC2** and choose your instance's **CPUUtilization** metric.
3. You can view the real-time CPU usage after running the script.### 5. Set Up SNS for Notifications
Amazon Simple Notification Service (SNS) allows you to send messages to users via email, SMS, or other protocols. Hereβs how you can set up SNS to send an email when a CloudWatch Alarm is triggered:
#### Step 1: Create an SNS Topic
1. Go to the **SNS Dashboard** in AWS.
2. Click **Create Topic**.
- **Type**: Standard
- **Name**: `cpu-alarm-topic`
3. Click **Create Topic**.#### Step 2: Subscribe an Email to the SNS Topic
1. In the SNS topic you just created, click on **Create Subscription**.
- **Protocol**: Email
- **Endpoint**: Enter your email address.
2. Click **Create Subscription**.π‘ **Note**: You will receive a confirmation email. Make sure to confirm the subscription to start receiving alerts. Also When Directly creating aarms from cloudwatch the topics will be created , to make a subscriber confirm the verification mail sent to the specific user.
### 6. Create a CloudWatch Alarm β°
1. In CloudWatch, go to **Alarms** and click **Create Alarm**.
2. Select the **CPUUtilization** metric from your EC2 instance.
3. Set the alarm condition:
- **Threshold type**: Static
- **Threshold**: CPU utilization > 50%
- **Period**: 1 minute
4. Configure actions:
- **Send notification to**: Select the SNS topic you created (`cpu-alarm-topic`).
5. Review and create the alarm.## π Monitoring the Spike
Run the Python script, and after about a minute, youβll see the CPU utilization spike in CloudWatch metrics. When CPU utilization exceeds 50%, youβll receive an email notification via AWS SNS. Below Are Some Screenshots that are relevant to metrics and alerts


![]()
## π Resources
- **EC2 Instance**: [AWS EC2](https://aws.amazon.com/ec2/)
- **CloudWatch Alarms**: [AWS CloudWatch Alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)
- **SNS Notifications**: [AWS SNS](https://aws.amazon.com/sns/)## π Conclusion
This project demonstrates a simple yet effective way to monitor system performance using AWS services. By setting up alarms, you can proactively monitor and respond to changes in system performance.