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 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则