How to trigger lambda with some delay when message published to SQS?

Aff*_*hab 2 triggers delay amazon-sqs aws-lambda aws-sam

I have a lambda configured to be triggered when messages are published to SQS queue. Here is the SAM template for deployment.

  MyQueue:
    Type: AWS::SQS::Queue
    Properties:
        VisibilityTimeout: 180
        DelaySeconds: 90

  MyLambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ../pathToCode
      Handler: index.handler
      Events:
        MySQSEvent:
          Type: SQS
          Properties:
              Queue: !GetAtt MyQueue.Arn
Run Code Online (Sandbox Code Playgroud)

I am using DelaySeconds property of AWS::SQS::QUEUE which apparently doesn't work. My lambda get executed as soon as the message is published to queue. How can I put delay in it?

Ron*_*yis 5

最好的解决方案是使用AWS Step Functions。

由SQS触发的lambda应该执行状态机,其中第一步是所需的睡眠量,第二步是lambda调用。


ffx*_*sam 5

2021年,最好的答案是使用SQS新的“批处理窗口”功能:

https://aws.amazon.com/about-aws/whats-new/2020/11/aws-lambda-now-supports-batch-windows-of-up-to-5-minutes-for-functions/

使用此功能,您可以允许 SQS 队列在调用 Lambda 函数之前收集消息长达 5 分钟。