How to configure AWS Lambda SQS Trigger to consume messages only specific time?
We use AWS Lambda to send transactional emails and push notifications. When we aim to send an email or push notification we publish messages to related AWS SQS (Simple Queue Service). Our queue triggers Lambda function which processes messages in the related queue. After that, the Lambda function uses 3rd party API solutions to send email or notification. It’s simple so far, but we had problems with sending emails and notifications to our customers at nights. Customers might be disturbed to receive messages at nights and we have some messages that relate to customer transactions. So here, we designed this simple solution what we are trying to fix this problem.
There are two Lambda functions which are Push-Listener and Email-Listener. These two Lambda functions consume push and email queues. And also, there is one scheduled Lambda function which is Push-Email-SQS-Trigger-Configurer. This Lambda function controls these listener functions SQS Trigger configuration.We added Cloudwatch Events Cron Expression to trigger the configurer.
The configurer Lambda function is triggered by listener Lambda functions at8 am and 10 pm. Listener functions are disabled at 10 pm and are enabled at 8 am by the configurer. After that, enabled Lambda listeners’ SQS triggers are started to consume messages which are in queues. We used Update Event Source Mapping AWS API to enable or disable SQS Triggers.
Here is the code snippet to use as an example to make your own.
I hope it will be helpful for you. Don’t hesitate if you have any questions. Thanks!