Stateful AWS lambdas part2: scheduled lambda invoking every second

Building on Part 1, one of the restrictions of lambda is the the minimum scheduled time allowed by the Cloudwatch event trigger, which is 1 minute. What happens if you need to invoke your lambda every second? It’s not really possible using the current triggers, but we can build our own. To do this, we’ll have 2 lambdas

  • 1 target lambda, the one we invoke every second
  • 1 trigger lambda, which runs for exactly 1 minute and inside that minute, we trigger our target lambda every second

The target lambda

This can be anything, really – for demonstration purposes let’s put this in:

exports.handler = function (event, context, callback) {
    return callback(null, 'Doing some work!');
};

The trigger lambda

Following our pattern in Part 1:

var exitTimeout = false;
var lambdaTimeout = 60000;
var AWS = require('aws-sdk');
var lambda = new AWS.Lambda();

function main(callback) {
    exitTimeout = false;

    setTimeout(() => {
        exitTimeout = true;
    }, lambdaTimeout - 1000);

    function invokeWithTimeout() {
        if (exitTimeout === true) callback('Out of time, exiting');
        var params = {
            FunctionName: 'target',
            InvocationType: 'Event',
        };
        lambda.invoke(params).promise()
            .then(() => {
                console.log('Waiting a bit before invoking again');
                setTimeout(invokeWithTimeout, 1000);
            })
            .catch(console.log);
    }
    invokeWithTimeout();
}

exports.handler = function (event, context, callback) {
    return main(callback);
};
  • don’t forget, this lambda needs to have a role with a permission to invoke our target lambda
  • make sure you set the lambda timeout to 60 seconds
  • make sure to add an event source trigger for this lambda with an expression of 1 minuteScreen Shot 2016-12-26 at 9.21.24 PM.png

Your invocation graph should show ~60 invokes for that last minute:

Screen Shot 2016-12-26 at 9.23.31 PM.png

This won’t be perfect, since processing time is not taken into account (we only wait 1 second AFTER the invoke is done), but we could make this more sophisticated and precise if we wanted to.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s