DEV Community

Kevin Lewis
Kevin Lewis

Posted on

Accessing MongoDB Atlas From Netlify Functions

I recently tried to use a MongoDB Atlas instance with Netlify Functions and couldn't seem to connect to the database (I could locally). Here's why, and how I fixed it

Cause of the problem

When setting up a MongoDB Atlas Cluster you must provide a list of allowed IPs - with a one-click button to enable your own machine.

During local development everything will work file, but when you push your functions to Netlify the IP address will no longer match the one in the allowed list.

Unfortunately, Netlify don't provide the IP range for the machines that run serverless functions (as is the very nature of serverless), so this led me down a rabbit hole.

It is a single button

After spending a couple of hours to find the solution, I discovered it was just a single button click. In 'Network Access' in your cluster settings click 'Add IP Address'.

Then click 'Allow Access From Anywhere' and confirm your changes.

Hopefully this short post can save you debugging time.

Discussion (4)

borche profile image
Andreas Börjesson

Thanks for this info

coisox profile image
Aizal Manan

What!!!!!!!!!! You really really really really save my time! After a very long journey. The should have make it as default and optionally allow to specify IP for better security.

mral_x profile image
Alex Ferreira

Thanks for this! It would've taken waaaaaay longer than it already did for me to get to this conclusion! 🙏

_phzn profile image
Kevin Lewis Author

You’re very welcome!