Monitoring and Logging · Topic 1 of 5
CloudWatch Metrics and the Agent
Collect and store metrics with CloudWatch: default and custom metrics, the unified agent, embedded metric format, resolution, retention and metric streams.
- 4 min
- EC2 default metrics arrive every 5 minutes; detailed monitoring gives 1 minute.
- Memory and disk space metrics need the CloudWatch agent.
- High resolution metrics go down to 1 second; sub minute data is kept for 3 hours.
What it is
A metric is a time ordered set of data points, such as CPU utilization every minute. Amazon CloudWatch is the AWS service that stores metrics, graphs them and raises alarms on them.
Most AWS services publish metrics automatically. For things AWS cannot see, such as memory use inside an instance or your own business counters, you must publish custom metrics.
How it works
Namespaces, dimensions and resolution
A metric is identified by its namespace, name and dimensions. Namespaces for AWS services start with AWS/, such as AWS/EC2, and custom namespaces cannot use that prefix.
A dimension is a name and value pair such as InstanceId. Each unique combination of dimensions is a separate metric.
| Item | Detail |
|---|---|
| EC2 basic monitoring | Metrics every 5 minutes, free |
| EC2 detailed monitoring | Metrics every 1 minute, extra cost |
| Standard resolution custom metric | One minute granularity |
| High resolution custom metric | Down to 1 second, set with StorageResolution of 1 |
Not all EC2 metrics are visible to the hypervisor. Memory use, disk space use and swap are not published by default, so the CloudWatch agent is required to collect them.
Retention and rollup
CloudWatch keeps data at decreasing detail and merges it over time.
| Data point period | Retention |
|---|---|
| Under 60 seconds | 3 hours |
| 1 minute | 15 days |
| 5 minutes | 63 days |
| 1 hour | 455 days |
For longer storage, use a metric stream to send metrics to Amazon Data Firehose and then to S3 or another destination for analysis.
Publishing a custom metric
aws cloudwatch put-metric-data --namespace MyApp --metric-name QueueDepth \
--value 42 --unit Count --dimensions Service=checkout
Call this from a script, or better, use the agent or the embedded metric format.
The unified CloudWatch agent
The agent runs on EC2 instances and on premises servers and collects system level metrics (memory, disk, swap, network and more) as well as log files. It can also receive StatsD and collectd metrics. The instance role needs the CloudWatchAgentServerPolicy managed policy.
A minimal configuration file:
{
"metrics": {
"metrics_collected": {
"mem": {"measurement": ["mem_used_percent"]},
"disk": {"measurement": ["used_percent"], "resources": ["*"]}
}
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{"file_path": "/var/log/app.log",
"log_group_name": "/app/web",
"log_stream_name": "{instance_id}"}
]
}
}
}
}
Store the configuration in Systems Manager Parameter Store and apply it to many instances at once, then use a State Manager association to keep the agent installed and configured.
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
-a fetch-config -m ec2 -s -c ssm:AmazonCloudWatch-linux
Embedded metric format
The embedded metric format (EMF) lets an application write one structured JSON log line that CloudWatch turns into metrics. This is efficient for Lambda and containers, because there is no extra API call and the same record also supports detailed log queries.
{
"_aws": {
"Timestamp": 1700000000000,
"CloudWatchMetrics": [{
"Namespace": "MyApp",
"Dimensions": [["Service"]],
"Metrics": [{"Name": "OrderLatency", "Unit": "Milliseconds"}]
}]
},
"Service": "checkout",
"OrderLatency": 142
}
Specialised collection
| Need | Feature |
|---|---|
| Container metrics for ECS and EKS | Container Insights |
| Lambda memory, CPU and cold start data | Lambda Insights |
| Prometheus metrics at scale | Amazon Managed Service for Prometheus with Amazon Managed Grafana |
| Metrics from logs | Metric filters on a log group |
| Calculations across metrics | Metric math |
Aggregation
Choose the right statistic: average hides spikes, while maximum and percentiles such as p99 show them. Use metric math to build ratios, such as errors divided by requests, and Metrics Insights to query many metrics with SQL like syntax.
What to remember for the exam
- EC2 default metrics arrive every 5 minutes; detailed monitoring gives 1 minute.
- Memory and disk space metrics need the CloudWatch agent.
- High resolution metrics go down to 1 second; sub minute data is kept for 3 hours.
- Retention rolls from 1 minute for 15 days, to 5 minutes for 63 days, to 1 hour for 455 days.
- The agent configuration can live in Parameter Store and be rolled out by State Manager.
- The agent needs
CloudWatchAgentServerPolicyon the instance role. - EMF publishes metrics from log lines with no extra API calls.
- Metric streams send metrics to Firehose for long term storage and third party tools.
- Container Insights and Lambda Insights provide curated metrics for those platforms.
If a question asks for memory utilization of EC2 instances, the answer involves the CloudWatch agent, because the default metrics do not include it.
Common mistakes
Looking for memory metrics in AWS/EC2. They are not there until the agent publishes them in its own namespace.
Watching averages only. A latency average can look healthy while the 99th percentile is bad.
Creating metrics with high cardinality dimensions. Each unique dimension value is a new metric and increases cost.
Expecting old high resolution data. One second data is kept for only 3 hours before rolling up.
Placing an instance ID in a dimension for short lived instances creates many metrics that go stale, so prefer dimensions such as the Auto Scaling group name.
Check yourself
Why do EC2 memory metrics not appear by default?
The hypervisor cannot see inside the operating system, so the CloudWatch agent must publish them.
How can an application create metrics without calling PutMetricData?
It writes log lines in embedded metric format, and CloudWatch extracts the metrics.
Where do you send metrics for storage beyond 455 days?
Use a metric stream to Firehose and store the data in S3.
Topic 1 of 5 in Monitoring and Logging
Test yourself on Monitoring and Logging
The full AWS DOP-C02 question bank has 844 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.