Unable to Connect to Mongo Atlas from GCP App Engine

Viewed 505

I am trying to connect a nodejs app running on GCP App Engine to Mongo Atlas but I am receiving network errors. I have set up VPC Peering and have enabled a Serverless VPC connector and added the vpc_access_connector property in the app yaml and have whitelisted the IP range of the connector within Atlas. The exact error that I am seeing is

> MongoNetworkError: failed to connect to server [testcluster-shard-00-00-fbyja.gcp.mongodb.net:27017] on first connect
> [MongoNetworkError: connection 4 to testcluster-shard-00-00-fbyja.gcp.mongodb.net:27017 closed]

and the code I have is

var express = require("express");
var app = express();
require('dotenv').config();
var cors = require('cors');
const BodyParser = require('body-parser');
const MongoClient = require("mongodb").MongoClient;
const ObjectId = require('mongodb').ObjectID;
const CONNECTION_URL = process.env.DB_CONNECTION;

app.use(cors());
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({extended: true}));

var database, collection;

app.listen(process.env.PORT, () => {
    MongoClient.connect(CONNECTION_URL, {useNewUrlParser: true}, (error, client) => {
        if(error){
            throw error;
        }
        database = client.db("test");
        collection = database.collection("test");
    })
});
2 Answers

Unfortunately, you cannot configure a peering connection for M0/M2/M5 in Atlas.

Network Peering Connections You cannot configure Set up a Network Peering Connection on M0 Free Tier or M2/M5 shared clusters.

Moreover, since you are using App Engine, your application is behind GCP's load balancers and you won't be able to whitelist a stable set of static IP.

Possible solutions at this point:

  • Upgrade your Atlas cluster to an M10 to get access to the peering feature. This could cost you ~60USD/month
  • If it is not for production and you don't want to put money into this, I would suggest you using a free tier instance to deploy your app, then whitelist its IP for the Atlas clustier tier

Although you can not use GCP VPC Network Peering for Mo/M2/M5 clusters in Atlas.

You can set up a Cloud NAT with a static IP that connects with the VPC Network. You can add the static IP of your Cloud NAT to Atlas IP whitelist.

Related