AWS Document DB no coonect with MongoClient

23 Views Asked by At

I am trying to migrate my data from Atlas(mongo db) to AWS(Document DB). as new in Document DB i am not able to connect with and my mongo client given me error with connection time out.

step to create Document DB cluster on AWS.

I have tried the documentdb setup, and i have successfully setup the documentdb cluster, as i able to connect to datasource through pycharm.

Follow the below steps to create the db cluster:-

  1. created a vpc
  2. created internet gateway
  3. attached internet gateway to vpc
  4. created two public subnet in AZ [us-east-1a, us-east-1b], with default route table
  5. added one route entry as all trafic and giving source as internet gateway
  6. created subnet group by attaching both public subnet
  7. created cluster by attaching subnet group
  8. created ec2 instance and attached newly attached subnet group which contain below inbound rules
    -> ssh with port 22
    -> alltraffic with port all
  9. modify inbound rule of security group attached to db cluster
    -> custom tcp with port 27017 and source= security group of ec2 instance

and following my python script for connection

import pymongo
import sys

client = pymongo.MongoClient("mongodb://<user:password>@domain-url:27017/?replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false")

##Specify the database to be used
# db = client.get_database()
db = client.testDB

##Specify the collection to be used
col = db.sample_collection

##Insert a single document
col.insert_one({'hello': 'Amazon DocumentDB'})

##Find the document that was previously written
x = col.find_one({'hello': 'Amazon DocumentDB'})

##Print the result to the screen
print(x)

##Close the connection
client.close()

error:

pymongo.errors.ServerSelectionTimeoutError: docdb-2024-02-20-05-17-15.cluster-c94se2606167.us-east-1.docdb.amazonaws.com:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 65d445c1fe4ee021fa173a2e, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('docdb-2024-02-20-05-17-15.cluster-c94se2606167.us-east-1.docdb.amazonaws.com', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('docdb-2024-02-20-05-17-15.cluster-c94se2606167.us-east-1.docdb.amazonaws.com:27017: timed out')>]>

need basic step to create documentDb cluster and basic step to connect using python mongocliect

0

There are 0 best solutions below