title: How to call mongodb functions from an aws lambda in the Serverless Framework?
body: <p>I've been watching video tutorials on the Serverless Framework such as <a href="https://www.youtube.com/watch?v=Xpk2CMNmUYo&amp;t=322s" rel="nofollow noreferrer">this one</a> and based on the information I've gathered, it appears that using the framework is one of the most efficient ways to manage aws lambda functions. In the video link above, an example is given that uses a lambda function to connect to a DynamoDB table and then save data it. What I need to do is to connect to a MongoDB table and then get data from it.</p>
  
  <p>I've only found examples of using DynamoDB with Serverless but none with MongoDB. I thought that the process should be similar, but that doesn't seem to be the case. I found <a href="https://www.mongodb.com/blog/post/serverless-development-with-nodejs-aws-lambda-mongodb-atlas" rel="nofollow noreferrer">this tutorial</a> but it doesn't use the serverless framework.</p>
  
  <p>Here is an example of connecting and saving to a Dynamo db table from a lambda in serverless framework:</p>
  
  <pre><code>const uuidV1 = require('uuid/v1');
  const AWS = require('aws-sdk');
  const promisify = require('es6-promisify');
  const _ = require('lodash');
  const dynamo = new AWS.DynamoDB.DocumentClient();
  
  module.exports.saveUserToDatabase = function(userId, coffeeType, coffeeSize) {
    console.log('saveUserToDatabase');
  
    const item = {};
    item.drink = coffeeType;
    item.size = coffeeSize;
    item.userId = userId;
  
    return saveItemToTable('coffee-user-table', item);
  };
  </code></pre>
  
  <p>The following snippet is code used to connect to a MongoDB Atlas database using an npm package called lambda-local:</p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient;
  
  let atlas_connection_uri;
  let cachedDb = null;
  
  exports.handler = (event, context, callback) =&gt; {
      var uri = process.env['MONGODB_ATLAS_CLUSTER_URI'];
  
      if (atlas_connection_uri != null) {
          processEvent(event, context, callback);
      } 
      else {
          atlas_connection_uri = uri;
          console.log('the Atlas connection string is ' + atlas_connection_uri);
          processEvent(event, context, callback);
      } 
  };
  
  function processEvent(event, context, callback) {
      console.log('Calling MongoDB Atlas from AWS Lambda with event: ' + JSON.stringify(event));
  }
  </code></pre>
  
  <p><strong>Is there a way to implement the logic used for MongoDB Atlas and npm lambda-local except with Serverless Framework?</strong></p>