0
votes

I've having issues accessing MongoDB Atlas from Google Cloud functions. It is giving me error regarding IP Whitelisting but I've added both (Serverless VPC Access) IP address range and VPC Network Peering IP address range to MongoDB whitelist.

I've also created MongoDB peering with google cloud.

If I allow (access from anywhere) then my mongodb starts working fine, otherwise it gives error regarding IP whitelisting.

I'm not sure what else I should add to MongoDB whitelist when I've added both IP's already.

Can anyone help me regarding this? A simple step by step guide will mean a lot. (images/video can help big if possible)

**Edit

I took (Atlas GCP Project ID & Atlas VPC Name) to create (VPC Network Peering). And they both are (Active & Available).

enter image description here

enter image description here

And after that I created (Serverless VPC Access).

enter image description here

And added it to my function inside (connection), a function that will connect to mongoDB to get data. It works fine if I set mongoDB to (allow from everywhere) but do not work without it.

enter image description here

And after that I added all 3 IP's/CIDR blocks to the IP Whitelist.

  • The CIDR Block from MongoDB Atlas as in 1st image.
  • And CIDR Block from Serverless VPC Access.
  • And CIDR Blcok from VPC Network as well just like all above.

But I've still confused that when I run this function it still gives me error about IP Whitelist and only works if I allow traffic from everywhere in mongoDB.

Don't know what I'm doing right and what I'm doing wrong. As there aren't any videos available on internet to achieve this.

I even tried this article but still nothing works out.

https://medium.com/better-programming/connecting-google-cloud-functions-with-mongodb-atlas-499a0a82ccf3

This is the error I'm getting.

enter image description here

1
Is MongoDB Atlas in your VPC? If not, whitelisting VPC private IP addresses will not work. Edit your question with details. - John Hanley
You talk about serverless VPC access, Peering, and public IP whitelisting. Can you detail more precisely your architecture? Do you use Cloud NAT? Do you use private IP to connect to MongoDB Atlas? What is your egress configuration on your Cloud Function? Please, more details! - guillaume blaquiere
I just updated the question with more information, let me know if you guys need more detail. I really need help with this because I'm stuck with this for about 24-48hrs. - Newbie
Did you whitelist the CIDR-range you set for the VPC-access-connector? Both in Atlas and GCP?! I think that's what you meant with "And CIDR Block from Serverless VPC Access", but wasn't sure. I wrote an answer for App Engine once. Maybe there's some info that will help: stackoverflow.com/a/66446153/4341096 - AndyW

1 Answers

1
votes

If you know you need to whitelist specific IPs:

  • Whitelist all IPs.
  • Connect successfully.
  • Download server log.
  • Figure out which IP the connection came from.
  • Whitelist that IP.

Verify this IP is in your expected range, etc.

If you know you don't need to whitelist specific IPs:

  • Reference Atlas documentation that says so and explains how VPC peering is supposed to work (medium posts are not a substitute for official documentation).

If you don't know whether specific IPs need to be waitlisted:

  • Follow the first procedure and whitelist your IPs.
  • Then look for official documentation stating what the proper usage would be.