Sub topic 10
Alerting on sensitive API calls
This chapter assumes you can read a CloudTrail event, know the difference between management and data events, and have a trail running. You also need an SNS topic and basic AWS CLI skills.
Why alert
Event history and S3 logs answer questions after the fact. Alerting reports a sensitive call soon after it happens. Typical targets are StopLogging, DeleteTrail, root user sign-ins, changes to IAM policies, and changes to security groups.
CloudTrail has no alerting feature of its own. You connect it to one of two services:
| Option | How it works | Best for |
|---|---|---|
| EventBridge | A rule matches an event pattern and sends the event to a target such as SNS or Lambda. | Fast, per-event reactions |
| CloudWatch Logs | The trail delivers to a log group. A metric filter counts matching lines and an alarm fires on the count. | Thresholds, such as 5 failed sign-ins in 10 minutes |
How the pieces connect
Alert with EventBridge
CloudTrail management events reach the default event bus in the Region where they occurred. These events use the detail type AWS API Call via CloudTrail. Your rule pattern matches fields from the event record you read earlier.
This rule fires when someone stops or deletes a trail:
aws events put-rule \
--name alert-trail-tampering \
--event-pattern '{
"source": ["aws.cloudtrail"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["cloudtrail.amazonaws.com"],
"eventName": ["StopLogging", "DeleteTrail", "UpdateTrail"]
}
}'
aws events put-targets \
--rule alert-trail-tampering \
--targets 'Id=sns,Arn=arn:aws:sns:us-east-1:123456789012:security-alerts'The SNS topic must allow EventBridge to publish to it. Without this policy, the rule matches but no message arrives. Save the following as policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowEventBridgePublish",
"Effect": "Allow",
"Principal": { "Service": "events.amazonaws.com" },
"Action": "sns:Publish",
"Resource": "arn:aws:sns:us-east-1:123456789012:security-alerts"
}
]
}Then attach it to the topic:
aws sns set-topic-attributes \
--topic-arn arn:aws:sns:us-east-1:123456789012:security-alerts \
--attribute-name Policy \
--attribute-value file://policy.jsonAlert with CloudWatch Logs
Use this path when a single event is normal but a pattern is not. First, send the trail to a log group. Then create a metric filter on the JSON fields. For example, { $.eventName = "ConsoleLogin" && $.responseElements.ConsoleLogin = "Failure" } counts failed console sign-ins. A CloudWatch alarm on that metric notifies the SNS topic when the count passes your threshold.
Common errors
- Wrong Region. A rule only sees events from its own Region. Global services such as IAM and the console sign-in report to
us-east-1. Create rules for those events there. - Missing topic policy. The rule shows matches in its metrics, but no notification arrives. Check the SNS policy first.
- Pattern mismatch. Patterns are case-sensitive and match exact values.
stoploggingnever matchesStopLogging. - Data events. EventBridge only receives S3 object-level or other data events if a trail records them. Check your event selectors.
- Alert fatigue. Match narrowly. Alerting on every
Describe*call produces noise that people learn to ignore.
Test it
Run aws cloudtrail update-trail on a test trail with no changes, or call another harmless matched action. Then wait for the message. EventBridge typically delivers within a minute or so. Delivery through S3 takes longer, so do not use the S3 files to check a rule.
Exercise
An EventBridge rule for iam:CreateAccessKey matches in its metrics, but the on-call engineer receives no email. Name the two most likely causes.
Show answer
The SNS topic policy does not allow events.amazonaws.com to publish, or the email subscription was never confirmed. If the rule had no matches at all, the cause would more likely be a rule created in the wrong Region (IAM events go to us-east-1) or a pattern that does not match the event fields.