Connecting MongoDB Atlas & GCP virtual machine through private endpoint fails.

Info about the services :

  • “MongoDB Atlas” is being hosted on AWS cloud ,
  • Backend application is running in the GCP cloud.
  • Error Message: pymongo.errors.ServerSelectionTimeoutError:

The objective of the development is to use MIG and communicate with mongo atlas using private endpoint.
Due to this issue I am not able to use MIG to scale my servers. (MIG is a service by GCP.)
After successful creation of private endpoint I am not able to communicate with the database.
What I want is to successful communication with the mongo atlas DB.

I have used the following doc for the procedure.

Reference:

FYR: IP white listing works but I can’t use it in my case.

Things I have already tried:
Piece of code that i am using,

import pymongo

myclient = pymongo.MongoClient(“mongodb+srv://user:pass###@fon-dev.dasdsad.mongodb.net/fon-dev?ssl=true&ssl_cert_reqs=CERT_NONE&retryWrites=true&w=majority&authsource=fon-dev”, socketTimeoutMS=30000, connectTimeoutMS=30000)

mydb = myclient[fon-dev’]

print(“List of Collections\n--------------------”)
#list the collections
for coll in mydb.list_collection_names():
print(coll)

Printing myclient and mydb gives following results:

myclient

(MongoClient(host=[‘list of urls'], document_class=dict, tz_aware=False, connect=True, retrywrites=True, w='majority', authsource='fon-dev', replicaset='atlas-nksrzt-shard-0', sockettimeoutms=30000, connecttimeoutms=30000, tls=True),

mydb

Database(MongoClient(host=['list of urls'], document_class=dict, tz_aware=False, connect=True, retrywrites=True, w='majority', authsource='fon-dev', replicaset='atlas-nksrzt-shard-0', sockettimeoutms=30000, connecttimeoutms=30000, tls=True), 'fon-dev'))

It fails at this line “mydb.list_collection_names()“

I also tried connecting with mongo-shell, it also fails.

Other option:

When i tried pinging the system using, “ping fon-dev-shard-00-00.asdasd.mongodb.net

It works fine, with 0% packet loss.

I tried netcat command

CMD : nc -zv fon-dev-shard-00-00. asdasd*.mongodb.net 27017*

OP : nc: connect to fon-dev-shard-00-00. asdasd*.mongodb.net port 27017 (tcp) failed: Connection timed out*

I tried ufw command - for checking if firewall is blocking the connections.

CMD : sudo ufw status

OP : Status: inactive

All suggestions are appreciated.