Sub topic 5
Rules: connecting a bus to targets
This chapter assumes you can use the AWS CLI, read JSON, and understand IAM roles. It also assumes you have used Lambda, SQS, or SNS. You should know what an event bus is and what an event looks like.
What a rule does
A rule is a resource attached to one event bus. It holds an event pattern, or a schedule, and a list of targets. EventBridge compares every event that arrives on the bus with the pattern of each rule on that bus. When an event matches, EventBridge sends a copy to every target of that rule.
Three properties shape how you design rules:
- A rule belongs to exactly one bus. It never sees events from other buses.
- One event can match many rules. Each matching rule delivers independently.
- A rule can have up to five targets. They all receive the same matched event.
Create a rule
Save the event pattern from the previous chapter as pattern.json. Then create the rule on the custom bus:
aws events put-rule --name high-value-orders --event-bus-name orders-bus --event-pattern file://pattern.jsonThe command returns the rule's ARN. A new rule is enabled by default, so it starts matching events at once. If you leave out --event-bus-name, the rule is created on the default bus. This is a common mistake. The rule then never fires for events you publish to your custom bus.
You can pass --state DISABLED to create the rule switched off. Later you can use aws events disable-rule and aws events enable-rule with the same --name and --event-bus-name arguments. Disabling a rule stops matching but keeps its configuration.
Attach targets
A target is the resource that receives the event. Targets are added with put-targets, which takes the rule name, the bus name, and a list of target definitions:
aws events put-targets --rule high-value-orders --event-bus-name orders-bus --targets file://targets.jsonEach target needs two fields:
Idis a name that is unique within the rule. You use it to update or remove the target later.Arnis the ARN of the destination, such as a Lambda function, SQS queue, or SNS topic.
A minimal targets.json looks like this:
[
{
"Id": "order-alerts-queue",
"Arn": "arn:aws:sqs:us-east-1:123456789012:order-alerts"
}
]put-targets can succeed while individual targets fail, just as PutEvents can. The response contains FailedEntryCount and a FailedEntries list. Always check them.
Common errors
| Symptom | Likely cause |
|---|---|
| Rule never fires | Rule was created on the default bus, or the pattern does not match the event |
| Rule matches but the target gets nothing | The target's resource policy or the rule's role does not allow delivery |
| `put-targets` reports a failed entry | Invalid ARN, or the role in `RoleArn` cannot be used |
| Update has no effect | You reused a `put-rule` name on a different bus |
The permissions chapter covers the delivery errors in detail.
Exercise
You run aws events put-rule --name refunds --event-pattern file://refund-pattern.json and then publish a matching event to orders-bus. The rule never fires. What is the most likely cause, and how do you fix it?
Show answer
The command had no --event-bus-name, so the rule was created on the default bus, and it never sees events sent to orders-bus. Re-run put-rule with --event-bus-name orders-bus, then attach targets to that rule with put-targets --event-bus-name orders-bus.