Connecting Google Cloud Functions With MongoDB Atlas
By: Patrick Collins
Tags: Programming, JavaScript, Database, Google Cloud Platform, Mongodb

Step by Step

You can find a version of these steps in the MongoDB documentation and the Google Cloud documentation. Note that this only works if you have an M10+ version of the MongoDB Atlas cluster.

Set up a virtual private cloud on the GCP

VPC networks on the GCP

Go to your Google Cloud Console Home. Go to VPC Networks. You can either leave the default as default or add a new network here by clicking Create VPC Network . Be sure to grab your Project ID . You can find this by hitting the dropdown (in this case, you can see mine is chainlink ) and looking at the project ID. It will be next to the project name.

You will need your Project ID and VPC Network Name for the next part. In my case, the network name is default .

Original Image from MongoDB — the network name is “example” here.

Set up your MongoDB network peering

Navigate to your cluster that you want the access for. In the Security section of the left navigation, click Network Access. In the Peering tab, click the plus icon to Add Peering Connection. In the Peering Connection modal, select Google Cloud Platform and click Next. Enter the required information in the Peering Connection modal.

Project ID

GCP Project ID of the peer VPC. Refer to the dialog for instructions on finding your VPC Network Name

Name of the peer VPC. Refer to the dialog for instructions on finding your VPC Name.Atlas CIDR CIDR block for your Atlas cluster. (You can ignore this)

6. Then hit Initiate Peering .

Set up network peering on the GCP

Now that this is set up, your MongoDB instance is looking to start peering with your GCP VPC (a lot of acronyms in this world).

Back in the Google Cloud Console, click VPC network peering. Click Create Connection. Click Continue. In Name, enter a name for your peering connection. This can be anything you like. In Your VPC Network, enter the name of your GCP VPC network. For our example, we stuck with default . In Peered VPC network, select In another project. In Project ID, enter your Atlas GCP Project ID.

You can find this name in the VPC Peering view. In the Security section of the left navigation, click Network Access and then click the Peering tab.

IDs for your network peering access

8. In VPC network name, enter your Atlas VPC Name and hit Create.

Once this is done, in a few minutes you’ll see a green dot next to it in your Atlas MongoDB instance.

9. You’ll then need to whitelist your new VPC CIDR block address . You can find this on the network access peering tab too.

VPC CIDR block address

We finally get to go back to Cloud Functions now.

Set up serverless access to the VPC

At this point, if you use that VPC, services like Cloud Engine VMs (or anything with a static IP) will work. These will be able to connect to MongoDB via the VPC. What won’t work is our Cloud Functions since they don’t have access to the VPC. Remember, they are being spun up on IPs we don’t know, so we can’t whitelist them!

Easily enough, we can just give them:

Head over to Serverless VPC Access . Hit Create Connector . Enter a name and region. We will call it vpc-access . Choose the network/VPC that we just set up with MongoDB. Pick an unused/28 CIDR range in your VPC network, such as 10.8.0.0/28 . This usually works for most applications. It will raise an error otherwise. Hit Create. Head over to the Cloud Function that we want to give access to our database to. Hit Edit. At the bottom, we can go to the connections tab and add our new vpc-connector!

We now should be all set up to have our serverless Google Cloud Function connect to our MongoDB Atlas instance! You can test it out by trying to connect to a cluster locally and then connecting via our Cloud Function.