Sub topic 2
Rules: Connecting an EventBridge Bus to Targets
Assume you know JSON, the AWS CLI, and basic IAM. A rule connects an event bus to one or more targets. It contains an event pattern that selects matching events. EventBridge sends each match to the configured targets.
Writing Event Patterns
An event pattern is a JSON document. It tests fields in the event envelope, such as source and detail-type, and fields inside detail.
This pattern accepts OrderPlaced events from com.example.orders when the amount is at least 100 and the status is not test:
{
"source": ["com.example.orders"],
"detail-type": ["OrderPlaced"],
"detail": {
"amount": [{ "numeric": [">=", 100] }],
"status": [{ "anything-but": ["test"] }]
}
}A rule evaluates the pattern against each event on its bus. Values in arrays are alternatives. Conditions in different fields must all match. EventBridge patterns are exact about field names and value types, so 100 and "100" are different values.
Choosing and Configuring Targets
A target can be an AWS service such as Lambda, SQS, SNS, Step Functions, or Kinesis. A rule can have multiple targets. Each target needs a unique identifier and an ARN.
aws events put-rule \
--name high-value-orders \
--event-bus-name orders-bus \
--event-pattern file://pattern.json
aws events put-targets \
--rule high-value-orders \
--event-bus-name orders-bus \
--targets file://targets.jsonThe rule and target must use the same event bus. The target service also needs permission for EventBridge to invoke or send to it.
Reshaping Events with Input Transformers
An input transformer selects values from the original event and builds a smaller payload. This keeps targets independent of the full EventBridge envelope.
For an SQS target, the configuration can map $.detail.orderId and $.detail.amount into a new JSON document. Configure retry limits and a dead-letter queue for delivery failures:
[
{
"Id": "order-alerts-queue",
"Arn": "arn:aws:sqs:us-east-1:123456789012:order-alerts",
"InputTransformer": {
"InputPathsMap": {
"orderId": "$.detail.orderId",
"amount": "$.detail.amount"
},
"InputTemplate": "{\"orderId\": <orderId>, \"amount\": <amount>}"
},
"RetryPolicy": {
"MaximumRetryAttempts": 5,
"MaximumEventAgeInSeconds": 3600
},
"DeadLetterConfig": {
"Arn": "arn:aws:sqs:us-east-1:123456789012:order-alerts-dlq"
}
}
]Exercise
Which part of a rule decides whether an event is delivered to a target?
Show answer
The event pattern. It must match the event's envelope fields and any selected fields inside detail.