title: Set up Mongodb Lambda function Greengrass Component
body: <p>I am trying to successfully run a lambda function as greengrass component.
  My requirement is to have the function access the local mongodb server on my greengrass device.</p>
  <p>I am able to deploy the function with <strong>no container</strong> approach and also generate the component log files locally on the greengrass device.</p>
  <p>The component runs without any error, but does not return or print the mongodb read output.</p>
  <p>My greengrass device is AWS EC2 ARM instance with Ubuntu 18.0 using nodejs code.</p>
  <p>PFB the code for lambda function,</p>
  <pre><code>    // Import the MongoDB driver
  const MongoClient = require(&quot;mongodb&quot;).MongoClient;
  
  // const MONGODB_URI = &quot;mongodb+srv://&lt;USERNAME&gt;:&lt;PASSWORD&gt;@cluster0.cvaeo.mongodb.net/test?retryWrites=true&amp;w=majority&quot;;
  const MONGODB_URI ='mongodb://localhost:27017/ODIN_Local:4000/ODIN_Local?retryWrites=true';
  
  // Once we connect to the database once, we'll store that connection and reuse it so that we don't have to connect to the database on every request.
  let cachedDb = null;
  
  async function connectToDatabase() {
    if (cachedDb) {
      return cachedDb;
    }
  
    // Connect to our MongoDB database hosted on MongoDB Atlas
    const client = await MongoClient.connect(MONGODB_URI);
  
    // Specify which database we want to use
    const db = await client.db(&quot;ODIN_Local&quot;);
  
    cachedDb = db;
    return db;
  }
  
  exports.handler = async (event, context) =&gt; {
  /**
   * By default, the callback waits until the runtime event loop is empty before freezing the process 
   * and returning the results to the caller. Setting this property to false requests that AWS Lambda
   * freeze the process soon after the callback is invoked, even if there are events in the event loop.
   * AWS Lambda will freeze the process, any state data, and the events in the event loop.
   * Any remaining events in the event loop are processed when the Lambda function is next invoked, 
   * if AWS Lambda chooses to use the frozen process. 
   */
  //   context.callbackWaitsForEmptyEventLoop = false;
  
    // Get an instance of our database
    const db = await connectToDatabase();
  
    // Make a MongoDB MQL Query to go into the customer collection and return the first 20 movies.
    const customer = await db.collection(&quot;customer&quot;).find({}).limit(20).toArray();
  
    const response = {
      statusCode: 200,
      body: JSON.stringify(customer),
    };
  
    return response;
  };
  </code></pre>
  <p>I am stuck at this and any help would be appreciated.</p>
  <p>Regards,
  Aarushi</p>