Sub topic 9
Collecting memory, disk, and log files with the CloudWatch agent
This chapter assumes you can launch an EC2 instance, attach an IAM role to it, and run AWS CLI commands. It also assumes you know what a custom metric and a log group are from earlier chapters.
Why the agent exists
EC2 publishes CPU, network, and disk I/O metrics from outside the instance. The hypervisor cannot see inside the operating system. Memory use, filesystem space, and application log files are therefore missing by default.
The CloudWatch agent is a process that runs on the instance. It reads those values locally and sends them to CloudWatch. Metrics it collects arrive as custom metrics, in the CWAgent namespace unless you change it. Log lines it reads arrive in a log group.
Prerequisites on the instance
- Attach an instance profile whose role includes the AWS managed policy
CloudWatchAgentServerPolicy. Without it the agent cannot publish, and it fails with permission errors in its own log. - Install the agent package. On Amazon Linux this is
sudo dnf install -y amazon-cloudwatch-agent. - Write a configuration file and tell the agent to load it.
The configuration file
The configuration is JSON with three main sections. agent holds global settings. metrics says which system values to collect. logs says which files to ship.
{
"agent": { "metrics_collection_interval": 60 },
"metrics": {
"append_dimensions": { "InstanceId": "${aws:InstanceId}" },
"metrics_collected": {
"mem": { "measurement": ["mem_used_percent"] },
"disk": { "measurement": ["used_percent"], "resources": ["*"] }
}
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/app/app.log",
"log_group_name": "/app/web",
"log_stream_name": "{instance_id}"
}
]
}
}
}
}metrics_collection_intervalis the number of seconds between readings. 60 gives standard resolution.append_dimensionsadds the instance ID to every metric, so you can alarm on one instance.mem_used_percentandused_percent(disk) are the measurement names. The agent adds dimensions such aspathanddeviceto disk metrics itself.{instance_id}inlog_stream_nameis a placeholder the agent fills in, which gives each instance its own stream.
Loading the configuration
Save the file as /opt/aws/amazon-cloudwatch-agent/etc/config.json. Then start the agent with it.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
-a fetch-config -m ec2 -s \
-c file:/opt/aws/amazon-cloudwatch-agent/etc/config.jsonThe -a fetch-config option loads a configuration. -m ec2 sets the mode, and -s starts the agent. To check it, run the same tool with -a status.
Checking the result
After a couple of minutes, look in the CWAgent namespace in the console or list it from the CLI.
aws cloudwatch list-metrics --namespace CWAgent --metric-name mem_used_percentThe log group /app/web should also contain a stream named after the instance ID. The agent creates the group if it is missing, with retention set to never expire. Set a retention period yourself to avoid unbounded storage cost.
Common errors
| Symptom | Likely cause |
|---|---|
| No metrics appear | The instance role lacks `CloudWatchAgentServerPolicy`, or the agent is not running |
| Log group is empty | The file path is wrong, the file is unreadable by the agent, or nothing new has been written |
| Metrics have unexpected dimensions | `append_dimensions` or the disk `resources` list differs from your alarm's dimensions |
The agent only ships lines written after it starts, so it does not upload older content of an existing file.
Exercise
Why does an alarm on mem_used_percent return no data even though the agent is running and you can see the metric in the console?
Show answer
The alarm's dimensions must match the metric's exactly. The agent publishes this metric with the InstanceId dimension you configured in append_dimensions. If the alarm names a different set of dimensions, or a different namespace (it must be CWAgent), it points at a different metric that has no data.