CloudWatch setup for CPU and Memory on a Debian 12 AWS EC2 instance to send alerts via email and SMS involves the following steps:
Step 1: Install and Configure the CloudWatch Agent
- Update your instance and install prerequisites:
sudo apt update && sudo apt upgrade -y sudo apt install unzip -y
- Download the CloudWatch agent:
wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/arm64/latest/amazon-cloudwatch-agent.deb
- Install the CloudWatch agent:
sudo dpkg -i amazon-cloudwatch-agent.deb
- Configure the CloudWatch agent: Use the following command to generate a configuration file:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
- During the setup, specify:
- The metrics you want to monitor (e.g., CPU, memory, disk).
- Log collection if needed.
- Instance role-based authentication (use an IAM role with CloudWatch permissions).
/opt/aws/amazon-cloudwatch-agent/bin/config.json
. - During the setup, specify:
- Start the CloudWatch agent:
sudo systemctl start amazon-cloudwatch-agent sudo systemctl enable amazon-cloudwatch-agent
- Verify the agent is running:
sudo systemctl status amazon-cloudwatch-agent
Step 2: Configure CloudWatch Alarms
- Go to the AWS Management Console:
- Navigate to CloudWatch > Alarms > Create Alarm.
- Create a CPU Utilization Alarm:
- Select the EC2 instance metric.
- Choose CPUUtilization under the metrics list.
- Set the threshold, e.g., “If CPUUtilization > 80% for 5 minutes”.
- Create a Memory Utilization Alarm:
- Memory metrics are collected via the CloudWatch agent. They will appear under Custom Metrics in CloudWatch.
- Select the custom memory metric (e.g.,
CWAgent > Memory > Available
). - Set the threshold, e.g., “If Available Memory < 500MB for 5 minutes”.
- Set Actions:
- For both alarms, select Add Notification > Create a new SNS topic.
- Name the topic (e.g.,
CPU-Memory-Alerts
) and add your email address. - For SMS, add a phone number under Subscribers.
- Confirm Email Subscription:
- AWS will send a confirmation email to the address you specified. Open the email and confirm the subscription.
Step 3: Test the Setup
- Trigger a CPU Spike: Run the following command to increase CPU usage:
yes > /dev/null &
- Wait for the alarm to trigger.
- Stop the spike with
killall yes
.
- Trigger a Low Memory Alert: Allocate memory to simulate low available memory:
stress --vm 2 --vm-bytes 90% --timeout 60
- Verify Notifications:
- Ensure you receive the alerts via email and SMS when the thresholds are met.
Optional: Use CloudWatch Dashboard
- Create a CloudWatch Dashboard to view real-time metrics.
- Go to CloudWatch > Dashboards > Create Dashboard.
- Add widgets for CPU, memory, and other metrics for better visualization.
- Save and monitor your instance metrics from a single place.
IAM Role Permissions
Ensure your EC2 instance has an IAM role with the following permissions:
CloudWatchAgentServerPolicy
CloudWatchFullAccess
AmazonSNSFullAccess
Attach the IAM role to your EC2 instance via the IAM Management Console.
JSON Policy for CloudWatch Role allowing the CloudWatch agent access to the EC2 Instance:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudwatch:PutMetricData",
"ec2:DescribeTags",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
See how Dynaweb can help keep your website hosting online with CloudWatch setup for CPU and Memory alerts >> Get Started