使用死信队列处理 AWS Lambda 错误

Using a Dead Letter Queue for AWS Lambda Errors

我已尝试按照有关在 AWS 上为我的 Lambda 设置 SQS 死信队列的文档进行操作,但我似乎无法将错误传递给它。

我在 eu-west-2

上有一个 Lambda
exports.handler = async (event) => {
    if (event.desire === 'error') {
        throw new Error('dutifully throw an error');
    }
    
    const response = {
        statusCode: 200,
        body: JSON.stringify('Complete the lambda!'),
    };
    return response;
};

将异步调用设置为我的 sqs 队列 test-dlq

我已将以下内容添加到角色中:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": "sqs:*",
            "Resource": "arn:aws:sqs:eu-west-2:123456789:test-dlq"
        }
    ]
}

(我之前将此设置为 "Action": "sqs:SendMessage",但为简洁起见,已将其设置得更加宽松)

我已经设置了我的 SQS 队列来接收消息:

我的测试用例负载是:

{
    "desire": "error"
}

这会导致 lambda 出错并将错误添加到 CloudWatch 日志但不会传递到 DLQ。

我需要做些什么来将错误路由到队列吗?还是我配置错误了?

您的 DLQ 不起作用的原因是因为使用 Test 按钮或调用函数使用:

aws lambda invoke --function-name testFunction --payload '{"desire":"error"}' /tmp/out.json

导致 同步 调用,而不是异步调用。

要异步调用您的函数,您必须在 CLI 中使用 Event type

aws lambda invoke --function-name testFunction --invocation-type Event --payload '{"desire":"error"}' /tmp/out.json