Sabreena Sabreena - 1 year ago 173
Javascript Question

How to use setInterval in aws lambda function

I was experiencing difficulties when invoking same lambda function continuously by using setInterval function.

lambda function

var MongoClient = require('mongodb').MongoClient
, format = require('util').format;

function funUpdateCommand(event,context,callback){

var mongoUrl='mongodb://**.**.**.**:*****/DBname';
// var mongoUrl='mongodb://';

MongoClient.connect(mongoUrl, function(err, db) {

if(err) throw err;

var collection = db.collection('device');

var interval = setInterval(function() {
collection.find({"deviceCommand.command":"getAudio","deviceCommand.timestamp":{ $lte: new Date((new Date)*1 - 60000*2)}}).toArray(function(err, results) {
for(var i=0;i<results.length;i++){
collection.update({_id:results[i]._id},{$set:{"deviceCommand.command":" "}},function(err, results) {

}, 5000);

context.succeed("Successfully uploaded");



I am trying to update some of the documents in my mongoDB,i need to run the aws lambda function as a continuous background job ,but when using setInterval it returns timeout error .

How can i continuously run my aws lambda function using setInterval?

Answer Source

You can not keep lambda running forever. Lambda functions life time is limited with 300 seconds, after 300 seconds, your function dies. You can invoke same lambda function with cron expressions using CloudWatch Events.

You can learn more about lambda limits from here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download