Configure a Time-Specific AWS Lambda SQS Trigger

DZone 's Guide to

Configure a Time-Specific AWS Lambda SQS Trigger

To avoid being disturbed by notifications at those early morning hours, check out how you can configure lambdas to come out only at certain times.

· Cloud Zone ·
Free Resource

We use AWS Lambda to send transactional emails and push notifications. When we 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 a third-party API solution 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 using to try 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 at 8am and 10pm. Listener functions are disabled at 10pm and are enabled at 8am 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.

const AWS = require('aws-sdk')

exports.handler =  (event) => {
    var lambda =  new AWS.Lambda({apiVersion: '2015-03-31'});
    var today = new Date();
    var hour = today.getHours();

    var pushListenerParams, emailListenerParams
    if (hour > 7 && hour < 23) {
        pushListenerParams = getPushListenerParams(true);
        emailListenerParams = getEmailListenerParams(true);
    } else {
        pushListenerParams = getPushListenerParams(false);
        emailListenerParams = getEmailListenerParams(false);

    lambda.updateEventSourceMapping(pushListenerParams, function(err, res) {
        if(err) console.log('Push Listener Error => ' + JSON.stringify(err));
        else console.log('Push Listener Response => ' + JSON.stringify(res));

     lambda.updateEventSourceMapping(emailListenerParams, function(err, res) {
         if(err) console.log('Email Listener Error => ' + JSON.stringify(err));
         else console.log('Email Listener Response => ' + JSON.stringify(res));

     return "OK";

function getPushListenerParams(enabled) {
    return {
        BatchSize: 10,
        Enabled: enabled,
        FunctionName: "arn:aws:lambda:<region>:<your account id>:function:Push-Listener",
        UUID: "SQS Queue UUID (You should use AWS API/CLI while creating SQS Queue to see Queue UUID from API Response)"

function getEmailListenerParams(enabled) {
    return {
        BatchSize: 10,
        Enabled: enabled,
        FunctionName: "arn:aws:lambda:<region>:<your account id>:function:Email-Listener",
        UUID: "SQS Queue UUID (You should use AWS API/CLI while creating SQS Queue to see Queue UUID from API Response)"

I hope it will be helpful for you. Don’t hesitate if you have any questions. Thanks!

aws ,aws lambda ,aws sqs ,aws cloudwatch ,sqs ,lambda ,cloudwatch ,serverless ,cloud

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}