I would like to connect AWS DocumentDB locally to NodeJS.

0

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();
      });
    });
  }
);
  • 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

已提問 2 年前檢視次數 3550 次
2 個答案
0

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

profile pictureAWS
專家
已回答 2 年前
  • I'd like to access nodejs in the local environment

    Should I access the db by accessing the ssh with the code?

0

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."

AWS
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南