Sub topic 18
Understanding and controlling CloudWatch cost
This chapter assumes you have read the earlier chapters on custom metrics, alarms, the CloudWatch agent, and Logs Insights. CloudWatch has no single fee. You pay separately for several features, and each feature has its own billing unit. Prices vary by region and change over time, so check the CloudWatch pricing page for current rates.
The main cost drivers
| Feature | What you are billed for |
|---|---|
| Custom metrics | Each unique metric per month. This includes metrics the agent publishes. |
| Log ingestion | Gigabytes of log data sent to CloudWatch Logs |
| Log storage | Gigabytes stored per month, until retention deletes them |
| Logs Insights | Gigabytes of log data scanned by each query |
| Alarms | Each alarm per month. High-resolution, composite, and anomaly detection alarms cost more than standard ones. |
| Dashboards | Each dashboard per month, after a free allowance |
| API requests | Calls such as `GetMetricData`, often made by third-party tools |
Metrics from AWS services, such as EC2 CPU utilization, are free at their default frequency. Detailed monitoring on EC2 (1-minute data) is an extra charge.
For most accounts, log ingestion is the largest line item. It is billed once when data arrives, so noisy logging is expensive from the first byte.
Why dimensions can make custom metrics expensive
A metric is identified by its namespace, name, and exact set of dimensions. Each unique combination is billed as a separate metric. If you add a UserId dimension to PageLoadTime and have 50,000 users, you create 50,000 billable metrics. Use dimensions with a small, bounded set of values, such as Service or Environment. Keep unbounded values such as user IDs, request IDs, and IP addresses in logs.
Controlling log cost
Log groups keep data forever by default. Set a retention period on every group:
aws logs put-retention-policy \
--log-group-name /app/web \
--retention-in-days 30To find groups that never expire, list those with no retention setting:
aws logs describe-log-groups \
--query 'logGroups[?retentionInDays==`null`].logGroupName' \
--output textOther controls:
- Lower the log level in production, for example INFO instead of DEBUG.
- Send logs that you rarely query to the Infrequent Access log class, which has a lower ingestion price and fewer features.
- Narrow the time range in Logs Insights and select specific log groups, because you pay for data scanned.
- Export old logs to S3 if you must keep them long term.
Controlling metric and alarm cost
- Collect only the agent metrics you use. Remove unused entries from
measurement. - Use standard 1-minute resolution unless you need faster data.
- Delete alarms and dashboards for resources that no longer exist.
- Use the Billing console, Cost Explorer, and a budget alert to see which usage types grow.
Exercise
A team adds a RequestId dimension to a custom metric. Why does the CloudWatch bill rise sharply, and what should they do instead?
Show answer
Every unique RequestId creates a new metric, and each unique metric is billed separately. They should remove the dimension and write the request ID to logs, then search it with Logs Insights.
Exercise
Your Logs Insights costs are high. Name two ways to reduce them.
Show answer
Query a shorter time range, and select only the log groups you need. Both reduce the gigabytes scanned, which is what Insights bills for.