Sub topic 11
Automating alerts with EventBridge
Reading findings in the console works for a first pass. It does not scale to the point where someone must react within minutes to a public bucket. IAM Access Analyzer publishes findings to Amazon EventBridge, so you can route them to email, chat, a ticket queue, or code that remediates them. This chapter builds that pipeline with the AWS CLI.
This chapter assumes you have created an analyzer, can read a finding's fields, and know how archive rules affect finding status. It also assumes you are comfortable with JSON policies and the AWS CLI.
How findings reach EventBridge
Access Analyzer sends events to the default event bus in the same account and Region as the analyzer. It sends an event when it creates a finding and when a finding's status changes, for example from ACTIVE to RESOLVED. An event carries the finding's fields in its detail object.
The envelope uses source aws.access-analyzer. An external access finding has detail-type Access Analyzer Finding. The finding itself is in detail:
| Field in `detail` | What it tells you |
|---|---|
| `id` | The finding ID. Pass it to `get-finding-v2` or `get-finding-recommendation`. |
| `status` | `ACTIVE`, `ARCHIVED`, or `RESOLVED`. |
| `resourceType` | The kind of resource, such as `AWS::S3::Bucket`. |
| `resource` | The ARN of the affected resource. |
| `accountId` | The account that owns the resource. |
| `principal` | Who is granted access, for external access findings. |
| `isPublic` | Whether the finding grants public access. |
Treat the field list as a starting point. Event schemas can gain fields, and unused access findings carry different details than external access findings. Capture a real event before you write filters against it (see "Inspecting a real event" below).
Start with a broad pattern
The simplest pattern matches every Access Analyzer finding event:
{
"source": ["aws.access-analyzer"],
"detail-type": ["Access Analyzer Finding"]
}This alerts on every status change, including findings an archive rule archives and findings that become resolved. Narrow it so that people only get paged for what needs action.
Filter to findings worth a notification
EventBridge patterns match on nested fields under detail. This pattern matches only active findings for public access:
{
"source": ["aws.access-analyzer"],
"detail-type": ["Access Analyzer Finding"],
"detail": {
"status": ["ACTIVE"],
"isPublic": [true]
}
}Two design choices matter here:
- Filter on
status. Without"ACTIVE", you get a second notification when someone archives or fixes the finding. That is useful for an audit log, but noisy for a pager. - Filter on specific resource types when different teams own different services. Add
"resourceType": ["AWS::S3::Bucket"]to route bucket findings to the storage team's rule.
Archive rules and rule filters work together. Archive rules keep expected findings from becoming ACTIVE, and the ACTIVE filter on the event rule keeps archived findings from alerting anyone. Maintain the archive rules and the EventBridge patterns as a pair.
Build the pipeline with the CLI
The example sends public-access findings to an SNS topic. It uses account 111122223333 and Region us-east-1. Create the topic and an email subscription:
aws sns create-topic --name access-analyzer-alerts
aws sns subscribe \
--topic-arn arn:aws:sns:us-east-1:111122223333:access-analyzer-alerts \
--protocol email \
--notification-endpoint [email protected]The recipient must confirm the subscription by following the link in the confirmation email. Until then, nothing is delivered.
Save the event pattern from the previous section as pattern.json, then create the rule:
aws events put-rule \
--name access-analyzer-public-findings \
--description "Notify on new public access findings" \
--event-pattern file://pattern.jsonEventBridge can only publish to the topic if the topic's resource policy allows it. Save this as sns-policy.json. It limits publishing to this one rule through aws:SourceArn:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowEventBridgePublish",
"Effect": "Allow",
"Principal": { "Service": "events.amazonaws.com" },
"Action": "sns:Publish",
"Resource": "arn:aws:sns:us-east-1:111122223333:access-analyzer-alerts",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:events:us-east-1:111122223333:rule/access-analyzer-public-findings"
}
}
}
]
}Apply the policy and attach the topic as a target:
aws sns set-topic-attributes \
--topic-arn arn:aws:sns:us-east-1:111122223333:access-analyzer-alerts \
--attribute-name Policy \
--attribute-value file://sns-policy.json
aws events put-targets \
--rule access-analyzer-public-findings \
--targets 'Id=sns-alerts,Arn=arn:aws:sns:us-east-1:111122223333:access-analyzer-alerts'Without the topic policy, put-targets succeeds but deliveries fail silently from your point of view. The rule's FailedInvocations CloudWatch metric is where the failures show up.
Make the message readable
By default, SNS delivers the whole event as JSON. An input transformer pulls out a few fields and formats a sentence. Save this as targets.json:
[
{
"Id": "sns-alerts",
"Arn": "arn:aws:sns:us-east-1:111122223333:access-analyzer-alerts",
"InputTransformer": {
"InputPathsMap": {
"type": "$.detail.resourceType",
"resource": "$.detail.resource",
"account": "$.detail.accountId",
"id": "$.detail.id"
},
"InputTemplate": "\"Access Analyzer: public access finding on <type> <resource> in account <account>. Finding ID: <id>\""
}
}
]Then run aws events put-targets --rule access-analyzer-public-findings --targets file://targets.json. Using the same target Id replaces the earlier target definition.
Inspecting a real event
Patterns are exact, and a typo in a field name means the rule never matches and never errors. Before you rely on a rule, look at a real event:
- Create a temporary rule that matches only
{"source": ["aws.access-analyzer"]}. - Send it to a CloudWatch Logs log group. The log group's resource policy must allow EventBridge to write to it. The console sets this up for you.
- Trigger a finding, for example by applying the public bucket policy from the earlier chapter to a test bucket.
- Read the logged event and copy field names and values from it.
This also settles the detail-type question. Unused access findings are a different finding type from external access findings, and their events may carry a different detail-type than the one above. Match on what the logged event shows, not on this chapter.
You can also test a pattern without waiting for a real event:
aws events test-event-pattern \
--event-pattern file://pattern.json \
--event file://sample-event.jsonIt returns {"Result": true} or false.
Route to code for remediation
For actions beyond a notification, make a Lambda function the target. The function should do the smallest safe action, such as opening a ticket or tagging the resource with the finding ID. This handler reads the event and returns the fields a ticketing integration needs:
import json
def handler(event, context):
detail = event["detail"]
if detail.get("status") != "ACTIVE":
return {"skipped": detail.get("status")}
summary = {
"finding_id": detail["id"],
"resource": detail["resource"],
"resource_type": detail["resourceType"],
"account": detail["accountId"],
}
print(json.dumps(summary))
return summaryKeep these rules for automated responses:
- Make handlers idempotent. EventBridge delivers events at least once, so the same finding can arrive twice. Key your actions on the finding ID.
- Add a dead-letter queue to the target. After retries are exhausted, EventBridge puts the event in an SQS queue instead of dropping it.
- Be careful with automatic fixes. Removing a public policy statement automatically can break a legitimate partner integration. For unused access findings, notify and open a ticket instead of deleting roles, since a role that is unused for 90 days may still run a quarterly job.
Common errors
| Symptom | Likely cause |
|---|---|
| Rule exists, no notifications | Pattern field names or values do not match the real event. Test with `test-event-pattern`. |
| `FailedInvocations` metric rising | The target's resource policy does not allow `events.amazonaws.com`, or the SNS subscription is unconfirmed. |
| Duplicate notifications | The pattern has no `status` filter, so status changes also match. |
| No events for unused access | The pattern's `detail-type` is for external access findings only. Check the logged event. |
| Events missing in one Region | The rule was created in a different Region than the analyzer. |
Exercise
Your rule matches {"source": ["aws.access-analyzer"], "detail-type": ["Access Analyzer Finding"]}. The security team gets two emails for one public bucket: one when the finding appears, and one a day later after an engineer fixes the policy. What change to the pattern stops the second email, and why does it work?
Show answer
Add "detail": {"status": ["ACTIVE"]} to the pattern. Access Analyzer sends an event when a finding is created and another when its status changes. The fix changes the status to RESOLVED, so the second event has a status other than ACTIVE and no longer matches the rule.
Exercise
You created a rule and a target that publishes to an SNS topic. The rule shows no errors, but the CloudWatch metric FailedInvocations for the rule keeps rising. Name the first thing to check, and the condition key you would use to limit the permission to this rule.
Show answer
Check the SNS topic's resource policy. It must allow the service principal events.amazonaws.com to call sns:Publish on the topic. To limit it to this rule, add a condition on aws:SourceArn (with ArnEquals) set to the rule's ARN.