I am trying to connect aws documentdb from local environment following the docs Connecting to an Amazon DocumentDB Cluster from Outside an Amazon VPC - Amazon DocumentDB
i am able to connect from mongo shell but not from GO application
Tunneling command
ssh -L 27017::27017 @ -N
Mongo Shell(this is working)
mongo --sslAllowInvalidHostnames --ssl --host localhost:27017 --sslCAFile rds-combined-ca-bundle.pem --username --password
Sample Go codes(This is not working)
connectionURI := mongodb://:@localhost:27017/?sslCAFile=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&ssl=true&sslAllowInvalidHostnames=true&retryWrites=false&sslInsecure=true&sslVerifyCertificate=false
tlsConfig, err := getCustomTLSConfig(caFilePath)
if err != nil {
fmt.Printf(“Failed getting TLS configuration: %v”, err)
}
// Connect to MongoDB
client, err := mongo.NewClient(options.Client().ApplyURI(connectionURI).SetTLSConfig(tlsConfig))
//client, err := mongo.NewClient(options.Client().ApplyURI(connectionURI))
if err != nil {
fmt.Println("client error: ", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
fmt.Println("connect error: ", err)
}
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err = client.Ping(ctx, nil)
if err != nil {
fmt.Println("ping error: ", err)
}
fmt.Println(“Connected to MongoDB!”)
collection := client.Database(“mytestdb”).Collection(“mytestcollection”)
res, err := collection.InsertOne(context.TODO(), bson.M{“name”: “pi”, “value”: 3.14159})
if err != nil {
fmt.Printf(“Failed to insert document: %v”, err)
}
getting following errors
i. ping error: context deadline exceeded
ii. Failed to insert document: server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: test-1.test.us-west-2.docdb.amazonawscom:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(test-docdb-1.test.us-west-2.docdb.amazonawscom:27017[-3]) incomplete read of message header: EOF }, { Addr: test-docdb-3.test.us-west-2.docdb.amazonawscom:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(test-docdb-3.test.us-west-2.docdb.amazonawscom:27017[-4]) incomplete read of message header: EOF }, { Addr: test-docdb-2.test.us-west-2.docdb.amazonawscom:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(test-docdb-2.test.us-west-2.docdb.amazonawscom:27017[-5]) incomplete read of message header: EOF }, ]
I would greatly appreciate for any help/suggestion.
(I am new to Go language)
Thanks