2
votes

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
If you're not using TLS/SSL Atlas will simply drop the connection. Make sure your connection string uses +srv or explicitly enables TLS/SSLJoe
which cluster size are you using?gmolaire
@Joe The connection string is using +srvBrady Shober
@gumol It is an M0 clusterBrady Shober
@gumol No, allowing access from anywhere does allow the connection. I followed all of the steps for the VPC peering both from the Atlas and GCP documentation. It seems that despite setting this up the traffic to Atlas is still going through the external IP rather than the VPC, and I have not been able to figure out why this is.Brady Shober

2 Answers

4
votes

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
1
votes

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.