The solution is scattered around.
You can read through SQS Dead Letter queue setup and reference to my example. you can do exactly the same things using AWS SQS console without coding.
import boto3
sqs = boto3.client("sqs")
# I want to "lock" my queue for 5 minutes to allow my process have time to
# complete the task and delete the message afterwards.
response = sqs.create_queue(
QueueName="foo",
Attributes= {
"VisibilityTimeout" : "300"
}
)
# create a queue to store the "dead letter message"
dlq_response = sqs.create_queue(
QueueName="dlq-foo",
Attributes= {
"VisibilityTimeout" : "300"
}
)
queue_url = response["QueueUrl"]
# Attach RedrivePolicy to drive message to dead letter queue
# I want to make sure the message only read 1 time. Assume the program crash
# if it is not deleted.
# deadLetterTargetArn : You must specify the queue exact region name,
# exact Account name(replace 1234567890) and your dead letter queue name dlq-foo
sqs.set_queue_attributes(
QueueUrl = queue_url,
Attributes = {
"RedrivePolicy" : """{
"maxReceiveCount" : "1" ,
"deadLetterTargetArn" : "arn:aws:sqs:<region-name>:1234567890:dlq-foo"
}"""
}
)
Note : The RedrivePolicy only access literal strings, NOT dictionary. However,as the doucumentation point out, you need to put "dictionary like" value in there and format it as string. You may use a str(dict()) to convert dict to string , I use python triple-quotes quotes for reading clarity .