Lesson 2 of 4 Amazon CloudWatch and Log Analysis

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:

  • ConsoleLogin with errorMessage = "Failed authentication".
  • UnauthorizedOperation or AccessDenied errors.
  • 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.

The CIS root-usage alarm. The organisation trail delivers to a CloudWatch Logs group; a metric filter matches root activity not invoked by a service; the RootUsage metric triggers an alarm at one or more in 300 seconds, which notifies an encrypted SNS topic whose key policy must let CloudWatch use the key.
The scenario is illustrative.
True or false

Metric filters

Exam skills 1.1.4

Check how metric filters turn log events into metrics.

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

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.

FeatureNotes
Standard alarmA statistic over a period against a threshold; notifies SNS or triggers Auto Scaling, EC2 actions or OpsItems
Anomaly detectionLearns the expected range when there is no fixed threshold
Composite alarmCombines alarms to reduce noise
Missing data treatmentDecides whether sparse events trigger the alarm
Match the pairs

Alarm features

Exam skills 1.1.4 · 1.1.2

Match each alarm feature with the situation it suits.

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

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.
Select all that apply

Why an alarm stays silent

Exam skills 1.3.2

Pick the causes that can stop a security alarm from firing.

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

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-alerts

If 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.

Check yourself

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.