Collecting Custom Metrics and Logs with the CloudWatch Agent
The unified CloudWatch agent collects operating system metrics, application metrics and log files from EC2 instances and on-premises servers. This lesson explains what the agent collects, how it is installed and configured at scale with Systems Manager, how its configuration file is structured, and how permissions and troubleshooting work.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the metrics and logs the CloudWatch agent collects that are not available by default.
- Explain how to install and configure the agent at scale with Systems Manager and Parameter Store.
- Interpret the structure of the agent configuration file, including procstat, StatsD and collectd.
- Identify the permissions and common causes of missing agent data.
What the Agent Adds
The CloudWatch agent runs on Linux, Windows and macOS, on EC2 and on premises. It collects in-guest system metrics such as memory utilisation, swap, disk space used per file system, disk I/O, network and process counts on Linux, and Windows Performance Monitor counters on Windows. It collects per-process metrics with the procstat plugin, for example CPU and memory of a specific application process or whether it is running. It receives application metrics through StatsD and collectd protocols and scrapes Prometheus metrics. It also collects log files and Windows event logs and sends them to CloudWatch Logs, and it can act as an OpenTelemetry and X-Ray endpoint for traces.
Agent metrics are published to the CWAgent namespace by default and are custom metrics, charged per metric. Memory and disk alarms, which cannot be built from default EC2 metrics, are the most common reason to install the agent.
Installing and Configuring at Scale
Installing the agent on each server by hand does not scale or stay consistent. The standard approach uses Systems Manager, introduced in the Systems Manager module, as follows.
- Permissions: attach the managed policy
CloudWatchAgentServerPolicyto the instance profile (alongsideAmazonSSMManagedInstanceCore), or provide credentials for on-premises servers through the hybrid activation role or a credentials profile. - Installation: a State Manager association runs
AWS-ConfigureAWSPackagewith theAmazonCloudWatchAgentpackage on all target nodes, so new instances receive the agent automatically. - Configuration: create the JSON configuration, with the configuration wizard or by hand, and store it in Parameter Store, for example as
AmazonCloudWatch-linux-web. - Start: a second association runs
AmazonCloudWatch-ManageAgentwith actionconfigureand the parameter as the source, so every agent loads the same configuration and restarts when it changes.
Quick Setup offers a ready-made configuration for this, and EC2 Image Builder can include the agent in golden AMIs. Separate parameters per server role (web, database, worker) keep configurations specific.
Roll out the CloudWatch agent
The Configuration File
The agent configuration has three main sections: agent (collection interval, region, credentials), metrics (what to collect, the namespace, dimensions and aggregation) and logs (which files to send to which log groups). The example below collects memory and disk metrics, monitors a process and ships an application log.
{
"agent": {"metrics_collection_interval": 60},
"metrics": {
"namespace": "CWAgent",
"append_dimensions": {"InstanceId": "${aws:InstanceId}", "AutoScalingGroupName": "${aws:AutoScalingGroupName}"},
"aggregation_dimensions": [["AutoScalingGroupName"]],
"metrics_collected": {
"mem": {"measurement": ["mem_used_percent"]},
"disk": {"measurement": ["used_percent"], "resources": ["/", "/data"]},
"procstat": [{"exe": "orders-worker", "measurement": ["cpu_usage", "memory_rss", "pid_count"]}],
"statsd": {"service_address": ":8125", "metrics_collection_interval": 10}
}
},
"logs": {
"logs_collected": {
"files": {"collect_list": [{
"file_path": "/var/log/orders/app.log",
"log_group_name": "/orders/app",
"log_stream_name": "{instance_id}",
"retention_in_days": 30
}]}
}
}
}append_dimensions adds instance metadata to every metric, and aggregation_dimensions also publishes metrics aggregated across the Auto Scaling group, so the group's memory can be alarmed on as a whole while instances come and go. Setting retention_in_days in the configuration sets the log group retention when the agent creates it.
Agent configuration
Troubleshooting Missing Data
When agent metrics or logs do not appear, the causes are usually few. The table below lists them.
| Symptom | Likely cause |
|---|---|
| No CWAgent metrics from an instance | Agent not installed or not running, or no configuration loaded |
| AccessDenied in the agent log | Instance profile lacks CloudWatchAgentServerPolicy |
| Metrics present, logs missing | Wrong file path, file permissions, or log group not created and no permission to create it |
| Data stops for instances in private subnets | No route to CloudWatch and CloudWatch Logs endpoints (NAT gateway or VPC endpoints) |
| Metrics appear with unexpected dimensions | append_dimensions or aggregation_dimensions configuration |
The agent's own log on Linux is at /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log, and amazon-cloudwatch-agent-ctl -a status shows whether it is running and which configuration is loaded.
Memory Alarms for an Auto Scaling Group
Instances in an Auto Scaling group occasionally run out of memory and crash, but no alarm fires, because the team only has CPU metrics.
The team stores an agent configuration that collects mem_used_percent with AutoScalingGroupName in append_dimensions and as an aggregation dimension, and deploys it with State Manager associations to every instance with the application tag. A CloudWatch alarm on the group-level average memory above 85% for three of five minutes notifies the team, and a second alarm uses Metrics Insights to watch the maximum memory of any instance in the group, so a single leaking instance is detected even when the average is normal.
A team needs disk space alarms for EC2 instances. Which EC2 metric provides disk space used?
Show answer
None of the default EC2 metrics provide file system usage. The CloudWatch agent must collect the disk used_percent metric.
Exam Tips
Remember for the exam
- The CloudWatch agent collects memory, disk space, swap, process (procstat), StatsD, collectd and Prometheus metrics plus log files; default EC2 metrics do not include memory or disk usage.
- Install with State Manager and AWS-ConfigureAWSPackage, store configuration in Parameter Store, and apply it with AmazonCloudWatch-ManageAgent.
- The instance profile needs CloudWatchAgentServerPolicy; private subnets need endpoints or NAT.
- append_dimensions adds instance metadata; aggregation_dimensions publishes group-level aggregates.