{"id":17601019,"url":"https://github.com/moshdev2213/cloudwatch","last_synced_at":"2026-05-03T07:46:04.702Z","repository":{"id":259074992,"uuid":"876123731","full_name":"moshdev2213/CloudWatch","owner":"moshdev2213","description":"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! 🚀","archived":false,"fork":false,"pushed_at":"2024-10-21T15:56:42.000Z","size":261,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-04T22:24:15.012Z","etag":null,"topics":["aws","cloudwatch","ec2","pyhton","sns"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/moshdev2213.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-21T12:46:02.000Z","updated_at":"2024-10-21T16:01:58.000Z","dependencies_parsed_at":"2024-10-22T23:43:50.401Z","dependency_job_id":null,"html_url":"https://github.com/moshdev2213/CloudWatch","commit_stats":null,"previous_names":["moshdev2213/cloudwatch"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moshdev2213%2FCloudWatch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moshdev2213%2FCloudWatch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moshdev2213%2FCloudWatch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moshdev2213%2FCloudWatch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moshdev2213","download_url":"https://codeload.github.com/moshdev2213/CloudWatch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246249222,"owners_count":20747167,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","cloudwatch","ec2","pyhton","sns"],"created_at":"2024-10-22T12:07:47.056Z","updated_at":"2026-05-03T07:46:04.674Z","avatar_url":"https://github.com/moshdev2213.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# AWS CloudWatch 🖥️⌚\n\n\u003cimg width=\"960\" alt=\"18 (2)\" src=\"https://github.com/user-attachments/assets/b8af8b0c-adcc-49f7-bc8e-d2bb131f36eb\"\u003e\n\n\n## 📚 Project Overview\n\nThis 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.\n\nIn this project, I:\n\n1. Launched an EC2 instance on AWS.\n2. Simulated CPU spikes with a Python script.\n3. Monitored CPU utilization using AWS CloudWatch.\n4. Set up a CloudWatch Alarm to notify via email when CPU utilization exceeds 50% over a 1-minute average.\n\n## 🔧 AWS Services Used\n\n- **Amazon EC2**: A virtual server to run the CPU simulation.\n- **AWS CloudWatch**: Used to monitor the CPU utilization.\n- **CloudWatch Alarms**: To trigger alerts based on CPU utilization metrics.\n- **Amazon SNS (Simple Notification Service)**: Sends an email notification when the CloudWatch alarm is triggered.\n\n## 🚀 Setup Instructions\n\n### 1. Launch an EC2 Instance\n\n1. Go to the **EC2 Dashboard** on AWS.\n2. Click on **Launch Instance**.\n3. Choose your desired **AMI** (Amazon Machine Image) and **Instance Type** (e.g., `t2.micro` for testing).\n4. Configure **Security Groups** to allow SSH (port 22) access.\n5. Launch the instance and connect to it via SSH.\n\n### 2. Install Python on EC2\n\nAfter connecting to your instance via SSH, ensure Python is installed. If not, install it with:\n\n```bash\nsudo yum update -y\nsudo yum install python3 -y\n```\n\n### 3. Upload and Run the CPU Spike Script\n\n1. Use the following Python script to simulate a CPU spike:\n\n```python\nimport time\n\ndef simulate_cpu_spike(duration=30, cpu_percent=80):\n    print(f\"Simulating CPU spike at {cpu_percent}%...\")\n    start_time = time.time()\n\n    # Calculate the number of iterations needed to achieve the desired CPU utilization\n    target_percent = cpu_percent / 100\n    total_iterations = int(target_percent * 5_000_000)  # Adjust the number as needed\n\n    # Perform simple arithmetic operations to spike CPU utilization\n    for _ in range(total_iterations):\n        result = 0\n        for i in range(1, 1001):\n            result += i\n\n    # Wait for the rest of the time interval\n    elapsed_time = time.time() - start_time\n    remaining_time = max(0, duration - elapsed_time)\n    time.sleep(remaining_time)\n\n    print(\"CPU spike simulation completed.\")\n\nif __name__ == '__main__':\n    # Simulate a CPU spike for 30 seconds with 80% CPU utilization\n    simulate_cpu_spike(duration=30, cpu_percent=80)\n```\n\n2. Save this script as `cpu_spike.py` on the EC2 instance.\n3. Run the script to simulate CPU load:\n\n```bash\npython3 cpu_spike.py\n```\n\n### 4. Set Up CloudWatch Monitoring\n\n1. Navigate to **CloudWatch** in the AWS Console.\n2. Under **Metrics**, select **EC2** and choose your instance's **CPUUtilization** metric.\n3. You can view the real-time CPU usage after running the script.\n\n### 5. Set Up SNS for Notifications\n\nAmazon 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:\n\n#### Step 1: Create an SNS Topic\n\n1. Go to the **SNS Dashboard** in AWS.\n2. Click **Create Topic**.\n   - **Type**: Standard\n   - **Name**: `cpu-alarm-topic`\n3. Click **Create Topic**.\n\n#### Step 2: Subscribe an Email to the SNS Topic\n\n1. In the SNS topic you just created, click on **Create Subscription**.\n   - **Protocol**: Email\n   - **Endpoint**: Enter your email address.\n2. Click **Create Subscription**.\n\n💡 **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.\n\n### 6. Create a CloudWatch Alarm ⏰\n\n1. In CloudWatch, go to **Alarms** and click **Create Alarm**.\n2. Select the **CPUUtilization** metric from your EC2 instance.\n3. Set the alarm condition:\n   - **Threshold type**: Static\n   - **Threshold**: CPU utilization \u003e 50%\n   - **Period**: 1 minute\n4. Configure actions:\n   - **Send notification to**: Select the SNS topic you created (`cpu-alarm-topic`).\n5. Review and create the alarm.\n\n## 📊 Monitoring the Spike\n\nRun 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\n\n![alarm_metric](https://github.com/user-attachments/assets/2d5bf915-1777-4d79-8520-3289615f13c9)\n\n\u003cbr/\u003e\n\n\u003cimg width=\"1423\" alt=\"alert_notification\" src=\"https://github.com/user-attachments/assets/541db17f-a76b-4404-9c95-51997f79a7a9\"\u003e\n\n\u003cbr/\u003e\n\n![spike_metric](https://github.com/user-attachments/assets/6a33e1f4-9758-4d1b-96dd-fb5822c27ec0)\n\n\u003cbr/\u003e\n\n\u003cimg width=\"931\" alt=\"subscribe_notification\" src=\"https://github.com/user-attachments/assets/3a3975ed-0574-4f7f-af05-f4b96791943a\"\u003e\n\n\u003cbr/\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"559\" alt=\"subscription_success\" src=\"https://github.com/user-attachments/assets/33a2e01c-d5fe-48d4-9131-ae7f2a9c9c30\"\u003e\n\u003c/p\u003e\n\n\n## 📝 Resources\n\n- **EC2 Instance**: [AWS EC2](https://aws.amazon.com/ec2/)\n- **CloudWatch Alarms**: [AWS CloudWatch Alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)\n- **SNS Notifications**: [AWS SNS](https://aws.amazon.com/sns/)\n\n## 🌟 Conclusion\n\nThis 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.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoshdev2213%2Fcloudwatch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoshdev2213%2Fcloudwatch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoshdev2213%2Fcloudwatch/lists"}