Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connection error while connecting to AWS DocumentDB

getting the following error while connecting to AWS DocumentDB from node.js

connection error: { [MongoNetworkError: connection 1 to docdb-2019-01-28-06-57-37.cluster-cqy6h2ypc0dj.us-east-1.docdb.amazonaws.com:27017 timed out] name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ] }

here is my node js file

app.js

var mongoose = require('mongoose');
mongoose.connect('mongodb://abhishek:abhishek@docdb-2019-01-28-06-57-37.cluster-cqy6h2ypc0dj.us-east-1.docdb.amazonaws.com:27017/?ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0', {
    useNewUrlParser: true
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    console.log("connected...");
});
like image 397
Abhishek Chudekar Avatar asked Dec 01 '22 14:12

Abhishek Chudekar


1 Answers

By default aws documentdb is designed to connect only from same VPC. So to connect nodejs application from an ec2 in same vpc. You need to have the pem file as by default SSL is enabled while db instance is created.

step-1 : $ wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem in required directory

step-2 : Change the mongoose connection with options pointing to pem file

mongoose.connect(database.url, {
    useNewUrlParser: true,
    ssl: true,
    sslValidate: false,
    sslCA: fs.readFileSync('./rds-combined-ca-bundle.pem')})
.then(() => console.log('Connection to DB successful'))
.catch((err) => console.error(err,'Error'));

Here am using mongoose 5.4.0

To connnect from outside the VPC, please try to follow the below doc from aws: https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html

Personally I tried only to connect from VPC and it worked fine.

Update =====:>

To connect from Robo 3T outside VPC please follow the link - AWS DocumentDB with Robo 3T (Robomongo)

like image 75
Arun Ramachandran Avatar answered Dec 05 '22 12:12

Arun Ramachandran