Sub topic 18
Understanding and controlling CloudWatch cost
This chapter assumes you can run AWS CLI commands and that you know the CloudWatch pieces from earlier chapters: custom metrics, alarms, log groups, the agent, Logs Insights, and dashboards. Here you will learn which of those pieces generate charges, how to estimate them, and which settings reduce them.
The prices below are examples from the us-east-1 price list at the time of writing. They vary by region and change over time, so check the CloudWatch pricing page before you budget.
How CloudWatch charges
Most AWS services metered per hour or per request. CloudWatch is metered per feature, and each feature scales with a different quantity.
| Feature | What you pay for | Example rate (us-east-1) |
|---|---|---|
| Custom metrics | Each unique metric per month | About $0.30 for the first 10,000 |
| Standard alarms | Each alarm per month | About $0.10 |
| High-resolution alarms | Each alarm per month | About $0.30 |
| Composite alarms | Each alarm per month | About $0.50 |
| Log ingestion | GB of log data received | About $0.50 per GB (Standard class) |
| Log storage | GB stored per month | About $0.03 per GB |
| Logs Insights | GB of data scanned by queries | About $0.005 per GB |
| Dashboards | Dashboards beyond the free allowance | About $3 each per month |
| API calls | Requests such as GetMetricData | About $0.01 per 1,000 metrics requested |
Metrics that AWS services publish by default, such as basic EC2 CPU and network metrics, are free. You pay when you turn on extras such as EC2 detailed monitoring (1-minute data) or publish your own data.
Custom metrics: cardinality is the cost
CloudWatch treats every unique combination of namespace, metric name, and dimension values as a separate metric. The put-metric-data call from the custom metrics chapter used Service=checkout. If you add a UserId dimension, every user creates a new metric.
Suppose you publish 5 metrics and add a UserId dimension to each, with 2,000 active users:
- 5 metrics × 2,000 users = 10,000 metrics
- 10,000 × $0.30 = about $3,000 per month
Without UserId, the same data costs 5 × $0.30 = $1.50 per month. High-cardinality values such as user IDs, request IDs, session IDs, and IP addresses do not belong in dimensions. Put them in logs instead, and use Logs Insights to find individual users.
Note also that CloudWatch does not roll metrics up for you. A metric published with dimensions Service and Env is a different metric from one published with Service alone. If you want both views, you must publish both, and you pay for both. Choose the few dimension sets you will actually graph or alarm on.
Agent settings that multiply metrics
The agent configuration from the agent chapter used "resources": ["*"] for disk metrics. That reports every mounted filesystem, and each one becomes its own metric per instance. On a fleet of 100 instances with 6 mounts each, that is 600 disk metrics instead of 100. Name the paths you care about:
{
"agent": { "metrics_collection_interval": 60 },
"metrics": {
"append_dimensions": { "InstanceId": "${aws:InstanceId}" },
"metrics_collected": {
"mem": { "measurement": ["mem_used_percent"] },
"disk": { "measurement": ["used_percent"], "resources": ["/"] }
}
}
}The collection interval does not change the number of custom metrics, so it does not change the monthly metric charge. It does change how much data you can graph and alarm on. Going below 60 seconds (high resolution) raises alarm prices, so use it only where you need it.
Logs: ingestion usually dominates
For most accounts, log ingestion is the largest CloudWatch line item, because you pay for every GB at the moment it arrives. Storage is cheaper per GB, but it keeps accumulating if you never delete anything.
Set retention everywhere
A new log group keeps data forever by default. Find the groups with no retention policy:
aws logs describe-log-groups \
--query 'logGroups[?retentionInDays==`null`].logGroupName' \
--output textApply a 30-day policy to all of them. Review the list first, because some groups may need longer retention for compliance:
aws logs describe-log-groups \
--query 'logGroups[?retentionInDays==`null`].logGroupName' \
--output text | tr '\t' '\n' | while read -r group; do
[ -n "$group" ] && aws logs put-retention-policy \
--log-group-name "$group" \
--retention-in-days 30
doneRetention only controls storage. It does nothing for ingestion, which you have already paid for.
Reduce what you ingest
- Lower the log level in production. DEBUG output in a busy service can be most of your bill.
- Do not log full request and response bodies unless you need them.
- Ship only the files you need. Each
collect_listentry in the agent configuration is a source of ingestion. - Check which groups are largest. The
IncomingBytesmetric in theAWS/Logsnamespace is published per log group, so you can graph it and find the heaviest ones. - Consider the Infrequent Access log class for logs you rarely read. Ingestion costs less, but some features are missing, including metric filters and subscription filters. Choose the class when you create the log group, because you cannot change it afterward.
- Archive old data to S3 if you must keep it for years, because S3 storage is cheaper than CloudWatch storage.
Logs Insights scans
Logs Insights charges by data scanned, so a query over 30 days of a large group costs about 30 times what a one-day query does. Select only the log groups you need, keep the time range as narrow as the question allows, and use filter early in the query. Filtering does not reduce the scanned amount within the selected time range, so the time range matters most.
Alarms and dashboards
Alarm costs are small per alarm and large at scale. Per-instance alarms on 500 instances at $0.10 each cost $50 per month, and a composite alarm at $0.50 is not free either. Review these points:
- Delete alarms for resources that no longer exist. An alarm stuck in
INSUFFICIENT_DATAfor a terminated instance still costs money. - Use high-resolution alarms only when 10- or 30-second evaluation matters.
- Use composite alarms to reduce notifications, not to reduce cost, since each one is an additional alarm.
- Anomaly detection alarms count toward the bill using more than one metric's worth of charges, so use them where static thresholds do not work.
Dashboards include a small free allowance (three dashboards with up to 50 metrics each). Beyond that, each dashboard has a monthly charge. A dashboard also issues API calls whenever someone has it open with auto-refresh, and large widgets that request many metrics add to the GetMetricData charge. Close long-running dashboard tabs, and use a slower refresh interval on wall displays.
Finding where the money goes
Use these tools in order:
- Cost Explorer. Group by service (CloudWatch), then by usage type. Usage types with names containing
DataProcessing-Bytespoint to log ingestion,MetricMonitorUsageto custom metrics, andAlarmMonitorUsageto alarms. - The
AWS/Logsnamespace. GraphIncomingBytesper log group to find the heaviest sources. list-metrics. Count your metrics per namespace to find cardinality problems:
aws cloudwatch list-metrics --namespace MyApp \
--query 'length(Metrics)'If that number is far larger than the number of metric names times the dimension sets you intended, a high-cardinality dimension is the cause.
A budget alarm completes the picture. AWS Budgets can alert you when CloudWatch spending forecasts to exceed a limit, so a runaway log loop is caught in days rather than at the end of the month.
Common mistakes
- Putting user, request, or session IDs in metric dimensions.
- Leaving log groups at the default "never expire" retention.
- Leaving DEBUG logging enabled after an incident.
- Using
"resources": ["*"]for disk metrics on hosts with many mounts. - Running wide Logs Insights queries repeatedly over long time ranges from a script.
- Forgetting alarms and dashboards after the resources they watch are gone.
Exercise
Your service publishes 4 custom metrics to the MyApp namespace. A developer adds a CustomerId dimension to all of them, and the service has 1,500 customers. At about $0.30 per metric per month, what is the new monthly cost of these metrics, and what would you change?
Show answer
There are 4 × 1,500 = 6,000 unique metrics, so about 6,000 × $0.30 = $1,800 per month, compared with about $1.20 before the change. Remove CustomerId from the dimensions and publish the metrics with only low-cardinality dimensions such as Service. Write the customer ID into the application log instead, and use Logs Insights to look at one customer.
Exercise
A log group was created two years ago with default settings, and its storage cost keeps growing. Which command limits storage to 30 days, and why does it not reduce the ingestion charge?
Show answer
Run aws logs put-retention-policy --log-group-name <name> --retention-in-days 30. Retention deletes old events and so limits storage. Ingestion is charged when data arrives, so only logging less data, or using the Infrequent Access class for new groups, reduces that charge.