Search code examples
amazon-web-servicesaws-lambdaamazon-sqs

Is it possible to give a few days delay when using sqs?


I need to handle some events in my lambda. When the lambda receive an event, it needs to update the data in database. But the event includes a date which is a future date, I need to implement a logic to write to database when at the future date.

For example, if today is 2021-08-20, when I revive this event, I need to write to database 11 days later. I am thinking to put these events in SQS delay queue but it has a maximum of 15 minutes delay time. And it is far less than what I expected ( could be a few weeks or even a few months later).

{
   effectDate: "2021-09-01",
   value: xxxx
}

another solution is to create a cloudwatch rule in my lambda. The rule will trigger another lambda to write to db. This solution also has a limitation that cloudwatch rule has a maximum 100 rules per region. I may receive thousands of events per day so I can't rely on this solution.

Using dynamodb TTL + streaming may work but the problem is dynamodb TTL doesn't work well for less than 48 hours. It runs TTL scheduler every 48 hours which means I can't schedule an event which get triggered less than 48 hours.

Is there any way to solve the issue?


Solution

  • You can use a step function for this. In Step Functions there is a Wait-State, that allows you to pause execution until a point in time or for a certain period of time. This can be set per execution.

    The docs for the wait state can be found here: Amazon State Language: Wait

    A wait state that dynamically waits until a point in time determined by the input (expirydate-key) looks like this:

    "wait_until" : {
        "Type": "Wait",
        "TimestampPath": "$.expirydate",
        "Next": "NextState"
    }
    

    You can do the same for a number of seconds:

    "wait_until" : {
        "Type": "Wait",
        "SecondsPath": "$.waitForSeconds",
        "Next": "NextState"
    }
    

    The general idea is that you create a step function with a task that extracts the amount of time to wait, the next state is the Wait-state and after the wait state you do whatever processing is necessary.