Why is AWS Lambda always disabled?

I am testing aws lambda using nodejs with version 4.3. I can successfully complete all the statements in my handler function in the console test, which includes connecting to the mongodb host in our vpc. But the function always expires. I found several posts and resources that discuss using callbacks and setting properties in context and resolving the IAM role, but no matter what I do, it always ends in a timeout. 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 a problem on mongo");
            callback("problem", "status not so good");
        }
    });

    callback(null, {"status":"outside of device find block"});
}
+4
source share
1 answer

You have a typo:

context.callbackWaitsForEmtpyEventLoop = false;

it should be:

context.callbackWaitsForEmptyEventLoop = false;

callbackWaitsForEmptyEventLoop:

callbackWaitsForEmptyEventLoop

true. . , Node.js , . false, AWS Lambda, callback, . AWS Lambda , Node.js( , Lambda , AWS Lambda ). . .

:

// Times out due to typo
exports.function1 = (event, context, callback) => {
    setInterval(() => console.log('Long wait'), 100000);
    context.callbackWaitsForEmtpyEventLoop = false;
    callback(null, 'Hello from Lambda');
};

// Returns successfully
exports.function2 = (event, context, callback) => {
    setInterval(() => console.log('Long wait'), 100000);
    context.callbackWaitsForEmptyEventLoop = false;
    callback(null, 'Hello from Lambda');
};
+8

Source: https://habr.com/ru/post/1666653/


All Articles