I would like to connect AWS DocumentDB locally to NodeJS.
We've tested with ec2 and Cloud9 and I connected to Studio 3t using ec2 ssh tunnel.
And I follow the example of programming connection https://docs.aws.amazon.com/documentdb/latest/developerguide/connect_programmatically.html There is a timeout error locally in NodeJS, so I am asking.
Even if you try to access using .pem file locally in NodeJS, a timeout error occurs and asks.
What is the reason and how can I improve it? I'm sharing the source below.
Thank you in advance
var MongoClient = require('mongodb').MongoClient;
var username = 'DBusername';
var password = 'DBpassword';
var client = MongoClient.connect(
`mongodb://<DBusername>:<DBpassword>@<clusterendpoint>:27017/<DBname>?tls=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false`,
{
tlsCAFile: `./rds-combined-ca-bundle.pem`,
},
function (err, client) {
if (err) throw err;
//Specify the database to be used
db = client.db('DBname');
//Specify the collection to be used
col = db.collection('CollectionName');
//Insert a single document
col.insertOne({ hello: 'Amazon DocumentDB' }, function (err, result) {
//Find the document that was previously written
col.findOne({ hello: 'DocDB;' }, function (err, result) {
//Print the result to the screen
console.log(result);
//Close the connection
client.close();
});
});
}
);
When you say "locally" do you mean trying to connect to DocumentDB from your laptop?
Refer to the instructions in this document - https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html
I'd like to access nodejs in the local environment
Should I access the db by accessing the ssh with the code?
Hello, I see that you are using replica set mode above. If you are using SSH tunneling since connection is local, please use the cluster endpoint only, and not replica set.
As per this document, "When using an SSH tunnel, we recommend that you connect to your cluster using the cluster endpoint and do not attempt to connect in replica set mode (i.e., specifying replicaSet=rs0 in your connection string) as it will result in an error."
Relevant questions
Unable to connect to EC2 instance using Public IP address with ssh
asked 2 years agoproblem of connecting new EC2 instance
asked 2 years agoFailure to connect to Cloud9 instance - never get past spinning icon
asked 5 years agoI am not able to ssh to EC2 instances
asked 2 months agoSSH timeout AWS EC2
asked 4 months agoI would like to connect AWS DocumentDB locally to NodeJS.
asked a month agoWhat is the difference between EC2 Instance Connect and Session Manager SSH connections?
Accepted Answerasked 3 years agoEB-Cli SSH Tunnel to RDS Database
asked 3 years agoConnect to RDS using SSM
Accepted Answerasked 2 years agoBest practice of using Keypair, in EC2
asked 5 months ago
Just to make sure, have you created a SSH tunnel from the machine that you are running NodeJS to an EC2 machine in same VPC as DocumentDB? If not, you will have to do this first