What Is a Metric Filter?
A metric filter inspects each log event in a CloudWatch Logs group against a filter pattern. Whenever an event matches, CloudWatch Logs emits a metric datum—either incrementing a counter or setting a value. Once published, you can:- Trigger CloudWatch Alarms
- Plot the data on CloudWatch Dashboards
- Automate responses with EventBridge or Lambda
Metric filters operate in near real‐time and can be applied to both text and JSON‐formatted logs.
How It Works
- Define a filter pattern (e.g.,
"ERROR","[timestamp, requestId, ...]"). - Attach the filter to a log group in CloudWatch Logs.
- Configure the filter to publish metric data—choose a namespace, metric name, and value.
- Use CloudWatch Metrics to visualize data or set alarms on thresholds.

Example: Tracking HTTP 404 Errors
Monitor spikes in “HTTP 404” errors by turning each occurrence into a custom metric.1. Define the Filter Pattern
2. Associate with Your Log Group
3. Publish Metric Data
Each time a log line containsHTTP 404, CloudWatch Logs will emit a MyApp-404Errors metric with a value of 1.
4. Create an Alarm
Overly broad filter patterns can lead to high metric‐filter charges. Always scope patterns tightly and test with sample logs.
Real-World Use Cases
| Use Case | Filter Pattern | Metric Name |
|---|---|---|
| API Latency | { $.latency = * } | MyApp/APIResponseTime |
| Login Failures | "Authentication failure" | MyApp/LoginFailures |
| Disk Utilization | { $.diskUsage > 80 } | MyApp/DiskUtilization |
| Database Errors | "SQL ERROR" | MyApp/DatabaseErrors |