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.
Why delivery can fail
EventBridge delivers each matched event to each target on its own. A delivery can fail for several reasons:
- The target service is throttled or briefly unavailable.
- The target returns an error, such as a Lambda function that is throttled.
- The rule's role or the target's resource policy does not allow delivery.
- The target no longer exists, for example a deleted queue.
EventBridge treats some of these errors as temporary and retries them. It does not retry others, such as a missing permission or a deleted target. Those fail at once and go to the dead-letter queue if you configured one.
The retry policy
Each target has a retry policy with two settings:
| Setting | Meaning | Default | Limit |
|---|---|---|---|
| MaximumRetryAttempts | How many times EventBridge retries after the first attempt | 185 | 0 to 185 |
| MaximumEventAgeInSeconds | How long EventBridge keeps trying | 86400 (24 hours) | 60 to 86400 |
Retries use exponential backoff with jitter, so the gaps between attempts grow. Whichever limit is reached first ends the retrying. If you set 185 attempts but 3600 seconds, the event is dropped after one hour even if attempts remain.
The dead-letter queue
A dead-letter queue (DLQ) is an SQS standard queue that receives events EventBridge could not deliver. Without one, an event that exhausts its retries is discarded. The only trace is the FailedInvocations metric.
A DLQ message holds the original event. Message attributes describe the failure, including an error code and error message, and the rule and target involved. Use these to find the cause.
The target definition below sets both the retry policy and the DLQ.
[
{
"Id": "order-alerts-queue",
"Arn": "arn:aws:sqs:us-east-1:123456789012:order-alerts",
"RetryPolicy": {
"MaximumRetryAttempts": 5,
"MaximumEventAgeInSeconds": 3600
},
"DeadLetterConfig": {
"Arn": "arn:aws:sqs:us-east-1:123456789012:order-alerts-dlq"
}
}
]Save it as targets.json and attach it:
aws events put-targets --rule high-value-orders --event-bus-name orders-bus --targets file://targets.jsonAllow EventBridge to write to the DLQ
The DLQ needs a resource policy that lets EventBridge send messages to it. Without this policy, the failed events cannot be stored, and nothing is kept. The policy limits access to your rule with aws:SourceArn.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowEventBridgeToSendToDlq",
"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"
}
}
}
]
}Note that the rule ARN for a custom bus includes the bus name.
Common errors
- The DLQ stays empty while events go missing. The queue policy is probably missing or its
aws:SourceArndoes not match the rule ARN. - Events disappear after one hour.
MaximumEventAgeInSecondsis lower than you expected. Check it, not only the attempt count. - The DLQ is a FIFO queue. EventBridge supports only standard queues as DLQs.
- A DLQ in another account or Region. Keep the DLQ in the same account and Region as the rule.
- Nobody reads the DLQ. Alarm on
ApproximateNumberOfMessagesVisiblefor the queue. After you fix the cause, redrive the messages or resend them with PutEvents.
Exercise
A target has MaximumRetryAttempts set to 100 and MaximumEventAgeInSeconds set to 120. A target outage lasts 30 minutes. What happens to an event matched at the start of the outage, and what must be true for you to recover it?
Show answer
EventBridge stops retrying after 120 seconds, because the event age limit is reached before the attempt limit. The event is then sent to the target's dead-letter queue. To recover it, the target must have a DeadLetterConfig pointing to an SQS standard queue whose resource policy allows events.amazonaws.com to call sqs:SendMessage. After the outage, you resend the events from that queue.