Lesson 2 of 5 CloudWatch Logs and the CloudWatch Agent

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=0

The following diagram shows a metric filter feeding an alarm.

Alarming on application errors with a metric filter. A filter pattern on the log group matches new events, such as ERROR without Retrying, and publishes 1 per match to a custom metric, with a default value of 0 when nothing matches. An alarm on the metric then notifies through SNS. Events that arrived before the filter was created are not counted.
The pattern and threshold are illustrative.
True or false

Metric filters

Exam skills 1.1.1 · 1.1.3

Check how metric filters turn log events into metrics.

Log in to try this activity Interactive activities are free with an account.

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.

Centralising logs with subscription filters. Each source account's subscription filter sends matching events in near real time to a CloudWatch Logs destination in the central account. The destination's access policy lists the allowed accounts and fronts a Firehose stream, which delivers to S3 or OpenSearch Service. A log group can have up to two subscription filters.
OS stands for Amazon OpenSearch Service. The destination's access policy is what lets other accounts send to it.

Exporting and other options

Three ways to get logs out of CloudWatch Logs are often confused. The following table compares them.

MethodLatencyTypical use
CreateExportTask to S3Up to 12 hours; one active task per accountOccasional bulk export for archive or audit
Subscription filter to FirehoseNear real time (buffering seconds to minutes)Continuous delivery to S3, OpenSearch or a SIEM
Subscription filter to LambdaNear real timeCustom processing or alerting on each event
Sort into groups

Real time or batch?

Exam skills 1.1.1

Choose how to move log data to each destination.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.