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>

Now when I run the program I get:

pymongo.errors.ServerSelectionTimeoutError: timed out, timed out, timed out

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

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.