| 1 |
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
.. _aws-boto3-sqs-dead-letter-queue: |
| 12 |
|
| 13 |
###################################### |
| 14 |
Using dead-letter queues in Amazon SQS |
| 15 |
###################################### |
| 16 |
|
| 17 |
This Python example shows you how to use a queue to receive and hold messages from other queues that |
| 18 |
the queues can't process. |
| 19 |
|
| 20 |
The scenario |
| 21 |
============ |
| 22 |
|
| 23 |
A dead letter queue is one that other (source) queues can target for messages that can't be processed |
| 24 |
successfully. You can set aside and isolate these messages in the dead letter queue to determine why |
| 25 |
their processing did not succeed. You must individually configure each source queue that sends messages |
| 26 |
to a dead letter queue. Multiple queues can target a single dead letter queue. |
| 27 |
|
| 28 |
In this example, Python code is used to route messages to a dead letter queue. The code uses the |
| 29 |
SDK for Python to use dead letter queues using this method of the AWS.SQS client class: |
| 30 |
|
| 31 |
* `set_queue_attributes <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.set_queue_attributes>`_. |
| 32 |
|
| 33 |
For more information about Amazon SQS dead letter queues, see |
| 34 |
`Using Amazon SQS Dead Letter Queues <http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html>`_ |
| 35 |
in the *Amazon Simple Queue Service Developer Guide*. |
| 36 |
|
| 37 |
Prerequisite tasks |
| 38 |
================== |
| 39 |
|
| 40 |
To set up and run this example, you must first complete these tasks: |
| 41 |
|
| 42 |
* Create an Amazon SQS queue to serve as a dead letter queue. For an example |
| 43 |
of creating an Amazon SQS queue, see :ref:`aws-boto3-sqs-create-queue`. |
| 44 |
|
| 45 |
Configure source queues |
| 46 |
======================= |
| 47 |
|
| 48 |
After you create a queue to act as a dead letter queue, you must configure the other queues that route |
| 49 |
unprocessed messages to the dead letter queue. To do this, specify a redrive policy that identifies |
| 50 |
the queue to use as a dead letter queue and the maximum number of receives by individual messages |
| 51 |
before they are routed to the dead letter queue. |
| 52 |
|
| 53 |
The example below shows how to: |
| 54 |
|
| 55 |
* Configure a source queue using |
| 56 |
`set_queue_attributes <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.set_queue_attributes>`_. |
| 57 |
|
| 58 |
|
| 59 |
Example |
| 60 |
------- |
| 61 |
|
| 62 |
.. code-block:: python |
| 63 |
|
| 64 |
import json |
| 65 |
|
| 66 |
import boto3 |
| 67 |
|
| 68 |
# Create SQS client |
| 69 |
sqs = boto3.client('sqs') |
| 70 |
|
| 71 |
queue_url = 'SOURCE_QUEUE_URL' |
| 72 |
dead_letter_queue_arn = 'DEAD_LETTER_QUEUE_ARN' |
| 73 |
|
| 74 |
redrive_policy = { |
| 75 |
'deadLetterTargetArn': dead_letter_queue_arn, |
| 76 |
'maxReceiveCount': '10' |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
# Configure queue to send messages to dead letter queue |
| 81 |
sqs.set_queue_attributes( |
| 82 |
QueueUrl=queue_url, |
| 83 |
Attributes={ |
| 84 |
'RedrivePolicy': json.dumps(redrive_policy) |
| 85 |
} |
| 86 |
) |
| 87 |
|