我正在尝试从以下来源运行我的第一个lambda函数:https://github.com/coveooss/sqs-deadletterqueue-replayer-lambda/blob/master/sqs-deadletterqueue-replayer-lambda.py
from __future__ import print_function
import boto3
def transfer_messages(source_queue, target_queue):
total_messages_transferred = 0
while True:
messages = gather_messages(source_queue)
if not messages:
break
total_messages_transferred += len(messages)
send_messages(messages, target_queue)
delete_messages(messages)
print("In total " + str(total_messages_transferred) + " were transferred.")
def gather_messages(queue):
messages = queue.receive_messages(MaxNumberOfMessages=10, WaitTimeSeconds=20)
print("Collected: " + str(len(messages)) + " messages.")
return messages
def send_messages(messages, queue):
entries = [dict(Id=str(i + 1), MessageBody=message.body) for i, message in enumerate(messages)]
queue.send_messages(Entries=entries)
def delete_messages(messages):
for message in messages:
print("Copied " + str(message.body))
message.delete()
def handle_message(event, context):
sqs = boto3.resource(service_name='sqs')
source_queue_name = event['source_queue_name']
target_queue_name = event['target_queue_name']
print("From: " + source_queue_name + " To: " + target_queue_name)
if source_queue_name != target_queue_name + "-deadletter":
print("Exiting because the source_queue_name is not the same as the target_queue_name with '-deadletter' at the end.")
exit(1)
source_queue = sqs.get_queue_by_name(QueueName=source_queue_name)
target_queue = sqs.get_queue_by_name(QueueName=target_queue_name)
transfer_messages(source_queue, target_queue)
我在选择Python 2.7作为语言后创建了此代码,并使用以下测试数据运行它:
{
"target_queue_name": "test-queue",
"source_queue_name": "test-queue-deadletter"
}
我遇到以下错误:
Response:
{
"errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'"
}
Request ID:
"dfbe59ad-76d2-49c6-919a-1f9477f37d00"
Function Logs:
START RequestId: dfbe59ad-76d2-49c6-919a-1f9477f37d00 Version: $LATEST
Handler 'lambda_handler' missing on module 'lambda_function': 'module' object has no attribute 'lambda_handler'
END RequestId: dfbe59ad-76d2-49c6-919a-1f9477f37d00
REPORT RequestId: dfbe59ad-76d2-49c6-919a-1f9477f37d00 Duration: 0.68 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 53 MB
在我看到的所有其他答案中,都提到我需要编写一个lambda_handler
函数,但是handle_message
对我来说就是那个函数。为什么这不起作用?