Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (19.1k points)

I am testing out aws lambda, using nodejs with the 4.3 version. I'm able to successfully complete all the statements in my handler function within the console test, which includes connecting to a MongoDB host within our vpc. But, the function always times out. I've found several posts and resources that discuss using the callback, and setting properties on the context, and IAM role permissions, but no matter what I do, it always ends up timing out. Current code: 

'use strict';

 

var Mongoose = require('mongoose');

var Device = require('./device_model');

var Alarm = require('./alarm_model');

var Event = require('./event_model');

 

var mongoConnection = process.env.MONGO_URL;

 

var connection = Mongoose.connect(mongoConnection);

 

Mongoose.connection.once('open', function() {

    console.log("Connecting to mongo at: " + mongoConnection);

    console.log("Mongoose connection in lambda opened");

});

 

Mongoose.connection.on('error', function(){

    console.error("Error creating mongoose connection in lambda, exiting!");

    process.exit(1);

});

 

exports.check_alarms = function(event, context, callback) {

 

    context.callbackWaitsForEmtpyEventLoop = false;

    console.log("The incoming event: " + JSON.stringify(event));

 

    var device = null;

    Device.findByUUID(event.uuid, function(error, result){

        if(!error){

            device = result;

            console.log("the device: " + JSON.stringify(device));

            if(event.Ale && event.Ale.length > 0) {

                console.log("We have an alarm, checking if already set");

                callback(null, {"status":"alarms"});

            } else {

                console.log("Event contains no alarm; checking for historic active");

                callback(null, {"status":"no alarms"});

            }

        } else {

            console.log("there's a problem on mongo");

            callback("problem", "status not so good");

        }

    });

 

    callback(null, {"status":"outside of device find block"});

}

1 Answer

0 votes
by (44.4k points)

There is a typo, Emtpy:

context.callbackWaitsForEmtpyEventLoop = false;

This will work:

context.callbackWaitsForEmptyEventLoop = false;

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories

...