Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

like image 610
Affan Shahab Avatar asked Apr 03 '19 08:04

Affan Shahab


1 Answers

The best solution is to use AWS Step Functions.

The lambda triggered by the SQS should execute a state machine, where the first step is the required amount of sleep, and the second one is the lambda invocation.

like image 147
Ronyis Avatar answered Nov 01 '22 10:11

Ronyis