0

I have setup an AWS lambda to do some data saving for me to MongoDB. I'd like to reuse the connection so I dont have to create a new connection every time the lambda is invoked. But if I leave the db connection open, the callback for the Lambda handler doesnt work!

Is there something I'm doing wrong thats creating this behavior? Here is my code:

var MongoClient = require('mongodb').MongoClient

exports.handler = (event, context, callback) => {
    MongoClient.connect(process.env.MONGOURL, function (err, database) {
        //database.close();
        callback(null, "Successful db connection")
    });
}
AlexKogan
  • 355
  • 1
  • 5
  • 16
  • [From the documentation:](http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html) *"Any declarations in your Lambda function code (outside the handler code, see Programming Model) remains initialized, providing additional optimization when the function is invoked again. For example, if your Lambda function establishes a database connection, instead of reestablishing the connection, the original connection is used in subsequent invocations. You can add logic in your code to check if a connection already exists before creating one."* – Neil Lunn Nov 10 '17 at 23:48
  • So "in brief" the **same answers** for persisting and sharing connections between modular code ( or functions ) that apply to general nodejs applications also apply here. See [How do I manage MongoDB connections in a Node.js web application?](https://stackoverflow.com/questions/10656574/how-do-i-manage-mongodb-connections-in-a-node-js-web-application). Its just that your `getDB()` function you would implement needs to be declared "outside" of any "handler" code, and in the handler you simply grab the current "instance" of that connection, which is is fact "persisted". Just like the docs say. – Neil Lunn Nov 10 '17 at 23:52
  • @NeilLunn Lambda has to be told not to wait for the event loop to be empty, but instead to go ahead and return the result and freeze the container. – Michael - sqlbot Nov 11 '17 at 03:59
  • @Michael-sqlbot Not the point "I am addressing" here. What I am addressing is *"I'd like to reuse the connection so I don't(sic) have to create a new connection every time the lambda is invoked"* which is clearly the misconception in the question that there is a **need** to establish a connection with every function call. Clearly this is not true. – Neil Lunn Nov 11 '17 at 04:08
  • I see what you are saying, @NeilLunn. I interpreted the perceived "need" as a reaction to the fact that the function doesn't work as expected when the connection isn't closed, rather than a misconception that there should/would be such a need. – Michael - sqlbot Nov 11 '17 at 04:17

1 Answers1

4

This is caused by not setting context.callbackWaitsForEmptyEventLoop = false. If left at the default true, the callback does not cause Lambda to return the response because your database connection is keeping the event loop from being empty.

http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427