Skip to Content
Asterrr's Handbook
2 · Incident response

Metric Filters and Alarms

Turning CloudTrail events into CloudWatch metrics and alarms for unauthorized calls, security group changes and root usage.

The decision: how do you turn a CloudTrail event pattern into a threshold alert, and when is EventBridge the better choice?

CloudTrail has no alerting. To get an alert, send the trail to CloudWatch Logs, match events with a metric filter, then alarm on the metric. CloudWatch cannot decide what counts as "too many". You choose the threshold and period.

eventsmatchmetric = 1in ALARMemailCloudTrail trailLog groupMetric filterAlarmSNS topicSecurity team
The classic detective alert chain. Each step is a separate resource you must create.

Setup steps

  1. Configure the trail to send events to a CloudWatch Logs log group in the same account. The default role is CloudTrail_CloudWatchLogs_Role.
  2. Create a metric filter on that log group with a JSON filter pattern.
  3. Assign a metric namespace, name and value (usually 1). Leave the default value blank.
  4. Create an alarm on the metric. Statistic Sum, period 5 minutes, threshold such as >= 1.
  5. Attach an SNS topic to the In alarm state. Email subscribers must confirm the subscription.

Common patterns

DetectFilter pattern ideaThreshold
Unauthorized API callserrorCode equals UnauthorizedOperation or AccessDenied*Your chosen count in a period
Security group changeseventName is one of AuthorizeSecurityGroupIngress, AuthorizeSecurityGroupEgress, RevokeSecurityGroupIngress, RevokeSecurityGroupEgress, CreateSecurityGroup, DeleteSecurityGroup1 in 5 minutes
Console sign-in failureseventName = ConsoleLogin and errorMessage = "Failed authentication"3 in 5 minutes
IAM policy changeseventName in PutRolePolicy, AttachUserPolicy, CreatePolicyVersion and the related put, attach, detach and delete calls1
Root account use{ $.userIdentity.type = "Root" && $.userIdentity.invokedBy NOT EXISTS && $.eventType != "AwsServiceEvent" }1

The root filter excludes calls made by AWS services on the root's behalf and service events, so it fires only on real root activity.

Which mechanism for which alert

NeedUse
Count or rate of a log pattern, with a thresholdMetric filter plus alarm
React to a single API call immediately with automationEventBridge rule on the CloudTrail event
Ad-hoc search of past logsLogs Insights or Athena
Detect root MFA disabledSecurity Hub or Trusted Advisor check

Exam trap

A metric filter only counts events that arrive after it is created. It does not back-fill history. Use Logs Insights or Athena for past events.

Exam signal

"Notify when the root user is used" or "alert on more than N AccessDenied calls" maps to trail to CloudWatch Logs, metric filter, alarm, SNS. "Notify on a single event with no threshold" maps to EventBridge.

Further reading

On this page