简体   繁体   中英

Specify a dead letter queue when creating an SQS queue using Boto3

I'm trying to create a pair of SQS queues using boto3. The first one is a dead letter queue for the second one.

def create_queues(qname):

    # create a fifo queue with a deadletter queue
    dl_queue = sqs.create_queue(
        QueueName=f'{qname}-dead-letter.fifo',
        Attributes={'FifoQueue': "true"}
    )
    dl_arn = dl_queue.attributes['QueueArn']
    print(dl_arn)


    policy = {
      "maxReceiveCount" : '3',
      "deadLetterTargetArn": dl_arn
    }
    policy = json.dumps(policy)

    task_queue = sqs.create_queue(
        QueueName=f'{qname}.fifo',
        Attributes={'RedrivePolicy': policy}
    )

create_queues('test')

I can create queues with other attributes just fine, but the RedrivePolicy attribute needed to specify the dead letter Queue ARN is a nested attribute, while all the others are simple key value pairs. The boto docs page isn't clear how to handle this nested attribute.

I've tried with both a boto.resource and a boto.client, and several variations of JSON, strings, and Python dictionaries. I've also seen a bunch of related error messages and questions, but haven't found a simple solution (I think setting the attribute afterwards is a workaround, but I'd like to figure out how to set this RedrivePolicy at creation time rather.)

The error message I get for the code above is as follows. I'm not sure if it is complaining about the colons in the ARN or about the quotation marks in the JSON policy.

Traceback (most recent call last):
  File "sqshelper.py", line 57, in <module>
    create_test_queue()
  File "sqshelper.py", line 29, in create_test_queue
    queue_url = create_sqs('testbot.fifo', redrive_policy=redrive_policy)
  File "sqshelper.py", line 16, in create_sqs
    response = sqs_client.create_queue(
  File "/Users/g/git/sqstasks/venv/lib/python3.8/site-packages/botocore/client.py", line 395, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/g/git/sqstasks/venv/lib/python3.8/site-packages/botocore/client.py", line 725, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the CreateQueue operation: Can only include alphanumeric characters, hyphens, or underscores. 1 to 80 in length

As jarmod hinted at in the comment, this was from not specifying the Fifo attribute for the dead letter queue.

The following code works as expected.

import boto3
import json

from dotenv import load_dotenv

load_dotenv()

sqs = boto3.resource("sqs", region_name="eu-west-2")


def create_queues(qname):

    # create a fifo queue with a deadletter queue
    dl_queue = sqs.create_queue(
        QueueName=f"{qname}-dead-letter.fifo", Attributes={"FifoQueue": "true"}
    )
    dl_arn = dl_queue.attributes["QueueArn"]

    print("successfully created dead letter queue")
    print(dl_arn)

    policy = {"maxReceiveCount": "3", "deadLetterTargetArn": dl_arn}
    policy = json.dumps(policy)

    task_queue = sqs.create_queue(
        QueueName=f"{qname}.fifo",
        Attributes={"RedrivePolicy": policy, "FifoQueue": "true"},
    )


create_queues("testqueue")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM