In today’s cloud-driven world, proactively monitoring the performance of your infrastructure is crucial to maintaining system reliability and efficiency. AWS CloudWatch offers robust monitoring for AWS services, enabling you to set alerts for performance metrics like CPU utilization, memory usage, and more. Integrating CloudWatch with Amazon SNS (Simple Notification Service) lets you receive timely email alerts, ensuring your team can respond swiftly to potential issues. This guide will walk you through setting up CloudWatch for EC2 monitoring and alerting via SNS.
Topics Covered:
- Setting Up SNS for Email Alerts
- Launching an EC2 Instance for Testing
- Enabling Detailed Monitoring of EC2 Instances
- Configuring CloudWatch Alarms for CPU Utilization
- Testing and Verifying Alarm Notifications
1. Setting Up SNS for Email Alerts
Amazon SNS lets you send notifications for various AWS services, including CloudWatch. We’ll start by creating an SNS topic and subscribing an email endpoint to it so you can receive alerts when specific conditions are met.
- Create an SNS Topic:
- Navigate to Amazon SNS in the AWS Console.
- Choose Create Topic, select Standard, and provide a name (e.g., EC2-Monitoring-Alerts).
- Click Create Topic to save the configuration.
- Subscribe to an Email Endpoint:
- Open the newly created topic and select Create Subscription.
- Choose Protocol as Email, and enter your email address in the Endpoint field.
- Confirm the subscription by verifying the email sent to your inbox.
Once configured, SNS will handle notifications from CloudWatch alarms, promptly informing you of any performance issues with your EC2 instances.
2. Launching an EC2 Instance for Testing
We’ll first launch a test EC2 instance to monitor CPU utilization and test alerting.
- Launch an EC2 Instance:
- Go to EC2 in the AWS Console and select Launch Instance.
- Choose an Amazon Machine Image (AMI), such as Amazon Linux 2.
- Select an instance type (e.g., t2.micro) and proceed with the default settings.
- In the Configure Instance settings, ensure Enable CloudWatch Detailed Monitoring is selected.
- Configure storage and any necessary tags, then launch the instance.
- SSH into the EC2 Instance (optional):
- Access the instance via SSH to simulate a load and test the alarms.
3. Enabling Detailed Monitoring on EC2 Instances
Detailed monitoring provides more granular data than essential monitoring, allowing you to capture performance metrics every minute.
- Enable Detailed Monitoring:
- Go to EC2 > Instances.
- Select your instance and go to the Monitoring tab.
- If detailed monitoring is not enabled, click Actions > Monitor and troubleshoot > Enable detailed tracking.
- Note that allowing detailed monitoring may incur additional charges.
4. Configuring CloudWatch Alarms for CPU Utilization
Now that we have an instance running with detailed monitoring let’s configure a CloudWatch alarm to monitor CPU utilization.
- Create a CloudWatch Alarm:
- Open CloudWatch in the AWS Console and go to Alarms > Create Alarm.
- Click Select Metric and choose EC2 Metrics > Per-Instance Metrics.
- Locate and select CPUUtilization for your EC2 instance.
- Configure the alarm conditions (e.g., trigger when CPU utilization exceeds 80% for 5 minutes).
- Select Next, and set a name for the alarm (e.g., High-CPU-Utilization-Alert).
- Set the Alarm Action:
- Under Notification, select In alarm, and choose the SNS topic you created (EC2-Monitoring-Alerts).
- Set Notification for In alarm to send a message to your SNS topic when the alarm state changes to “In Alarm.”
- Click Next and Create Alarm.
5. Testing and Verifying Alarm Notifications
Finally, we’ll verify that the alarm functions correctly by inducing high CPU utilization and confirming the alert.
- Simulate High CPU Utilization:
- SSH into your EC2 instance and run a command to simulate CPU load (e.g., stress command if available or a looped script).
- Monitor the CloudWatch console to see the CPU utilization spike.
- Verify Alarm and Notification:
- Once the CPU threshold is crossed, CloudWatch will trigger the alarm and send an email notification via SNS.
- Check your email to confirm the notification is received as expected.
- Testing Alarm Recovery:
- Stop the load-generating process to reduce CPU utilization, which should automatically reset the alarm.
- Ensure you receive a notification when the alarm returns to OK, confirming recovery.
Conclusion
Monitoring EC2 performance and setting up alerts in AWS CloudWatch can significantly enhance your system’s reliability and give your team the insights they need to maintain performance. With CloudWatch and SNS configured, you’re now equipped to proactively respond to performance issues, minimize downtime, and optimize resource management.
References
Monitor your instances using CloudWatch
Create a billing alarm to monitor your estimated AWS charges