Certainly! To monitor EC2 disk space usage with a CloudWatch alarm using Terraform, you need to:
- Install the CloudWatch agent on your EC2 instance to collect disk metrics (since default EC2 metrics do not include disk usage).
- Configure the CloudWatch agent to send disk space metrics.
- Create a CloudWatch alarm in Terraform based on the custom metric.
Below is a step-by-step example:
1. CloudWatch Agent Configuration (User Data Example)
Add this to your EC2 instance's user_data to install and configure the CloudWatch agent:
bash
#!/bin/bash
yum install -y amazon-cloudwatch-agent
cat <<EOF > /opt/aws/amazon-cloudwatch-agent/bin/config.json
{
"metrics": {
"append_dimensions": {
"InstanceId": "\${aws:InstanceId}"
},
"metrics_collected": {
"LogicalDisk": {
"measurement": [
"% Free Space"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
}
}
}
}
EOF
/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
Note: Adjust the config for Linux/Windows and the disk path as needed.
2. Terraform: CloudWatch Alarm for Disk Space
Assuming your agent is publishing a metric like LogicalDisk % Free Space to the CWAgent namespace, here's a Terraform example:
hcl
resource "aws_cloudwatch_metric_alarm" "disk_space_low" {
alarm_name = "EC2-DiskSpace-Low"
comparison_operator = "LessThanThreshold"
evaluation_periods = "2"
metric_name = "LogicalDisk % Free Space"
namespace = "CWAgent"
period = "60"
statistic = "Average"
threshold = "20" # Alarm if free space is less than 20%
alarm_description = "Alarm when disk space is low"
dimensions = {
InstanceId = "<EC2_INSTANCE_ID>"
# For Linux, you may need to specify "Filesystem" and "MountPath" instead
# Filesystem = "/dev/xvda1"
# MountPath = "/"
}
actions_enabled = true
alarm_actions = [aws_sns_topic.alerts.arn] # Optional: SNS topic for notifications
}
3. (Optional) SNS Topic for Notifications
hcl
resource "aws_sns_topic" "alerts" {
name = "ec2-diskspace-alerts"
}
4. Notes
- Replace
<EC2_INSTANCE_ID> with your actual instance ID or use Terraform interpolation.
- For Linux, the metric might be
disk_used_percent or similar, and dimensions could be Filesystem and MountPath.
- Make sure your EC2 instance has the correct IAM permissions to publish metrics.
Example: For Linux (disk_used_percent)
If using the CloudWatch Agent default config for Linux, the metric might be disk_used_percent:
hcl
resource "aws_cloudwatch_metric_alarm" "disk_space_low" {
alarm_name = "EC2-DiskSpace-Low"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "2"
metric_name = "disk_used_percent"
namespace = "CWAgent"
period = "60"
statistic = "Average"
threshold = "80" # Alarm if disk usage is more than 80%
alarm_description = "Alarm when disk usage is high"
dimensions = {
InstanceId = "<EC2_INSTANCE_ID>"
Filesystem = "/dev/xvda1"
MountPath = "/"
}
actions_enabled = true
alarm_actions = [aws_sns_topic.alerts.arn]
}
Summary
- Install and configure CloudWatch agent to send disk metrics.
- Create a CloudWatch alarm in Terraform using the custom metric.
- (Optional) Set up SNS notifications.
Let me know if you need a full working example or help with a specific OS!