简体   繁体   English

使用Boto3创建SQS队列时指定死信队列

[英]Specify a dead letter queue when creating an SQS queue using Boto3

I'm trying to create a pair of SQS queues using boto3.我正在尝试使用 boto3 创建一对 SQS 队列。 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.我可以很好地创建具有其他属性的队列,但是指定死信 Queue ARN 所需的RedrivePolicy属性是一个嵌套属性,而所有其他属性都是简单的键值对。 The boto docs page isn't clear how to handle this nested attribute. boto 文档页面不清楚如何处理此嵌套属性。

I've tried with both a boto.resource and a boto.client, and several variations of JSON, strings, and Python dictionaries.我尝试过使用 boto.resource 和 boto.client,以及 JSON、字符串和 Python 词典的几种变体。 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.)我也看到了一堆相关的错误消息和问题,但还没有找到一个简单的解决方案(我认为之后设置属性是一种解决方法,但我想弄清楚如何在创建时设置这个 RedrivePolicy 而不是.)

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.我不确定它是在抱怨 ARN 中的冒号还是 JSON 策略中的引号。

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.正如 jarmod 在评论中暗示的那样,这是因为没有为死信队列指定 Fifo 属性。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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