Turning Logs into Metrics and Streams with Filters
This lesson explains how metric filters turn log patterns into metrics that alarms can watch, and how subscription filters stream log events to other services in real time.
Learning outcomes
After completing this lesson, you will be able to:
- Create metric filters with filter patterns.
- Stream logs with subscription filters.
- Choose between export, subscription and other options.
Metric filters
A metric filter applies a filter pattern to new events in a log group and publishes a metric value, usually 1 per match, to a namespace and metric you choose. It does not process events that arrived before it was created. Patterns can match terms (ERROR), exclude terms (ERROR -Retrying), match space-delimited fields ([ip, user, ..., status=5*, size]) or match JSON properties ({ $.eventName = "ConsoleLogin" && $.errorMessage = "Failed authentication" }). Setting a default value of 0 publishes zero when no events match, which keeps the alarm out of INSUFFICIENT_DATA.
Metric filters on a log group that receives CloudTrail events are the classic way to alert on security-relevant API calls, such as root user sign-in, security group changes or unauthorised calls, as recommended by the CIS AWS Foundations Benchmark.
aws logs put-metric-filter --log-group-name CloudTrail/management \
--filter-name RootUsage \
--filter-pattern '{ $.userIdentity.type = "Root" && $.userIdentity.invokedBy NOT EXISTS && $.eventType != "AwsServiceEvent" }' \
--metric-transformations metricName=RootAccountUsage,metricNamespace=CISBenchmark,metricValue=1,defaultValue=0The following diagram shows a metric filter feeding an alarm.

Metric filters
Subscription filters
A subscription filter sends matching events, in near real time, to Amazon Data Firehose (for delivery to S3, OpenSearch Service or a partner), Kinesis Data Streams, a Lambda function or Amazon OpenSearch Service. A log group can have up to two subscription filters. To centralise logs from many accounts, each source account's subscription filter sends to a destination in the central account that fronts a Firehose stream or Kinesis stream, and the destination's access policy lists the allowed accounts. Account-level subscription filter policies apply one filter to all log groups in an account, which avoids configuring each group.
The following diagram shows logs from several accounts centralised through a destination.

Exporting and other options
Three ways to get logs out of CloudWatch Logs are often confused. The following table compares them.
| Method | Latency | Typical use |
|---|---|---|
| CreateExportTask to S3 | Up to 12 hours; one active task per account | Occasional bulk export for archive or audit |
| Subscription filter to Firehose | Near real time (buffering seconds to minutes) | Continuous delivery to S3, OpenSearch or a SIEM |
| Subscription filter to Lambda | Near real time | Custom processing or alerting on each event |
Real time or batch?
Alarm on application errors
An application on EC2 writes lines such as 2026-10-12T09:15:02Z ERROR PaymentGateway timeout order=8812 to /var/log/app.log, which the CloudWatch agent ships to the /app/prod log group. Operations wants an alert when more than 10 payment timeouts occur in 5 minutes. A metric filter with the pattern "ERROR" "PaymentGateway timeout" publishes PaymentTimeouts with value 1 and default value 0. An alarm on the Sum of PaymentTimeouts over 5 minutes, threshold 10, notifies the on-call SNS topic. Because the filter only processes new events, the team tests it with aws logs test-metric-filter against sample lines before relying on it.
Which destinations can a subscription filter stream log events to?
Show answer
Amazon Data Firehose, Kinesis Data Streams, Lambda or Amazon OpenSearch Service, in near real time.
Exam Tips
Remember for the exam
- Metric filters only process events that arrive after the filter is created.
- Set a default value of 0 so the metric reports when nothing matches.
- Stream logs in near real time with subscription filters to Firehose, Kinesis, Lambda or OpenSearch Service.
- Centralise logs across accounts with subscription filters to a cross-account destination.
- Export tasks are for occasional bulk export, not continuous delivery.