title: NodeJS in AWS Lambda to Mongo Atlas - connection fails with no logs
body: <p>I am JS newbie so this may be some silly trouble. I have a lambda written in NodeJS 10.x and I am trying to add MongoDB Atlas insertion. I have started with this tutorial: <a href="https://docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/" rel="nofollow noreferrer">https://docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/</a></p>
  
  <p>This is my code:</p>
  
  <pre><code>const MongoClient = require('mongodb').MongoClient;
  let cachedDb = null;
  
  function connectToDatabase (uri) {
      console.log('Connect to mongo database');
  
      if (cachedDb) {
          console.log('Using cached database instance');
          return Promise.resolve(cachedDb);
      }
  
      return MongoClient.connect(uri)
          .then(db =&gt; {
              console.log('Successful connect');
              cachedDb = db;
              return cachedDb;
          }).catch(err =&gt; {
              console.log('Connection error occurred: ', err);
              callback(err);
          });
  }
  
  function insertUser(db, email) {
      console.log('=&gt; modify database');
      return db.collection('users').insertOne({"email" : email})
          .then(() =&gt; { callback(null, result); })
          .catch(err =&gt; {
              console.log('Insert error occurred: ', err);
              callback(err);
          });
  }
  
  exports.handler = (payload, context, callback) =&gt; {
      const { email, password } = JSON.parse(payload.body);
  
      context.callbackWaitsForEmptyEventLoop = false;
      connectToDatabase(MONGODB_URI)
          .then(db  =&gt; {
              console.log('Mongo connected')
              insertUser(db, email);
              })
          .then(result =&gt; {
              console.log('Mongo insert succeeded', result);
          })
          .catch(err =&gt; {
              console.log('Mongo insert failed', err);
              return responses.INTERNAL_SERVER_ERROR_500(err, callback, response);
          });
  
      console.log('finished mongo stuff');
  </code></pre>
  
  <p>I can see the following logs in CloudWatch:</p>
  
  <pre><code>START RequestId: 0338d336-7d33-40d5-abc7-1511f1c9ea4c Version: $LATEST
  2020-01-11T12:18:00.808Z    0338d336-7d33-40d5-abc7-1511f1c9ea4c    INFO    Connect to mongo database
  2020-01-11T12:18:00.855Z    0338d336-7d33-40d5-abc7-1511f1c9ea4c    INFO    finished mongo stuff
  2020-01-11T12:18:01.416Z    0338d336-7d33-40d5-abc7-1511f1c9ea4c    ERROR   (node:8) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
  END RequestId: 0338d336-7d33-40d5-abc7-1511f1c9ea4c
  </code></pre>
  
  <p>The item is not inserted in Atlas. I have added more verbose logging but it is not shown. If the mongo connect failed there shall be an error. But it seems that the error handlers are ignored. Where is the problem?</p>