Sub topic 1
What CloudWatch does: metrics, logs, alarms, and dashboards
Amazon CloudWatch is the AWS service that collects telemetry from your resources and applications, stores it, and lets you act on it. This chapter gives you the working model: which kinds of data CloudWatch holds, how they relate, and where each later chapter fits.
Prerequisites
This chapter assumes you have:
- An AWS account, and an understanding that most services are scoped to a region.
- Basic IAM knowledge: users and roles, and how a policy allows an action.
- Working knowledge of EC2 or another compute service such as Lambda or ECS.
- Enough comfort with the AWS CLI to run a command and read JSON output.
- A general idea of what a log file and a numeric metric are.
The four building blocks
CloudWatch is several features that share one data store and one permission model. Four of them matter most.
| Building block | What it holds | Typical question it answers |
|---|---|---|
| Metrics | Time-ordered numeric data points | "How busy was this instance over the last hour?" |
| Logs | Text events grouped into log groups and streams | "What did the application print when the request failed?" |
| Alarms | A rule that watches one metric (or other alarms) and has a state | "Tell me when error rate stays high." |
| Dashboards | Saved pages of graphs and numbers | "Show me the health of the whole checkout service." |
Metrics and logs are the data. Alarms and dashboards are the ways you use that data. Other features in CloudWatch, such as Logs Insights, metric filters, anomaly detection, and Synthetics canaries, build on these four.
Metrics
A metric is a named series of data points. Each data point has a timestamp, a value, and usually a unit. Many AWS services publish metrics for you without any setup. For example, EC2 publishes CPUUtilization to the AWS/EC2 namespace. You can also publish your own, called custom metrics, which a later chapter covers.
By default, EC2 reports at 5-minute granularity (basic monitoring). Detailed monitoring reports every minute and costs extra. Some things are not reported by default at all. EC2 metrics come from the hypervisor, so CloudWatch cannot see inside the operating system. Memory use and disk space used need the CloudWatch agent, which is covered later.
CloudWatch keeps metric data for a fixed time, and older data is stored at coarser resolution:
| Data resolution | Retained for |
|---|---|
| Under 60 seconds (high resolution) | 3 hours |
| 60 seconds | 15 days |
| 5 minutes | 63 days |
| 1 hour | 455 days (15 months) |
You do not choose these tiers. CloudWatch aggregates as data ages. A one-minute spike you can see today may be averaged away in a graph of data from three months ago.
Logs
CloudWatch Logs stores text events. It organizes them in two levels:
- A log group is a container for related logs, often one per application or service, such as
/app/web. Retention, encryption, and access control are set here. - A log stream is an ordered sequence of events from a single source inside the group, such as one instance or one container.
The default retention for a new log group is "never expire". This is a common source of surprise cost, and you should set a retention period deliberately. Logs and cost are covered in their own chapters.
Alarms
A metric alarm watches one metric over a number of periods and compares it to a threshold. At any moment, it is in one of three states:
OK: the metric is within the threshold.ALARM: the threshold has been breached according to the rule.INSUFFICIENT_DATA: not enough data has arrived to decide.
An alarm can run actions when it changes state, such as publishing to an SNS topic, which then sends email or calls a webhook. Alarms do not fix anything by themselves. They tell something else to act.
Dashboards
A dashboard is a customizable page of widgets. Widgets can graph metrics, show a single number, display a Logs Insights query result, or list alarm states. Dashboards are not tied to one region: a single dashboard can mix widgets from several regions, and with cross-account setup, from several accounts.
How the pieces connect
Data flows in from AWS services, from the CloudWatch agent on your hosts, and from your own code. Once stored, you can graph it, alarm on it, or derive new data from it. Logs can even turn into metrics, by way of metric filters.
Notice that the arrow from logs to metrics gives you a way to alarm on something that no service publishes directly, for example the count of ERROR lines.
Seeing it in the CLI
You do not need to build anything yet. These two read-only commands show what already exists in your account and region.
aws cloudwatch list-metrics --namespace AWS/EC2 --metric-name CPUUtilization
aws logs describe-log-groups --query 'logGroups[].[logGroupName,retentionInDays]' --output tableThe first command lists each CPUUtilization metric, with the dimensions that identify it, such as InstanceId. The second lists your log groups. A retention value of None in the table means the group never expires.
The CLI uses your configured default region. CloudWatch is regional, so metrics and logs from us-east-1 do not appear when you query eu-west-1. If a list is unexpectedly empty, check the region first.
Common mistakes
- Expecting memory or disk metrics by default. EC2 does not report them. Install and configure the CloudWatch agent.
- Forgetting regions. An alarm must be in the same region as the metric it watches. Dashboards are the main exception, since they can show several regions.
- Leaving log retention at "never expire". Storage grows and is billed monthly.
- Reading old graphs as if they were precise. Older data has been aggregated to coarser periods.
- Treating
INSUFFICIENT_DATAas healthy. It means the alarm cannot judge. It does not mean everything is fine. Later chapters cover how to handle missing data on purpose.
What comes next
The following chapters take each block in turn. You will first learn how a metric is identified by namespace and dimensions, then how statistics and periods shape what you read. After that come custom metrics, alarms and their actions, logs, the agent, metric filters, Logs Insights, and dashboards. The later chapters cover noise reduction, anomaly detection, EventBridge, canaries, cross-account views, and cost.
Exercise
An EC2 instance runs an application that is slowing down because it is running out of memory. You open the CloudWatch metrics for the instance in the AWS/EC2 namespace and find no memory metric. Why, and what do you need to do?
Show answer
EC2's built-in metrics come from the hypervisor, which cannot see inside the guest operating system, so memory use is not published by default. To get it, install and configure the CloudWatch agent on the instance so that it publishes memory metrics, which appear as custom metrics. You can then graph them and alarm on them.
Exercise
You run aws logs describe-log-groups and see None for retentionInDays on every group. What does that mean, and why might it matter?
Show answer
It means the log groups never expire, so every event is kept and billed for storage until you delete it. Over time this raises cost, so you should set a retention period that fits your needs.
Exercise
A teammate says an alarm in INSUFFICIENT_DATA state proves the service is healthy. Explain what is wrong with that.
Show answer
INSUFFICIENT_DATA means the alarm does not have enough data points to decide whether the threshold is breached. The metric may have stopped being reported because the source failed, so the state says nothing about health.