Changed from AWS-self-hosted MongoDB to Atlas causes pymongo.errors.ServerSelectionTimeoutError:

I have an app that runs in ECS that connected to a self-hosted mongoDB EC2 instance in the same VPC. The connection url was the private IP of the EC2 instances. Below is the connection code

# # mongodb connection string from systen env
MONGODB_URL = os.environ.get('MONGODB_URL', 'localhost')
MONGODB_PORT= int(os.environ.get('MONGODB_PORT', '27017'))
client = MongoClient(MONGODB_URL, MONGODB_PORT)

I moved the DB to Atlas, established a VPC peering connection, whitelisted the AWS VPC network, and created a new DB user just for the app.

Then I changed the connection string in the ECS task to

MONGODB_URL: mongodb+srv://<username>:<password>@mongodb-prod.zzl18.mongodb.net/nameofDB?retryWrites=true&w=majority

Now when I run the program I get:

pymongo.errors.ServerSelectionTimeoutError: mongodb-prod-shard-00-00.zzl18.mongodb.net:27017: timed out,mongodb-prod-shard-00-02.zzl18.mongodb.net:27017: timed out,mongodb-prod-shard-00-01.zzl18.mongodb.net:27017: timed out

To see if it was a network issue, I opened the DB network access up to 0.0.0.0/0

I still receive the same error.

Maybe the issue is the connection string, but I’m not sure. Any help you can provide would be greatly appreciated.

I figured it out. The solution was as simple as adding the ECS program subnet to the VPC route table.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.