CloudWatch Agents
When monitoring EC2 instances with Amazon CloudWatch, you typically use CloudWatch for logs, metrics, and custom monitoring. There are two main agents involved:
๐ง 1. CloudWatch Agent Types¶
a. CloudWatch Logs Agent (Deprecated)¶
-
Purpose: Only sends log files to CloudWatch Logs.
-
Language: Written in Python.
-
Installation: Via
awslogspackage. -
Status: Deprecated โ use Unified Agent instead.
b. CloudWatch Unified Agent (Recommended)¶
-
Purpose: Sends logs and metrics (both default and custom) to CloudWatch.
-
Features:
-
Collect CPU, memory, disk, network metrics.
-
Push custom application logs.
-
Collect procstat, disk IO, etc.
-
-
Installation: Single agent; installed from
amazon-cloudwatch-agentpackage.
๐ 2. CloudWatch Metrics for EC2¶
a. Default Metrics (from EC2 without agent)¶
Sent automatically every 5 minutes (or 1 minute with detailed monitoring):
-
CPUUtilization -
NetworkIn,NetworkOut -
DiskReadBytes,DiskWriteBytes -
StatusCheckFailed, etc.
๐ No agent needed for default metrics.
b. Custom Metrics (with CloudWatch Unified Agent)¶
Requires Unified Agent to collect:
-
Memory usage
-
Disk space usage
-
Swap usage
-
Custom app-level metrics (via
statsdor embedded API)
๐ These are not available without the agent.
๐ 3. CloudWatch Logs¶
a. Log Types You Can Send:¶
-
/var/log/messages,/var/log/syslog,/var/log/nginx/access.log, etc. -
App logs like Python, Java, Node.js logs.
b. Where to Configure:¶
-
Unified Agent config file:
/opt/aws/amazon-cloudwatch-agent/bin/config.json -
Or use the Wizard:
amazon-cloudwatch-agent-config-wizard
๐ Summary¶
| Feature | No Agent | Logs Agent (Deprecated) | Unified Agent โ |
|---|---|---|---|
| Basic EC2 Metrics | โ | โ | โ |
| Custom Metrics (Memory, etc.) | โ | โ | โ |
| Logs Collection | โ | โ | โ |
| StatsD/CollectD Support | โ | โ | โ |
| Recommended | โ | โ | โ |
โ๏ธ CloudWatch Unified Agent โ Setup Workflow (Logs + Metrics)¶
๐ Step 1: IAM Role/Permissions¶
Attach an IAM Role to your EC2 instance with this policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudwatch:PutMetricData",
"ec2:DescribeVolumes",
"ec2:DescribeTags",
"logs:PutLogEvents",
"logs:DescribeLogStreams",
"logs:DescribeLogGroups",
"logs:CreateLogStream",
"logs:CreateLogGroup"
],
"Resource": "*"
}
]
}
๐ Step 2: Install the Unified CloudWatch Agent¶
For Amazon Linux / Ubuntu / Debian:¶
# Download & Install
sudo yum install amazon-cloudwatch-agent -y # Amazon Linux
# or
sudo apt-get install amazon-cloudwatch-agent -y # Ubuntu/Debian
โ๏ธ Step 3: Create the Agent Configuration File¶
You can use the wizard or manually create a config.
๐ง Use Wizard (Recommended):¶
The wizard prompts you to choose:
-
Logs to collect (e.g.,
/var/log/syslog) -
Metrics to collect (e.g., memory, disk, swap)
-
Region
-
Destination (CloudWatch Logs group)
๐ OR Manually create a config:¶
Example config (/opt/aws/amazon-cloudwatch-agent/bin/config.json):
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
},
"metrics": {
"append_dimensions": {
"InstanceId": "${aws:InstanceId}"
},
"metrics_collected": {
"cpu": {
"measurement": ["cpu_usage_idle", "cpu_usage_user"],
"metrics_collection_interval": 60
},
"mem": {
"measurement": ["mem_used_percent"]
},
"disk": {
"measurement": ["used_percent"],
"resources": ["/"]
}
}
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/syslog",
"log_group_name": "ec2-syslog",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}
โถ๏ธ Step 4: Start the Agent¶
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
โ Step 5: Verify the Setup¶
-
โ Logs: Go to CloudWatch > Logs > Log Groups.
-
โ Metrics: Go to CloudWatch > Metrics > All metrics > CWAgent.
-
๐งช Run
top,df -h, or write logs to test.
๐ Optional: Automate with User Data (Cloud Init)¶
If launching EC2 instances frequently, use this in EC2 User Data:
#!/bin/bash
yum install -y amazon-cloudwatch-agent
cat <<EOF > /opt/aws/amazon-cloudwatch-agent/bin/config.json
{
... (your config here)
}
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
๐ Example Dashboard Metrics to Add:¶
-
mem_used_percent -
cpu_usage_user -
disk_used_percent -
Log stream errors count