【问题标题】:Error when sending message from lambda to DLQ将消息从 lambda 发送到 DLQ 时出错
【发布时间】:2021-01-27 05:51:11
【问题描述】:

我关注这篇文章是为了从 lambda 发送到 DLQ:

Using dead-letter queues in Amazon SQS — Boto3 documentation

代码如下

from datetime import datetime
import json
import os
import boto3
from botocore.vendored import requests

QUEUE_NAME = os.environ['QUEUE_NAME']
MAX_QUEUE_MESSAGES = os.environ['MAX_QUEUE_MESSAGES']
dead_letter_queue_arn = os.environ['DEAD_LETTER_QUEUE_ARN']
sqs = boto3.resource('sqs')
queue_url = os.environ['SQS_QUEUE_URL']

redrive_policy = {
    'deadLetterTargetArn': dead_letter_queue_arn,
    'maxReceiveCount': '10'
}


def lambda_handler(event, context):

    # Receive messages from SQS queue
    queue = sqs.get_queue_by_name(QueueName=QUEUE_NAME)
    response = requests.post("http://httpbin.org/status/500", timeout=10)
    if response.status_code == 500:
        sqs.set_queue_attributes(QueueUrl=queue_url,
                                 Attributes={
                                    'RedrivePolicy': json.dumps(redrive_policy)
                                    }
                                )

我这样做是因为我需要实现指数退避,但我什至无法发送到 DLQ,因为这个错误

[ERROR] AttributeError: 'sqs.ServiceResource' object has no attribute 'set_queue_attributes'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 24, in lambda_handler
    sqs.set_queue_attributes(QueueUrl=queue_url,

根据set_queue_attributes() 文档,该对象具有set_queue_attributes 属性。

【问题讨论】:

    标签: amazon-web-services aws-lambda boto3 amazon-sqs


    【解决方案1】:

    如果有人遇到同样的问题,客户端和资源之间存在差异,我想错误有必要的信息,但对于我来说,AWS 很难发现,据此

    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.set_queue_attributes

    response = client.set_queue_attributes(
        QueueUrl='string',
        Attributes={
            'string': 'string'
        }
    )
    

    你应该使用客户端

    import boto3
    
    client = boto3.client('sqs')
    

    我的错误是我已经从 boto 获得了一些与 sqs 相关的东西

    sqs = boto3.resource('sqs')
    

    这就是错误的原因 [错误] AttributeError: 'sqs.ServiceResource' 对象没有属性 'set_queue_attributes'

    因为我需要使用客户端而不是来自 sqs 的资源

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2020-12-03
      • 2021-05-06
      • 1970-01-01
      相关资源
      最近更新 更多