Sub topic 3
Turning Log Events into Metrics with Metric Filters
Assume you know AWS regions, IAM permissions, the AWS CLI, and the difference between logs and metrics. This chapter connects CloudWatch log analysis to alarms, dashboards, and automated responses.
Turning log events into metrics with metric filters
A metric filter watches events in a CloudWatch Logs log group. When an event matches its pattern, the filter publishes a metric value.
aws logs put-metric-filter \
--log-group-name /app/web \
--filter-name ErrorCount \
--filter-pattern "ERROR" \
--metric-transformations metricName=ErrorCount,metricNamespace=MyApp,metricValue=1,defaultValue=0This filter adds one to MyApp/ErrorCount for each event containing ERROR. You can alarm on that metric or place it on a dashboard. Choose patterns carefully. A broad pattern can create noisy alerts and unnecessary custom-metric charges.
Querying logs with Logs Insights
Logs Insights searches existing log events without requiring a metric filter. It is useful for investigation and reports.
fields @timestamp, @message
| filter @message like /ERROR/
| stats count() as errors by bin(5m)
| sort errors desc
| limit 20This query counts errors in five-minute bins and shows the busiest bins.
Building dashboards
A CloudWatch dashboard combines metric widgets from services, custom namespaces, and resources. Add CPU, request errors, latency, and ErrorCount together so operators can compare related signals. Keep widgets tied to decisions, such as scaling or rollback.
Reducing alert noise with composite alarms
A composite alarm combines the states of other alarms. For example, alert only when high CPU and high error rate occur together. This reduces duplicate notifications, but it can hide a real problem if one child alarm is misconfigured.
Anomaly detection alarms
Anomaly detection learns an expected metric range from historical data. Use it for workloads with changing baselines, such as traffic. Review the model after deployments and seasonal changes.
Reacting with EventBridge
EventBridge can match CloudWatch alarm state-change events and invoke Lambda, Systems Manager, or another target. Use it when a state transition needs workflow automation rather than only an SNS message.
Synthetics canaries
A Synthetics canary runs scripted requests against an endpoint. It tests user-visible behavior, including routes that internal metrics may miss.
Cross-account and cross-region observability
CloudWatch cross-account dashboards and monitoring let a central account view metrics from other accounts and regions. Configure IAM sharing deliberately and preserve the source account and region in widget labels.
Understanding and controlling cost
Costs depend mainly on custom metric volume, high-cardinality dimensions, log ingestion and storage, Logs Insights queries, dashboards, alarms, and canary runs. Set log retention, avoid unnecessary dimensions, query narrow time ranges, and delete unused dashboards and alarms.
Exercise
Why might a metric filter be preferable to querying logs every time an alarm is needed?
Show answer
It continuously converts matching events into a metric that an alarm can evaluate automatically.