Sub topic 10
Retries and dead-letter queues
This chapter assumes you can create rules and targets with the AWS CLI, read IAM policies, and use SQS. It covers what EventBridge does when it cannot deliver an event to a target, and how to keep those events instead of losing them.
What "delivery" means in EventBridge
When an event matches a rule, EventBridge sends it to each target of that rule. Each target is delivered independently. If one target fails, the other targets of the same rule still receive the event.
EventBridge delivers events at least once. A target can occasionally receive the same event twice, so consumers should be idempotent. An idempotent consumer can process the same event more than once and still produce the same result. A common approach is to record the event id and skip IDs you have already handled.
Delivery can fail for two broad reasons:
- Retryable errors. The target is throttled, unavailable, or returns a 5xx-style error. These are often temporary.
- Non-retryable errors. The target no longer exists, or the role or resource policy denies access. Retrying will not help.
The default retry behavior
If you set nothing, EventBridge retries a failed delivery for up to 24 hours and up to 185 times, using exponential backoff with jitter. Backoff means the wait between attempts grows. Jitter adds randomness so many failed deliveries do not all retry at the same moment.
After the retry window ends, EventBridge drops the event. Without a dead-letter queue, you will not know which events were lost, other than from metrics.
Configuring a retry policy
A retry policy is set per target. It has two settings:
| Setting | Meaning | Range |
|---|---|---|
| `MaximumRetryAttempts` | How many times EventBridge retries after the first attempt fails | 0 to 185 |
| `MaximumEventAgeInSeconds` | How long EventBridge keeps trying | 60 to 86400 (24 hours) |
Delivery stops when either limit is reached first. Setting MaximumRetryAttempts to 0 disables retries.
Shorter limits suit events that lose value quickly, such as a "price changed" notification. Longer limits suit events that must arrive, such as an order, as long as the target can handle a late arrival.
The target definition below uses a retry policy and a dead-letter queue, together with an input transformer from the previous chapter:
[
{
"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"
}
}
]EventBridge makes at most 5 retries and stops trying after one hour, whichever comes first. It then sends the event to the queue order-alerts-dlq.
Dead-letter queues
A dead-letter queue (DLQ) is an SQS queue that receives events EventBridge could not deliver. You attach it to a target with DeadLetterConfig. It is a per-target setting, so each target needs its own configuration, though several targets can share one queue.
Key points:
- The DLQ must be a standard SQS queue. FIFO queues are not supported as EventBridge DLQs.
- The DLQ must be in the same account and Region as the rule.
- EventBridge needs permission to send messages to it, through the queue's resource policy.
- Delivery to the DLQ is itself best effort. If it fails, for example because the policy is missing, the event is lost.
Granting EventBridge access to the DLQ
Add a policy to the queue that allows the EventBridge service to call sqs:SendMessage, limited to your rule with aws:SourceArn:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowEventBridgeDlq",
"Effect": "Allow",
"Principal": { "Service": "events.amazonaws.com" },
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-east-1:123456789012:order-alerts-dlq",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:events:us-east-1:123456789012:rule/orders-bus/high-value-orders"
}
}
}
]
}Without the condition, any EventBridge rule in any account could write to the queue. Note that a rule on a custom bus has the bus name in its ARN, as shown.
If the DLQ is encrypted with a customer managed KMS key, the key policy must also allow events.amazonaws.com to use the key. A queue encrypted with the AWS managed key for SQS cannot receive these messages, because its key policy cannot be changed.
What a dead-lettered message contains
The message body is the original event. The useful diagnostic information arrives as SQS message attributes:
| Attribute | Content |
|---|---|
| `ERROR_CODE` | A short code, such as `RESOURCE_NOT_FOUND` or `NO_PERMISSIONS` |
| `ERROR_MESSAGE` | A readable explanation of the failure |
| `RULE_ARN` | The rule that matched the event |
| `TARGET_ARN` | The target that failed |
| `RETRY_ATTEMPTS` | How many retries were made |
| `EXHAUSTED_RETRY_CONDITION` | Which limit ended delivery: attempts or event age |
Because the body is the original event and not the transformed payload, you can fix the target and resend the same body.
The delivery path
Configuring this with the CLI
Create the rule and then attach the targets file shown earlier:
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.jsonEven when put-targets returns, check FailedEntryCount in the response. A nonzero value means some targets were not saved, for example because the DLQ ARN is invalid.
Target-side behavior that affects retries
Some targets have their own retry behavior, and it interacts with the EventBridge policy:
- Lambda, asynchronous invocation. EventBridge invokes Lambda asynchronously. EventBridge considers delivery successful once Lambda accepts the event. Errors inside your function are then handled by Lambda's own retry settings and its own on-failure destination or DLQ, not by the EventBridge retry policy. Configure both layers.
- SQS and SNS. These accept the event and acknowledge it quickly. Failures are usually permissions, a deleted resource, or encryption key problems.
- API destinations. Throttling or 429 and 5xx responses from the HTTP endpoint are retried. Most 4xx responses are not retried, because they signal a bad request.
This split matters when debugging. A missing event in a Lambda function may never appear in the EventBridge DLQ, because EventBridge already delivered it.
Monitoring failed deliveries
EventBridge publishes rule-level metrics to CloudWatch in the AWS/Events namespace. For failed delivery, watch these:
FailedInvocations: attempts that failed after all retries. A nonzero value means events were not delivered.InvocationsFailedToBeSentToDlq: events that could not be written to the DLQ. This signals a permissions or encryption problem with the DLQ.InvocationsSentToDlq: events that were written to the DLQ.ThrottledRules: rules that were throttled, which delays matching.
Create a CloudWatch alarm on InvocationsSentToDlq greater than 0, and another on InvocationsFailedToBeSentToDlq greater than 0. The second one tells you when your safety net has a hole. You can also alarm on ApproximateNumberOfMessagesVisible for the DLQ itself.
Handling dead-lettered events
Treat the DLQ as a work list, not a storage place. A workable routine:
- An alarm notifies you when the queue has messages.
- Read
ERROR_CODEandERROR_MESSAGEon a sample message to find the cause. - Fix the cause: restore the target, correct the role, or raise a quota.
- Resend the message bodies to the bus with
PutEvents, or send them straight to the target. - Delete the messages once they are processed.
Set the DLQ's message retention period to a longer value than the default of 4 days if your team may not react quickly. The maximum is 14 days.
Resending through the bus will trigger every matching rule, including targets that already received the event. Because of at-least-once delivery you should have idempotent consumers anyway, but be careful with targets that have side effects such as sending email.
Common errors
- Events missing and no DLQ messages. The DLQ policy is missing or the
aws:SourceArndoes not match the rule ARN (a frequent mistake is leaving out the bus name). CheckInvocationsFailedToBeSentToDlq. put-targetsrejects the DLQ. The queue is FIFO, in another Region, or in another account.- Retries continue longer than expected. The default is 24 hours. Set
MaximumEventAgeInSecondsexplicitly. - Retries on a permanent failure. Only some errors are retried. Permission errors usually go to the DLQ quickly with
NO_PERMISSIONS. - A DLQ that nobody reads. Without an alarm, failures accumulate until retention deletes them.
Choosing settings
| Situation | Suggested retry policy | DLQ |
|---|---|---|
| Order or payment events | Defaults or long age, many attempts | Yes, long retention, alarm |
| Time-sensitive notifications | Age of a few minutes, few attempts | Optional |
| Idempotent, low-value telemetry | Default | Optional |
| Targets with side effects | Moderate limits | Yes, resend manually after review |
Exercise
A rule sends events to a Lambda function. The function throws an error on every invocation, but the EventBridge DLQ for that target stays empty and FailedInvocations is 0. Why, and where should you configure failure handling?
Show answer
EventBridge invokes Lambda asynchronously and counts delivery as successful once Lambda accepts the event. The function's errors happen after that point, so the EventBridge retry policy and DLQ are not involved. Configure Lambda's asynchronous invocation settings: its retry attempts and an on-failure destination or DLQ on the function.
Exercise
You set a DLQ on a target, but after the target is deleted, no messages appear in the queue and InvocationsFailedToBeSentToDlq is above 0. List two likely causes and the fix for each.
Show answer
First, the queue's resource policy may not allow events.amazonaws.com to call sqs:SendMessage, or its aws:SourceArn condition may not match the rule ARN (for a custom bus the ARN includes the bus name, as in rule/orders-bus/high-value-orders). Fix the policy. Second, the queue may be encrypted with a key that EventBridge cannot use, such as the AWS managed SQS key. Switch to a customer managed KMS key whose key policy allows events.amazonaws.com, or remove encryption.