Fetching mongoDB data with lambda functions

I’m having an issue fetching mongoDB data using lambda functions when not working locally.

I set up a sample mongodb filled with their sample data and simply want to pull it to be used on my website. When I work locally via the netlify-cli I am able to properly query the database and return the value I’m looking for without error. It’s when I push the function up to my site that I begin to run into issues with it timing out.

My code can be seen below (username and pw changed)

/* eslint-disable */
const fetch = require('node-fetch');
const { MongoClient } = require('mongodb');

async function getData() {
  const uri =
    'mongodb+srv://<user>:<password>@cluster0-2pfxk.mongodb.net/test?retryWrites=true&w=majority';
  const client = new MongoClient(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  try {
    await client.connect();
    const test = await client
      .db('sample_airbnb')
      .collection('listingsAndReviews')
      .findOne({ name: 'Apt Linda Vista Lagoa - Rio' });
    return test;
  } catch (err) {
    console.log(err); // output to netlify function log
  } finally {
    await client.close();
  }
}

exports.handler = async function(event, context) {
  try {
    const data = await getData();
    return {
      statusCode: 200,
      body: JSON.stringify({ id: data._id })
    };
  } catch (err) {
    console.log(err); // output to netlify function log
    return {
      statusCode: 500,
      body: JSON.stringify({ msg: err.message }) 
    };
  }
};

As mentioned before, when run locally, this works as expected outputting the desired _id. It’s not until I push it to my project that I run into timeout issues. I made sure that I am closing the client, so I don’t think it’s a matter of me leaving that open, but I don’t know.

The function has the following endpoint: https://mongodb--virtualbackgrounds.netlify.com/.netlify/functions/mongo

Any advice or suggestions would be greatly appreciated!

Thanks.

1 Like

It took me awhile, but I finally realized that the issue was due to not whitelisting Netlify on MongoDB. This is why locally testing it was running fine, but then once it was pushed up it would timeout.

I temporarily set mongoDB to allow connections from everywhere, and sure enough it got the data.
Leading to my next issue that perhaps someone else knows something about – Setting up VPC Peering so that the lambda function will always be whitelisted for the DB.

I’m very much a beginner in terms of back-end so this is all pretty new to me. If anyone has any ideas or advice, I’m all ears!

1 Like

@braveux Funnily, I was in the same exact situation, struggled all day, and finally figured it out (whitelisting all) literally 10 minutes ago. I, too, would appreciate any community support/advice on this as I don’t want to leave the DB open to all connections.

Is VPC Peering the next thing to learn?

Hey @braveux and @lilbigwheel! Linking this conversation which I think may be useful:

TL;DR There’s no way to whitelist an IP range for our functions because there’s no way to know which range they will be spun up on when they’re invoked. Our suggestions in the past have been to whitelist everyone but make sure you have a very strong MongoDB user password (and put your DB_URL in a Netlify environment variable)… or switch to FaunaDB.

Maybe not the answer you wanted to hear, but hopefully it’s helpful- let us know if you find workarounds or if we can answer any other questions on this!