Building Security Detections with Metric Filters and Alarms
This lesson explains how metric filters and alarms turn log patterns and metrics into notifications, the way many security detections are built with native AWS tools.
Learning outcomes
After completing this lesson, you will be able to:
- Write metric filters for security events in CloudTrail logs.
- Describe alarms on metric filter results.
- Explain how dashboards and Contributor Insights find top contributors.
- Diagnose why an alarm does not fire.
Metric filters
Match patterns in log events and publish a metric. Typical security filters on CloudTrail logs count:
ConsoleLoginwitherrorMessage = "Failed authentication".UnauthorizedOperationorAccessDeniederrors.- Root user activity; IAM and security group changes.
Filters apply only to events ingested after they are created.
The following diagram follows a root user action to the alert raised by a CIS root-usage alarm.

Metric filters
Alarms
A standard alarm compares a statistic over a period, such as the sum of a metric filter's matches in five minutes, with a threshold. When the threshold is crossed it notifies an SNS topic, or triggers Auto Scaling, EC2 actions or Systems Manager OpsItems. An anomaly detection alarm learns the metric's expected range from its history, which suits metrics with no sensible fixed threshold, such as API call volume that varies by time of day.
A composite alarm combines other alarms with AND and OR logic, so on-call staff are paged only when several conditions occur together, which reduces noise. Missing data treatment decides how an alarm treats periods with no data points. It matters for security metric filters, which produce data only when a matching event occurs. The following table summarises the features.
| Feature | Notes |
|---|---|
| Standard alarm | A statistic over a period against a threshold; notifies SNS or triggers Auto Scaling, EC2 actions or OpsItems |
| Anomaly detection | Learns the expected range when there is no fixed threshold |
| Composite alarm | Combines alarms to reduce noise |
| Missing data treatment | Decides whether sparse events trigger the alarm |
Alarm features
Dashboards and Contributor Insights
Dashboards share metrics across accounts and Regions. Contributor Insights finds top contributors, e.g. source IPs with the most rejected connections in flow logs.
When an alarm does not fire
An alarm can fail to fire for several reasons. The following list describes the most common causes.
- The filter pattern does not match the log format.
- Wrong statistic or period.
- Missing data treated as not breaching.
- An SNS topic policy, or its KMS key policy, blocks CloudWatch from publishing.
Why an alarm stays silent
Alarming on root user activity
The CIS benchmark expects an alarm whenever the root user is used. With the organisation trail delivering to a CloudWatch Logs group, a metric filter matches root activity that is not performed by an AWS service on the user's behalf:
aws logs put-metric-filter --log-group-name org-trail-logs --filter-name root-usage \
--filter-pattern '{ $.userIdentity.type = "Root" && $.userIdentity.invokedBy NOT EXISTS && $.eventType != "AwsServiceEvent" }' \
--metric-transformations metricName=RootUsage,metricNamespace=CISBenchmark,metricValue=1,defaultValue=0
aws cloudwatch put-metric-alarm --alarm-name cis-root-usage \
--namespace CISBenchmark --metric-name RootUsage --statistic Sum --period 300 \
--evaluation-periods 1 --threshold 1 --comparison-operator GreaterThanOrEqualToThreshold \
--treat-missing-data notBreaching --alarm-actions arn:aws:sns:eu-west-1:111111111111:security-alertsIf the alarm never fires during a test, check the pattern against a real event with aws logs test-metric-filter, and check that the encrypted SNS topic's key policy lets cloudwatch.amazonaws.com use the key. An EventBridge rule on ConsoleLogin by root achieves the same with lower latency, and many organisations use both.
How can you find the IP addresses generating the most rejected traffic?
Show answer
With Contributor Insights on VPC Flow Logs.
Exam Tips
Remember for the exam
- To alert on failed console sign-ins, create a metric filter and an alarm on CloudTrail logs.
- To find the IP addresses generating the most rejected traffic, use Contributor Insights on flow logs.
- With no fixed threshold, use an anomaly detection alarm.
- Metric filters do not process past log events.
- If an alarm cannot publish to an encrypted topic, fix the SNS or KMS policy.