Newbie
Newbie

Reputation: 388

MongoDB and Google Cloud Functions VPC Peering?

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.

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

Upvotes: 0

Views: 1176

Answers (2)

Tomer
Tomer

Reputation: 400

I encountered the same issue, and ultimately discovered that I needed to use a private connection string rather than a standard one.

from the official documentation:

You must add your VPC CIDR block address (or subset) associated with the peer VPC to the IP access list before your new VPC peer can connect to your Atlas cluster. When connecting to your cluster, you must use the new private connection strings to utilize the peering.

https://www.mongodb.com/docs/atlas/security-vpc-peering/

Private connection strings examples:

mongodb://xyx456-shard-00-00-pri.ab123.mongodb.net:27017
mongodb+srv://xyx456-pri.ab123.mongodb.net

Upvotes: 0

D. SM
D. SM

Reputation: 14490

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.

Upvotes: 1

Related Questions