Sub topic 12
Routing findings with EventBridge
This chapter assumes you can read a GuardDuty finding, have a detector enabled, and know the basics of JSON, IAM roles, and the AWS CLI. You will learn how GuardDuty and Security Hub publish findings to Amazon EventBridge, how to write event patterns that select the findings you care about, and how to attach targets safely.
Why route findings at all
A finding that nobody sees does not protect anything. GuardDuty stores findings in its console and API, but it does not page anyone. EventBridge is the service that turns a finding into an action. It receives events from AWS services, compares each event with your rules, and delivers matching events to targets such as Lambda, SNS, SQS, Step Functions, or an event bus in another account.
The routing layer gives you three things:
- Selection: only the findings that matter reach a human or a script.
- Fan-out: one finding can notify a chat channel, open a ticket, and start remediation at the same time.
- Decoupling: detection (GuardDuty) and response (your code) change independently.
How findings reach EventBridge
GuardDuty sends events to the default event bus in the same account and Region as the detector. There are three event triggers:
- A new finding is created.
- An existing finding is updated, for example when a repeated activity increases its count.
- The publishing frequency interval elapses and updated findings are sent again.
New findings are always sent within about five minutes. The --finding-publishing-frequency setting only controls how often updates to existing findings are published. The default is six hours. The valid values are FIFTEEN_MINUTES, ONE_HOUR, and SIX_HOURS. The setup chapter created a detector with this command:
aws guardduty create-detector --enable --finding-publishing-frequency FIFTEEN_MINUTESIf you already have a detector, change the frequency with aws guardduty update-detector --detector-id "$DETECTOR_ID" --finding-publishing-frequency FIFTEEN_MINUTES.
Security Hub also publishes events. It sends findings to EventBridge with the source aws.securityhub, and its detail type is Security Hub Findings - Imported. Imported findings include those that GuardDuty forwarded. This gives you a choice of two places to attach rules, which the comparison table below covers.
The shape of a GuardDuty event
Every EventBridge event has the same envelope: version, id, detail-type, source, account, time, region, resources, and detail. For GuardDuty:
sourceisaws.guardduty.detail-typeisGuardDuty Finding.detailholds the finding itself:type,severity,title,resource,service, and so on.
Event patterns match against these fields, so the field names you learned when reading findings are the same names you use in rules.
Writing an event pattern
An event pattern is a JSON document. Each key is a field of the event, and each value is an array of acceptable values. The rules for combining them are simple:
- Values in one array are OR. The event matches if the field equals any of them.
- Different fields are AND. The event must satisfy every field you list.
- Fields you do not mention are ignored.
This pattern from the topic matches every finding with severity 7 or higher:
{
"source": ["aws.guardduty"],
"detail-type": ["GuardDuty Finding"],
"detail": {
"severity": [{ "numeric": [">=", 7] }]
}
}The numeric operator takes a comparison and a number. You can give a range with two comparisons, for example { "numeric": [">=", 4, "<", 7] } for medium findings.
Useful pattern operators
| Operator | Example | Matches |
|---|---|---|
| Exact value | `"type": ["Backdoor:EC2/C&CActivity.B"]` | One specific finding type |
| Prefix | `"type": [{ "prefix": "UnauthorizedAccess:" }]` | Every finding type in a threat purpose category |
| Numeric | `"severity": [{ "numeric": [">=", 7] }]` | Severity at or above a value |
| Anything-but | `"type": [{ "anything-but": ["Recon:EC2/Portscan"] }]` | Everything except the listed values |
| Exists | `"resource": { "instanceDetails": [{ "exists": true }] }` | Findings that involve an EC2 instance |
Finding types start with the threat purpose, such as Recon, UnauthorizedAccess, Backdoor, or CryptoCurrency. A prefix match on that first word lets you route a whole category to one team.
Testing a pattern before you trust it
Two checks catch most mistakes. First, the aws events test-event-pattern command compares a pattern with a sample event without creating anything. Second, create-sample-findings produces real events that flow through the real rule. Sample findings have a title that starts with "[SAMPLE]" and the sample flag in the service.additionalInfo object, so your handler can tell them apart from genuine threats.
DETECTOR_ID=$(aws guardduty list-detectors --query 'DetectorIds[0]' --output text)
aws guardduty create-sample-findings --detector-id "$DETECTOR_ID" --finding-types Recon:EC2/PortProbeUnprotectedPort UnauthorizedAccess:EC2/SSHBruteForceSample findings from the Recon:EC2/PortProbeUnprotectedPort type have a low severity, and UnauthorizedAccess:EC2/SSHBruteForce is also low. A rule that requires severity 7 will therefore not fire for these two. To exercise a high-severity rule, generate a type that GuardDuty rates high, such as Backdoor:EC2/C&CActivity.B!DNS or CryptoCurrency:EC2/BitcoinTool.B!DNS.
Creating the rule and a target
Creating a rule has three parts: the rule with its pattern, the target, and the permission that lets EventBridge invoke the target.
aws events put-rule \
--name guardduty-high-severity \
--event-pattern '{"source":["aws.guardduty"],"detail-type":["GuardDuty Finding"],"detail":{"severity":[{"numeric":[">=",7]}]}}' \
--state ENABLEDNext, attach a Lambda function as the target and grant EventBridge permission to call it. The function name gd-responder below is an example. Replace the account ID, Region, and function name with your own.
aws events put-targets \
--rule guardduty-high-severity \
--targets 'Id=gd-responder,Arn=arn:aws:lambda:us-east-1:111122223333:function:gd-responder'
aws lambda add-permission \
--function-name gd-responder \
--statement-id allow-eventbridge-guardduty \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn arn:aws:events:us-east-1:111122223333:rule/guardduty-high-severityThe --source-arn condition matters. Without it, any EventBridge rule in any account could invoke the function. For an SNS target, the equivalent step is a topic policy that allows events.amazonaws.com to publish.
The function itself can start as small as the one below. It reads the finding type, severity, and the affected instance ID, then returns them. A real handler would continue from here with an action such as isolating the instance.
def handler(event, context):
detail = event["detail"]
finding_type = detail["type"]
severity = detail["severity"]
instance_id = (
detail.get("resource", {})
.get("instanceDetails", {})
.get("instanceId")
)
print(f"{finding_type} severity={severity} instance={instance_id}")
return {"type": finding_type, "severity": severity, "instanceId": instance_id}Notice the use of .get() with defaults. Not every finding involves an EC2 instance. Findings about S3 buckets, IAM users, or EKS clusters have a different resource structure, so instanceDetails may be missing. A handler that indexes it directly raises KeyError on those events.
GuardDuty rule or Security Hub rule
| Question | Rule on GuardDuty events | Rule on Security Hub events |
|---|---|---|
| Event source | `aws.guardduty` | `aws.securityhub` |
| Finding format | GuardDuty's own JSON | AWS Security Finding Format (ASFF) |
| Scope | One account and Region | Can include other products and, with aggregation, other Regions |
| Latency | Lowest | Adds a short delay for import |
| Best for | Fast, GuardDuty-only response | One rule covering many products |
If you only react to GuardDuty, the direct rule is simpler and faster. If you want one workflow for findings from GuardDuty, Inspector, and Macie, attach the rule to Security Hub events and match on the ASFF fields instead. Do not attach both paths to the same responder without deduplication, or a single threat triggers it twice.
Failure modes and gotchas
- Rules are Regional. A rule in
us-east-1never sees a finding fromeu-west-1. Deploy the rule in every Region where GuardDuty runs, or forward events to a central bus. - Member accounts in an Organization send events to their own bus. The delegated administrator account sees member findings in GuardDuty, but the EventBridge events are generated in the account and Region where the finding is created, so check where your rule lives and what it can see.
- Delivery is at least once. EventBridge can deliver the same event twice, and updated findings reuse the same finding ID. Make handlers idempotent, for example by recording the finding ID and the update time.
- Failed deliveries are retried for up to 24 hours by default. Attach a dead-letter queue (SQS) to the target so you can see events that never reached it.
- Updates also match. A rule on severity fires again each time the finding is updated at the publishing frequency. If a notification should happen once, store the finding ID.
- Throttling and cost. EventBridge charges little for default-bus events from AWS services, but Lambda invocations, SNS messages, and cross-account delivery do cost money. A noisy rule at low severity can fan out into many invocations.
- Automatic action is risky. A handler that stops instances on every high finding can cause an outage if the finding is a false positive. Start with notification, then add narrowly scoped actions with approval or tagging checks.
Putting it together
A practical setup for one account looks like this:
- Keep the detector's publishing frequency at 15 minutes if you need updates to reach responders quickly.
- Create one rule for severity 7 or higher that notifies an SNS topic, so a person always hears about serious findings.
- Create separate, narrow rules for specific finding types that map to a known runbook.
- Add a dead-letter queue to each target and an alarm on its depth.
- Repeat in each Region, or forward to a central bus.
Exercise
A rule matches "severity": [{"numeric": [">=", 7]}] and has a Lambda target. You run create-sample-findings for Recon:EC2/PortProbeUnprotectedPort and the function never runs. Give the most likely reason and one way to confirm the rule works.
Show answer
That sample finding has low severity, so it does not satisfy the severity 7 condition and the rule correctly ignores it. To confirm the rule, generate a sample finding of a type rated high, such as Backdoor:EC2/C&CActivity.B!DNS, or run aws events test-event-pattern with a sample high-severity event.
Exercise
Your Lambda handler reads event["detail"]["resource"]["instanceDetails"]["instanceId"] and fails with KeyError for some findings. Why does it fail, and what is the fix?
Show answer
Findings that involve resources other than EC2, such as S3 buckets or IAM users, have no instanceDetails object. Use .get() with safe defaults, as in the example handler, and skip or branch for non-instance findings.
Exercise
A single GuardDuty finding triggers your responder twice. Name two causes described in this chapter.
Show answer
Rules are attached to both the GuardDuty event path and the Security Hub imported-findings path, so each finding arrives twice. Alternatively, the finding was updated at the publishing frequency and matched the rule again, or EventBridge delivered the event more than once. Make the handler idempotent by recording the finding ID.