Search code examples
amazon-web-servicesaws-lambdaamazon-cloudwatchamazon-snsaws-event-bridge

how do I delay the reading of a SNS message?


I currently have a lambda function that is subscribed to an SNS queue.

It is working the way it should and pretty soon after posting messages to this queue the event is being run.

The problem arrives when I get the requirement to postpone the lambda execution. i.e. push it an arbitrary amount of time into the future.

From what I can tell this isn't available out of the box with SNS. I had a look at scheduling it with eventbridge/cloudwatch, but it seems like I can't schedule the event per message and need to have predefined delays (up to 100 of them?), which doesn't really work for me.

So I guess the question is: Are there any relatively low effort solutions to delaying the reading of a message (or somehow postponing the send) on SNS?

I've thought about polling, but that sounds like a pretty high effort and expensive solution.


Solution

  • EventBridge Scheduler (launched in Nov 2022) supports One Time Schedules, which is probably what you want here. You can use the CreateSchedule API Call with the at(yyyy-mm-ddThh:mm:ss) syntax for ScheduleExpression. Note, this is different from the Scheduled Rules feature which was first supported in CloudWatch Events and which does not support One Time Schedules.

    Alternatively, you could put your logic into a Step Functions State Machine and use the Wait State with TimestampPath to dynamically set the time to wait for.

    If you simply want to run the single Lambda function, then the One Time Schedule will likely be best. But if you have multiple actions you'd like to coordinate, Step Functions might be best. And you can always combine them as as Step Functions is a target for EventBridge Scheduler.