Files
floci/docs/services/eventbridge.md
T
Mario Toffia 0a54d0708e feat(eventbridge): execute target retry and dead-letter policies (#4736)
* feat(eventbridge): execute target retry and dead-letter policies

PutTargets now validates, stores and returns a target's RetryPolicy and
DeadLetterConfig. Failed deliveries from PutEvents and scheduled rules are
retried with backoff within the configured attempts and event age, and
exhausted or non-retryable events are sent to the standard SQS dead-letter
queue with the AWS failure attributes.

* fix(eventbridge): order target retries by due time and honour a lowered retry limit

Pending retries are kept in a queue ordered by their next attempt, so each wake-up
takes only what is due instead of scanning every pending retry. The clock is read
again for each retry, so a slow target cannot let a later retry run past its event
age, and a reset mid-tick stops the remaining retries. A retry now checks the
target's current MaximumRetryAttempts before invoking it, as it already did for
MaximumEventAgeInSeconds.

* fix(eventbridge): drop a target retry that outlives a reset

An attempt still running when the emulator resets can fail afterwards. Its retry
or dead-letter delivery is now dropped, so a rule and target recreated under the
same names after the reset do not receive a pre-reset event. A scheduled retry
wake-up keeps only its due time, not the event.

* fix(eventbridge): wake a target retry at its due time after a slow attempt

The retry delay was measured from when the failed attempt started but counted from
when it failed, so every retry after a slow attempt woke late by the attempt's
duration, and an expired event reached its dead-letter queue late.

* fix(eventbridge): drop a removed target's retries and drive retries from one tick

RemoveTargets now drops the removed targets' pending retries, so a target re-added
under the same Id does not receive an event from before its removal. Pending retries
are taken by one periodic tick, as in the scheduler's dispatcher, instead of a timer
per retry, so a reset leaves no timers behind.

* fix(eventbridge): warn about a blank dead-letter queue ARN

PutTargets accepts a whitespace-only DeadLetterConfig Arn, since AWS constrains only
its length. Delivery skipped it without a word; it now gets the same WARN as any other
dead-letter queue that is not a standard SQS queue in the rule's region.
2026-10-01 07:59:18 -04:00

162 lines
7.1 KiB
Markdown

# EventBridge
**Protocol:** JSON 1.1 (`X-Amz-Target: AmazonEventBridge.*`)
**Endpoint:** `POST http://localhost:4566/`
## Supported Actions
<!-- floci:actions:start -->
| Action | Description |
| --- | --- |
| `CreateEventBus` | Create a custom event bus |
| `DeleteEventBus` | Delete an event bus |
| `DescribeEventBus` | Get event bus details |
| `UpdateEventBus` | Update event bus description, KMS key, dead-letter config, or log config |
| `ListEventBuses` | List all event buses |
| `PutRule` | Create or update a rule with a schedule or event pattern |
| `DeleteRule` | Delete a rule |
| `DescribeRule` | Get rule details |
| `ListRules` | List rules |
| `EnableRule` | Enable a disabled rule |
| `DisableRule` | Disable a rule |
| `PutTargets` | Add targets to a rule |
| `RemoveTargets` | Remove targets from a rule |
| `ListTargetsByRule` | List targets for a rule |
| `PutEvents` | Publish custom events to an event bus |
| `TestEventPattern` | Test whether a sample event matches a given pattern (no targets fired) |
| `ListTagsForResource` | - |
| `TagResource` | - |
| `UntagResource` | - |
| `PutPermission` | - |
| `RemovePermission` | - |
| `CreateArchive` | - |
| `DescribeArchive` | - |
| `UpdateArchive` | - |
| `DeleteArchive` | - |
| `ListArchives` | - |
| `CreateConnection` | Create a connection for API destinations (credential values are stored but never returned) |
| `DescribeConnection` | Get connection details with credential values stripped |
| `UpdateConnection` | Update connection description, auth type, or auth parameters |
| `DeleteConnection` | Delete a connection |
| `ListConnections` | List connections, optionally filtered by name prefix or state |
| `StartReplay` | - |
| `DescribeReplay` | - |
| `CancelReplay` | - |
| `ListReplays` | - |
<!-- floci:actions:end -->
## Configuration
| Variable | Default | Description |
|---|---|---|
| `FLOCI_SERVICES_EVENTBRIDGE_ENABLED` | `true` | Enable or disable the service |
## Examples
```bash
export AWS_ENDPOINT_URL=http://localhost:4566
# Create a custom event bus
aws events create-event-bus \
--name my-bus \
--endpoint-url $AWS_ENDPOINT_URL
# Create a rule matching a pattern
aws events put-rule \
--name order-placed-rule \
--event-bus-name my-bus \
--event-pattern '{"source":["com.myapp"],"detail-type":["OrderPlaced"]}' \
--state ENABLED \
--endpoint-url $AWS_ENDPOINT_URL
# Add a Lambda target
aws events put-targets \
--rule order-placed-rule \
--event-bus-name my-bus \
--targets '[{
"Id": "process-order",
"Arn": "arn:aws:lambda:us-east-1:000000000000:function:process-order"
}]' \
--endpoint-url $AWS_ENDPOINT_URL
# Publish an event
aws events put-events \
--entries '[{
"Source": "com.myapp",
"DetailType": "OrderPlaced",
"Detail": "{\"orderId\":\"123\",\"amount\":99.99}",
"EventBusName": "my-bus"
}]' \
--endpoint-url $AWS_ENDPOINT_URL
```
## Default Event Bus
EventBridge includes a default event bus (`default`) that accepts events from AWS services. Custom buses are for your own application events.
```bash
# List rules on the default bus
aws events list-rules --endpoint-url $AWS_ENDPOINT_URL
# Send to default bus
aws events put-events \
--entries '[{"Source":"myapp","DetailType":"test","Detail":"{}"}]' \
--endpoint-url $AWS_ENDPOINT_URL
```
## Event Bus Targets
A rule can target another event bus by ARN: the event is republished there, and that bus's own rules evaluate it and fan out normally. `Source`, `DetailType`, `Resources` and the originating `account`/`region` carry over, and each hop gets a new event id.
```bash
aws events put-targets \
--rule order-placed-rule \
--event-bus-name my-bus \
--targets '[{
"Id": "forward-to-domain-bus",
"Arn": "arn:aws:events:us-east-1:000000000000:event-bus/domain-bus"
}]' \
--endpoint-url $AWS_ENDPOINT_URL
```
## Step Functions Targets
A matching rule can start an unqualified Step Functions state machine ARN. The target supports the
full event as the default input, plus `Input`, `InputPath`, and `InputTransformer`. Floci resolves
the state machine from the account and region in its ARN.
State machine aliases and versions are not supported as EventBridge targets.
## Target Retry and Dead-Letter Queues
A target's `RetryPolicy` and `DeadLetterConfig` are validated by `PutTargets`, stored with the target, returned by `ListTargetsByRule`, and applied to delivery from `PutEvents` and scheduled rules.
```bash
aws events put-targets \
--rule order-placed-rule \
--event-bus-name my-bus \
--targets '[{
"Id": "orders-queue",
"Arn": "arn:aws:sqs:us-east-1:000000000000:orders",
"RetryPolicy": {"MaximumRetryAttempts": 4, "MaximumEventAgeInSeconds": 3600},
"DeadLetterConfig": {"Arn": "arn:aws:sqs:us-east-1:000000000000:orders-dlq"}
}]' \
--endpoint-url $AWS_ENDPOINT_URL
```
- The first attempt is immediate. Throttling, server-side errors from the target, and internal errors are retried with exponential backoff (1 second doubling, capped at 5 minutes) until `MaximumRetryAttempts` or `MaximumEventAgeInSeconds` runs out. The AWS defaults, 185 attempts and 86400 seconds, apply when a field is omitted.
- A missing target resource, a permission error, or any other client error is not retried.
- An exhausted or non-retryable event is sent to the `DeadLetterConfig` queue with the original event as the body and the `RULE_ARN`, `TARGET_ARN`, `ERROR_CODE`, `ERROR_MESSAGE`, `EXHAUSTED_RETRY_CONDITION` and `RETRY_ATTEMPTS` message attributes. `EXHAUSTED_RETRY_CONDITION` is omitted for a non-retryable error, since no retry limit was reached. The `ERROR_CODE` value is Floci's best mapping of the underlying failure.
- The dead-letter queue must be a standard SQS queue in the rule's region, as in AWS. It may be in another account. Any other queue, and any failure to send to it, is logged as a `WARN`.
- A retry uses the target's current definition, so `PutTargets` changes apply to pending retries, and a target removed with `RemoveTargets` stops being retried.
- An accepted asynchronous Lambda invocation counts as delivered. A later function error is handled by Lambda's own retry and destination settings, not by the rule.
- Pending retries are kept in memory and are dropped by an emulator reset or restart.
## Current Behavior
- `PutEvents` reports success once the source bus accepts an event. Target delivery failures are retried and dead-lettered as described above, and otherwise surface only as a `WARN` in the Floci logs.
- A `Detail` forwarded to an event bus must be a JSON object, as in AWS; anything else is dropped, including an `InputPath` selecting a scalar such as `$.detail.orderId` or an envelope carrying `"detail": null`.
- A bus ARN naming another account is forwarded under that account, so the target bus and its rules resolve there.
- Onward delivery from that bus follows each target type: SQS and Step Functions resolve cross-account, while Lambda, SNS, Batch and Firehose resolve in the caller's account.
- An event is forwarded between buses only once, matching AWS: a bus that received an event from another bus does not forward it on to a third. The second hop is dropped with only a `WARN` rather than reported to the caller.