# π EC2 Disk Usage Monitoring with Amazon CloudWatch Agent
This guide describes how to manually install and configure the **CloudWatch Agent** on an **Ubuntu EC2 instance** to monitor **disk usage** and set up **email alerts** using CloudWatch Alarms.
---
## β
Prerequisites
- EC2 instance (Ubuntu)
- IAM role attached to the instance with the following policies:
- `AmazonSSMManagedInstanceCore`
- `CloudWatchAgentServerPolicy`
- Sudo privileges on the instance
- Access to AWS CloudWatch and SNS for setting up alerts
---
## π Step 1: Attach IAM Role to EC2
1. Go to **EC2 β Instances β [Your Instance]**
2. Click **Actions β Security β Modify IAM Role**
3. Select a role that has:
- `AmazonSSMManagedInstanceCore`
- `CloudWatchAgentServerPolicy`
4. Click **Update IAM Role**
---
## π¦ Step 2: Install the CloudWatch Agent
```bash
cd /tmp
wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
Create the config JSON file to monitor disk usage:
sudo nano /opt/aws/amazon-cloudwatch-agent/bin/config.json
Paste the following content:
{
"metrics": {
"append_dimensions": {
"InstanceId": "${aws:InstanceId}"
},
"metrics_collected": {
"disk": {
"measurement": [
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"/"
]
}
}
}
}
Save and exit.
Run the following command to start the agent using the above config:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
-a fetch-config -m ec2 \
-c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json \
-s
rm /tmp/amazon-cloudwatch-agent.deb
- Go to CloudWatch β Metrics
- Navigate to
CWAgent
namespace - Check for the metric:
disk_used_percent
for your instance - Make sure values are being recorded
-
Go to CloudWatch β Alarms β Create Alarm
-
Choose metric:
CWAgent β disk_used_percent
-
Set condition:
- Threshold: Greater than
70
- Evaluation period: 1 out of 1 datapoints
- Threshold: Greater than
-
Set Notification:
- Choose or create an SNS topic
- Add your email address and confirm it
-
Name the alarm and click Create Alarm
Step | Description |
---|---|
1 | Attach IAM role to instance |
2 | Install CloudWatch Agent |
3 | Create config file to monitor disk usage |
4 | Start CloudWatch Agent with config |
5 | (Optional) Remove installer |
6 | Check CloudWatch metrics |
7 | Create alarm to notify on high disk usage |
Check disk usage:
df -h
Restart CloudWatch Agent:
sudo systemctl restart amazon-cloudwatch-agent
Stop Agent:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a stop
If using SNS for alerts, donβt forget to confirm the email address via the subscription confirmation email from AWS!