Sub topic 5
Publishing custom metrics
This chapter assumes you know what a namespace, a dimension, and a statistic are, and that you can run AWS CLI commands with a role that has cloudwatch:PutMetricData. Here you publish your own data points and read them back.
Why publish your own metrics
AWS services report metrics such as CPU utilization on their own. They cannot see what your application does. Page load time, items in a queue, or orders per minute are all application facts. A custom metric is a metric that you publish yourself into a namespace that you choose.
Do not use a namespace that starts with AWS/. That prefix is reserved for AWS services.
Publish one data point
The command below publishes one data point to the MyApp namespace.
aws cloudwatch put-metric-data \
--namespace MyApp \
--metric-name PageLoadTime \
--dimensions Service=checkout \
--unit Milliseconds \
--value 230Notice that put-metric-data writes dimensions as Service=checkout. The read commands use the longer Name=...,Value=... form. Mixing the two forms is a common source of errors.
Four things identify the metric: the namespace, the metric name, the dimension names, and the dimension values. The namespace and name above never change. If you publish with Service=cart, you create a different metric.
A new metric appears without any setup. Publishing the first data point creates it.
Read the data back
Use get-metric-statistics to confirm that the data arrived. You must repeat the exact namespace, name, and dimensions that you published with.
aws cloudwatch get-metric-statistics \
--namespace MyApp \
--metric-name PageLoadTime \
--dimensions Name=Service,Value=checkout \
--start-time 2026-11-26T18:00:00Z \
--end-time 2026-11-26T19:30:00Z \
--period 60 \
--statistics Average MaximumData can take a minute or two to show up. If the result is empty, check the time range first, then the dimensions.
Common errors
- Empty results. The query dimensions differ from the published ones. A metric published with one dimension is not found by a query with two.
- Wrong unit. The unit is stored with the data point. A query that filters on a different unit returns nothing, so choose a unit and keep it.
- Access denied. The caller lacks
cloudwatch:PutMetricData. This action does not support resource-level restrictions, so the policy uses*as the resource. - Too many dimension values. Each unique combination of dimension values is a separate metric and is billed as one. Do not use a request ID or user ID as a dimension.
Choosing how often to publish
Standard resolution metrics have a one-minute granularity. Publishing more often than that works, and CloudWatch combines the points when you ask for a one-minute period. Calls to put-metric-data also cost API requests, so batch several data points into one call when you publish from code.
Exercise
You publish PageLoadTime with --dimensions Service=checkout. A query with Name=Service,Value=checkout and Name=Env,Value=prod returns no data. Why?
Show answer
The query asks for a metric with two dimensions, Service and Env. The published metric has only the Service dimension, so it is a different metric. Remove the Env dimension from the query, or publish with both dimensions.