Monitoring Applications with CloudWatch Metrics and Alarms
This lesson explains how Amazon CloudWatch collects metrics from AWS services and your own applications, and how alarms take action when those metrics cross a threshold.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how namespaces, metric names and dimensions identify a metric.
- Identify the ways custom metrics reach CloudWatch, including the embedded metric format.
- Explain how alarm states, statistics, periods and datapoints to alarm decide when an alarm fires.
- Describe how anomaly detection and composite alarms reduce false alarms.
Metrics
A CloudWatch metric is a time series of values, identified by a namespace, such as AWS/Lambda, a metric name, such as Errors, and a set of dimensions, such as FunctionName=orders-api. Metrics reach CloudWatch in four ways.
Standard metrics are published automatically by AWS services at no charge: Lambda reports invocations, errors, duration and throttles, and EC2 reports CPU utilisation and network traffic every five minutes, or every minute with detailed monitoring. Custom metrics are values that only the application knows, such as orders placed or payments declined, published with the PutMetricData API. They are stored at standard resolution of one minute or high resolution of down to one second. The Embedded Metric Format (EMF) creates custom metrics from specially formatted JSON log lines: the function only writes a log line, and CloudWatch extracts the metric asynchronously, which adds no latency and no API calls. The CloudWatch agent runs on EC2 instances and on-premises servers to collect values that the hypervisor cannot see, such as memory use and free disk space, as well as log files. The following table summarises the sources.
| Source | How it works |
|---|---|
| Standard metrics | Published automatically by AWS services |
| Custom metrics | PutMetricData at standard (1-minute) or high (down to 1-second) resolution, with up to 30 dimensions |
| Embedded Metric Format (EMF) | Metrics created from structured log lines, with no extra API calls; ideal for Lambda |
| CloudWatch agent | Required for values EC2 does not report, such as memory and disk usage |
Metrics and alarms
Alarms
An alarm watches a metric and is always in one of three states: OK, ALARM or INSUFFICIENT_DATA. Changing state can trigger an SNS notification, an Auto Scaling policy or an EC2 action.
Each alarm evaluates a statistic, such as the sum or the average, over a period, such as one minute, and compares it with a threshold. A single period above the threshold is often a harmless spike, so the alarm considers the most recent evaluation periods, and changes to ALARM only when a chosen number of them, the datapoints to alarm, breach. With 3 out of 5, a brief spike is ignored but a sustained problem is detected within a few minutes.
Some metrics have gaps, for example an error count that is published only when errors occur. The missing data treatment decides what a gap means: treating it as not breaching suits such error counts, treating it as breaching suits a heartbeat metric whose absence indicates a failure, and the default treats it as missing, which can leave the alarm in INSUFFICIENT_DATA. The following table summarises the settings.
| Setting | Controls |
|---|---|
| Period | The length of each evaluation window |
| Evaluation periods and datapoints to alarm | How many breaching windows are needed, for example 3 out of 5, so one spike does not raise an alarm |
| Missing data treatment | Whether gaps count as breaching |
Example. Publish a custom metric, then alarm when at least 3 of the last 5 one-minute periods have 10 or more errors. Add --storage-resolution 1 for a high-resolution metric.
aws cloudwatch put-metric-data --namespace MyApp --metric-name OrderErrors \
--dimensions Service=orders --value 1 --unit Count
aws cloudwatch put-metric-alarm --alarm-name orders-high-errors \
--namespace MyApp --metric-name OrderErrors --dimensions Name=Service,Value=orders \
--statistic Sum --period 60 --evaluation-periods 5 --datapoints-to-alarm 3 \
--threshold 10 --comparison-operator GreaterThanOrEqualToThreshold \
--treat-missing-data notBreaching \
--alarm-actions arn:aws:sns:eu-west-1:111122223333:on-callThe following diagram shows an alarm that uses 3 out of 5 datapoints to alarm.

Smarter alarms
CloudWatch provides alarm types that reduce false alarms and noise. The following list describes them.
- Anomaly detection learns a metric's normal pattern and alarms when values leave the expected band; it suits metrics with daily or weekly cycles.
- Composite alarms combine several alarms into one, reducing noise.
Choose the alarm design
An alarm that never fires
An alarm should notify the team when the custom metric OrderErrors exceeds 10 a minute. Errors happen, but the alarm stays in OK.
| Check | Finding |
|---|---|
| Metric name and namespace | The code publishes to myapp; the alarm watches MyApp (names are case-sensitive) |
| Dimensions | The code adds Environment=prod; the alarm has no dimension, so it watches a different metric |
| Statistic | Average of 1-error data points is 1; the alarm should use Sum |
Metrics are identified by namespace, name and the exact set of dimensions, so all three must match. After fixing them and switching the statistic to Sum, the alarm fires as expected.
How can a Lambda function publish a custom metric, such as orders processed, without an extra API call for each value?
Show answer
By writing log entries in the embedded metric format (EMF). CloudWatch extracts the metric from the logs, so the function does not call PutMetricData.
Exam Tips
Remember for the exam
- To emit a business metric from Lambda without extra API calls, use the Embedded Metric Format.
- EC2 memory utilisation is not a default metric; install the CloudWatch agent to collect it.
- To avoid alarms on single spikes, require several datapoints to alarm (M out of N).
- For a metric with daily patterns and no sensible fixed threshold, use an anomaly detection alarm.