I'm trying to set up an API on the Google App Engine standard environment but I'm having trouble connecting to a distant MongoDB instance.
I use a free M0 cluster on MongoDB Atlas, all IP are whitelisted. The Google App Engine firewall rules allow all traffic from all IP addresses to make sure the connection request is not blocked.
However, I cannot connect to my Mongo instance and I get the following error:
ERROR db_connection querySrv ESERVFAIL <mongo-url>.gcp.mongodb.net
To connect to the MongoDB instance I use Mongoose and do the following:
const db = await mongoose.connect(uri, { useNewUrlParser: true });
However db
will always be null since I cannot connect. Is there a way to connect to MongoDB Atlas from App Engine Standard or do I have to use Cloud Datastore ?
It works for me with older driver version url:
You don't have to use Datastore. You can connect to a mongoDB hosted outside of Google servers, there is some example code here. I would also like to refer you to this documentation on connecting to external databases from the Standard Environment. Making sure your firewalls are open is necessary (and you have already done that, so that's great). MongoDB Node.JS drivers are listed here, depending on the version you use, different reference documents are available with connection samples (all listed on that same link).
I had to use the older version of the atlas url and set new url to false: