Sub topic 3
Namespaces and dimensions: identifying a metric
This chapter assumes you know what a metric and a data point are (covered in the previous chapter), that you can run AWS CLI commands, and that you have an EC2 instance or another resource to look at. Here you will learn how CloudWatch decides which metric a data point belongs to, and why a small mistake in naming can leave you with a metric that looks right but stays empty.
A metric is identified by three things
CloudWatch does not store a metric under a single name. A metric is uniquely identified by its namespace, its metric name, and its full set of dimensions. If any one of these differs, CloudWatch treats it as a different metric.
- A namespace is a container that groups related metrics, such as
AWS/EC2orMyApp. - A metric name is the thing being measured, such as
CPUUtilizationorPageLoadTime. - A dimension is a name/value pair that further identifies a metric, such as
InstanceId=i-0123456789abcdef0.
Namespaces
Namespaces keep metrics from different sources apart. Two services can both publish a metric called Latency without colliding, because each lives in its own namespace.
AWS services publish to namespaces that begin with AWS/. Examples include AWS/EC2, AWS/RDS, AWS/Lambda, and AWS/ApplicationELB. You cannot publish your own metrics into a namespace that starts with AWS/.
For your own metrics, you choose the namespace. Some rules to keep in mind:
- A namespace is a string of up to 255 characters. Letters, numbers, and a few punctuation characters such as
.,-,_,/,#, and:are allowed. - A namespace is created automatically the first time you publish a metric to it. You do not create it separately.
- Metrics in one namespace are not automatically combined with metrics in another. A query always names one namespace.
- The CloudWatch agent publishes to the
CWAgentnamespace by default. You can change this in the agent configuration.
A common convention is to use a short, stable namespace per application or team, such as MyApp or Payments/Checkout. Avoid putting a version number or an environment in the namespace. Use a dimension for those instead, so you can compare them in one graph.
Dimensions
A dimension lets you slice one metric by an attribute. CPUUtilization in AWS/EC2 exists once per instance because each instance has its own InstanceId dimension value.
Limits to know:
- A metric can have up to 30 dimensions.
- Each dimension has a name and a value. Both are strings.
- Dimension names and values cannot be empty and must contain only ASCII characters.
Exact matching: the main gotcha
CloudWatch matches dimensions exactly. A query must supply the same dimension names, and the same number of them, that were used when the data was published. CloudWatch does not roll up data across dimensions for you.
Suppose you publish PageLoadTime like this:
aws cloudwatch put-metric-data \
--namespace MyApp \
--metric-name PageLoadTime \
--dimensions Service=checkout \
--unit Milliseconds \
--value 230This creates one metric: namespace MyApp, name PageLoadTime, dimension Service=checkout. Now consider what each query returns.
| Query | Result |
|---|---|
| `MyApp`, `PageLoadTime`, `Service=checkout` | Returns the data point. |
| `MyApp`, `PageLoadTime`, no dimensions | Returns nothing. This is a different metric. |
| `MyApp`, `PageLoadTime`, `Service=checkout,Env=prod` | Returns nothing. The dimension set differs. |
| `MyApp`, `PageLoadTime`, `Service=Checkout` | Returns nothing. Values are case sensitive. |
Querying a metric with the wrong dimensions does not produce an error. You get an empty result, which is easy to mistake for "no traffic". If a graph or alarm shows no data, check the dimensions first.
Retrieving a metric with its dimensions
The CLI form for reading a metric uses Name=...,Value=... pairs. Here is the query for an EC2 instance:
aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=i-0123456789abcdef0 \
--start-time 2026-11-26T18:00:00Z \
--end-time 2026-11-26T19:00:00Z \
--period 300 \
--statistics Average MaximumNote that put-metric-data accepts the short form Service=checkout, while get-metric-statistics and put-metric-alarm use Name=InstanceId,Value=... in the examples here. Both are valid syntaxes for the same dimension structure. The CLI accepts either form for these commands, but mixing them up in scripts is a common source of copy-paste errors.
Finding out which metrics exist
Before you build an alarm, list what is actually there. list-metrics shows the exact namespace, name, and dimensions CloudWatch has stored:
aws cloudwatch list-metrics \
--namespace MyApp \
--metric-name PageLoadTimeThe output contains a Dimensions array for each metric. Copy the names and values exactly from this output into your alarm or query. This is the fastest way to resolve an "empty graph" problem.
Keep in mind that list-metrics only returns metrics that received data in roughly the last two weeks. A metric with no new data eventually stops appearing in the list, although its older data remains retrievable by its exact identity until it expires.
Aggregation and dimension design
Because there is no automatic roll-up, the dimensions you choose when you publish decide which questions you can answer later.
Say you publish PageLoadTime with the dimensions Service and Host. You can read the metric per service and host. You cannot ask for the average for the whole checkout service across hosts, unless you also published a data point with only Service=checkout.
You have three options:
- Publish at each level you need. Send one data point with
Service,Hostand another with onlyService. This costs more, because each unique combination is a separate custom metric. - Use metric math. Combine per-host metrics at query time with expressions such as
SEARCHand aggregation functions. This avoids extra metrics, but it is available in graphs and alarms, not in every API. - Choose fewer dimensions. Keep only the ones you will really filter by.
High cardinality is a cost problem
Cardinality is the number of distinct dimension combinations. Every unique combination of namespace, name, and dimensions is a separate metric, and custom metrics are billed per metric.
Putting a request ID, user ID, or timestamp in a dimension creates a new metric for every value. This inflates cost and makes the metrics useless for alarms, since each one receives a single data point. Use dimensions for bounded sets such as service, environment, region, or instance type. Put unbounded identifiers in logs instead.
Common errors
- Empty graph after publishing. The query's dimension set does not match the published set. Run
list-metricsand copy the dimensions exactly. - Alarm stuck in
INSUFFICIENT_DATA. The alarm's dimensions point to a metric that has no data, often after an instance is replaced and itsInstanceIdchanges. - Case mismatch.
Service=CheckoutandService=checkoutare separate metrics. - Publishing to
AWS/.... The call is rejected, because that prefix is reserved for AWS services.
Exercise
You publish a metric to namespace MyApp named Errors with dimensions Service=checkout and Env=prod. A teammate queries MyApp, Errors, with only Service=checkout and gets no data. Why, and what are two ways to get the combined view?
Show answer
CloudWatch matches the full dimension set exactly, so Service=checkout alone is a different metric from Service=checkout,Env=prod. The query must include both dimensions. To get a view across a dimension, either publish an extra data point that carries only Service=checkout, or use metric math or a SEARCH expression to combine the per-environment metrics at query time. Running list-metrics shows the exact dimension sets that exist.
Exercise
Why is it a bad idea to use a user ID as a dimension value?
Show answer
Each distinct user ID creates a separate metric, so the number of billed custom metrics grows with your user base. Each metric also gets very few data points, which makes alarms unreliable. Record user IDs in logs instead, and query them with Logs Insights.