在Lambda中重新处理DLQ事件 [英] Re-process DLQ events in Lambda

查看:108
本文介绍了在Lambda中重新处理DLQ事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置了SQS DeadLetterQueue的AWS Lambda函数'A'.当Lambda无法处理事件时,会将其正确发送到DLQ.有没有办法重新处理以DLQ结尾的事件?

I have an AWS Lambda Function 'A' with a SQS DeadLetterQueue configured. When the Lambda fails to process an event, this is correctly sent to the DLQ. Is there a way to re-process events that ended into a DLQ?

我找到了两种解决方案,但是它们都有缺点:

I found two solution, but they both have drawbacks:

  1. 创建一个新的Lambda函数'B',该函数从SQS读取,然后将事件一个接一个地发送到先前的Lambda'A'. ->在这里,我必须编写新代码并部署新功能
  2. 当事件到达SQS时再次触发Lambda'A'->这看起来很危险,因为我可能会执行循环执行

我的理想解决方案应该是按需使用Lambda'A'重新处理丢弃的事件,而不是从头开始创建新的Lambda'B'.有没有办法做到这一点?

My ideal solution should be re-processing on demand the discarded events with Lambda 'A', without creating a new Lambda 'B' from scratch. Is there a way to accomplish this?

推荐答案

最后,我没有从AWS找到任何解决方案来重新处理Lambda函数的DLQ事件.然后,我创建了自己的自定义Lambda函数(希望这对遇到相同问题的其他开发人员有所帮助):

Finally, I didn't find any solution from AWS to reprocess the DLQ events of a Lambda Function. Then I created my own custom Lambda Function (I hope that this will be helpful to other developers with same issue):

import boto3

lamb = boto3.client('lambda')
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName='my_dlq_name')


def lambda_handler(event, context):
    for _ in range(100):
        messages_to_delete = []
        for message in queue.receive_messages(MaxNumberOfMessages=10):
            payload_bytes_array = bytes(message.body, encoding='utf8')
            # print(payload_bytes_array)
            lamb.invoke(
                FunctionName='my_lambda_name',
                InvocationType="Event",  # Event = Invoke the function asynchronously.
                Payload=payload_bytes_array
            )

            # Add message to delete
            messages_to_delete.append({
                'Id': message.message_id,
                'ReceiptHandle': message.receipt_handle
            })

        # If you don't receive any notifications the messages_to_delete list will be empty
        if len(messages_to_delete) == 0:
            break
        # Delete messages to remove them from SQS queue handle any errors
        else:
            deleted = queue.delete_messages(Entries=messages_to_delete)
            print(deleted)

部分代码受这篇文章

这篇关于在Lambda中重新处理DLQ事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆