By using AWS re:Post, you agree to the Terms of Use
/I would like to connect AWS DocumentDB locally to NodeJS./

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 Answers
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

answered a month ago
  • 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."

answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions