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.
Setup steps
- Configure the trail to send events to a CloudWatch Logs log group in the same account. The default role is
CloudTrail_CloudWatchLogs_Role. - Create a metric filter on that log group with a JSON filter pattern.
- Assign a metric namespace, name and value (usually 1). Leave the default value blank.
- Create an alarm on the metric. Statistic
Sum, period 5 minutes, threshold such as>= 1. - Attach an SNS topic to the In alarm state. Email subscribers must confirm the subscription.
Common patterns
| Detect | Filter pattern idea | Threshold |
|---|---|---|
| Unauthorized API calls | errorCode equals UnauthorizedOperation or AccessDenied* | Your chosen count in a period |
| Security group changes | eventName is one of AuthorizeSecurityGroupIngress, AuthorizeSecurityGroupEgress, RevokeSecurityGroupIngress, RevokeSecurityGroupEgress, CreateSecurityGroup, DeleteSecurityGroup | 1 in 5 minutes |
| Console sign-in failures | eventName = ConsoleLogin and errorMessage = "Failed authentication" | 3 in 5 minutes |
| IAM policy changes | eventName in PutRolePolicy, AttachUserPolicy, CreatePolicyVersion and the related put, attach, detach and delete calls | 1 |
| 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
| Need | Use |
|---|---|
| Count or rate of a log pattern, with a threshold | Metric filter plus alarm |
| React to a single API call immediately with automation | EventBridge rule on the CloudTrail event |
| Ad-hoc search of past logs | Logs Insights or Athena |
| Detect root MFA disabled | Security 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.