How to Trigger a Lambda Function Every 5–10 Seconds

This is the first post in a two part series. The second part presents an alternate solution which is generally better, but has some trade-offs. I recommend reading both.

Overview: serverless.yml

I used Serverless Framework to put everything together. From line 20 onward, the serverless.yml below shows the everyminute and interval functions being created, along with the SQS queue.

Function: everyminute

As the name suggests, this function is triggered every minute by an EventBridge rule. The everyminute handler code below creates a series of delayed messages and sends them to the SQS queue in batches of up to 10.

Function: interval

This function is configured to consume messages from the SQS queue.

SQS Queue

I want to explicitly call out that I set the message retention period to 70 seconds (the default retention period is 4 days). I did this because I don’t want to build up a large backlog of visible messages if something breaks.

How accurate is it?

EventBridge

There are two aspects of accuracy to look at. The first is how accurate (or consistent) EventBridge is when invoking the everyminute function.

Lambda & SQS

Next we’ll look at the delayed messages being processed by the interval function. SQS messages reliably become visible after the delay, so the main concern is the one raised by Chase Douglas on Twitter about Lambda’s polling behaviour. Is it frequent enough?

The raw invocation times times on GitHub.

Enhancements

To take this idea further, it might be worth changing the everyminute function to intelligently adjust the delays in case it’s invoked earlier or later. EventBridge was consistent during testing, but maybe it’s less consistent over a longer period.

Conclusion

Honestly, I think this a success. I’m impressed by the accuracy. If you need more accuracy than this, you may be better off with a different solution.

Principal Engineer @ Just Eat Takeaway.com | AWS Community Builder