AWS SQS 不能可靠地触发 Lambda
AWS SQS doesn't reliably trigger Lambda
我已经使用 Lambda 和 SQS 设置了一个小型无服务器应用程序。
在我的例子中,我想 触发 一个 lambda 每次 消息 添加 到 SQS 队列。
我的 serverless.yml
中的函数
functions:
collectGame:
handler: js/collect.collectGame
memorySize: 128
timeout: 10
events:
- sqs:
arn:
Fn::GetAtt:
- gameRequestQueue
- Arn
- http:
method: post
cors:
origin: "https://my-api-url.com"
path: get/game/{id}
private: true
request:
parameters:
paths:
id:true
我通过一次向队列发送 31 条消息来测试该过程,但意识到只有 9 个 Lambda 被执行(通过查看 cloudwatch 日志)。我查看了队列,可以确认它已填满所有消息,并且在触发 9 个 Lambda 后它是空的。
我预计会有 31 次 Lambda 执行,但事实并非如此。有人知道我的 Lambda 没有被消息触发的潜在原因吗?
您的 lambda 函数可能被多条消息调用。如果您只希望每次 lambda 调用发送一条消息,则在创建事件源映射时应该能够将 BatchSize 设置为 1
看起来您正在使用无服务器框架。请参阅他们的 SQS 事件 documentation 以设置批量大小。
对于任何使用 aws sam 的人来说,这里是提到批量大小的 link:here,查找副标题 'Configuring a Queue as an Event Source'。这是我在 yaml 中与 DLQ 一起设置它的代码:
# add an event trigger in the properties section of your function
Events:
MySQSEvent:
Type: SQS
Properties:
Queue: !GetAtt MySqsQueueName.Arn
BatchSize: 1
# then define the queue
MySqsQueueName:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 800
ReceiveMessageWaitTimeSeconds: 10
DelaySeconds: 10
RedrivePolicy:
deadLetterTargetArn: !GetAtt MyDLQueue.Arn
maxReceiveCount: 2
# define a dead letter queue to handle bad messages
MyDLQueue:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 900
希望这对某人有所帮助 - 我花了很长时间才开发我的应用程序!
我也面临着完全相同的问题。问题出在我的 lambda 函数中。
如果批量大小大于 1,在这种情况下,在单个 lambda 调用中,多个 SQS 消息将传递给 lambda(基于批量大小),只需处理 lambda 中的所有消息(通过遍历所有消息)。
检查您的事件记录数组是否有多条消息。
{Records: [{..},{..},{..}]}
我已经使用 Lambda 和 SQS 设置了一个小型无服务器应用程序。 在我的例子中,我想 触发 一个 lambda 每次 消息 添加 到 SQS 队列。
我的 serverless.yml
中的函数functions:
collectGame:
handler: js/collect.collectGame
memorySize: 128
timeout: 10
events:
- sqs:
arn:
Fn::GetAtt:
- gameRequestQueue
- Arn
- http:
method: post
cors:
origin: "https://my-api-url.com"
path: get/game/{id}
private: true
request:
parameters:
paths:
id:true
我通过一次向队列发送 31 条消息来测试该过程,但意识到只有 9 个 Lambda 被执行(通过查看 cloudwatch 日志)。我查看了队列,可以确认它已填满所有消息,并且在触发 9 个 Lambda 后它是空的。
我预计会有 31 次 Lambda 执行,但事实并非如此。有人知道我的 Lambda 没有被消息触发的潜在原因吗?
您的 lambda 函数可能被多条消息调用。如果您只希望每次 lambda 调用发送一条消息,则在创建事件源映射时应该能够将 BatchSize 设置为 1
看起来您正在使用无服务器框架。请参阅他们的 SQS 事件 documentation 以设置批量大小。
对于任何使用 aws sam 的人来说,这里是提到批量大小的 link:here,查找副标题 'Configuring a Queue as an Event Source'。这是我在 yaml 中与 DLQ 一起设置它的代码:
# add an event trigger in the properties section of your function
Events:
MySQSEvent:
Type: SQS
Properties:
Queue: !GetAtt MySqsQueueName.Arn
BatchSize: 1
# then define the queue
MySqsQueueName:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 800
ReceiveMessageWaitTimeSeconds: 10
DelaySeconds: 10
RedrivePolicy:
deadLetterTargetArn: !GetAtt MyDLQueue.Arn
maxReceiveCount: 2
# define a dead letter queue to handle bad messages
MyDLQueue:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 900
希望这对某人有所帮助 - 我花了很长时间才开发我的应用程序!
我也面临着完全相同的问题。问题出在我的 lambda 函数中。 如果批量大小大于 1,在这种情况下,在单个 lambda 调用中,多个 SQS 消息将传递给 lambda(基于批量大小),只需处理 lambda 中的所有消息(通过遍历所有消息)。
检查您的事件记录数组是否有多条消息。
{Records: [{..},{..},{..}]}