3

How do I connect to Amazon's new DocumentBD database from node.js (In this case, using an out-of-the-box Elastic Beanstalk setup.)

This is my code, copied from the docs (with some values altered for privacy). It just times out and the eventual value of 'err' is 'true'. What did I miss? How can I get a better error?

var MongoClient = require('mongodb').MongoClient,fs = require('fs');
var ca = [fs.readFileSync("rds-combined-ca-bundle.pem")];
var connection_string = "mongodb://USERNAME:PASSWORD@docdb-2019-04-23-12-55-44.cluster-abcdefghij.eu-west-1.docdb.amazonaws.com:27017/?ssl=true&&replicaSet=rs0&readPreference=secondaryPreferred";

MongoClient.connect(
        connection_string, {
            sslValidate: true,
            sslCA: ca,
            useNewUrlParser: true
        },
        function (err,client) {
            console.log(err+" , "+ client);
        });

Here's hoping somebody knows.

4
  • 1
    Have you set your security group for your DocumentDB database to allow connections from wherever this code is running? Commented Apr 25, 2019 at 12:38
  • Excellent question. hmmm... let me go and see if I can figure out how to do that. Commented Apr 25, 2019 at 14:40
  • It worked. If you'd like to add that as an answer, then I'll tick it. Commented Apr 25, 2019 at 14:51
  • Great suggestion by hephalump@. Wanted to point you to the exact documentation. You can refer to point number 7.d of this documentation which talks about setting up the rules for security groups. Commented May 28, 2020 at 0:07

3 Answers 3

5

A timeout is often an indication that security groups are not properly configured. Check your DocumentDB inbound security group configuration to ensure that traffic from the source is permitted to your DocumentDB instance.

Sign up to request clarification or add additional context in comments.

4 Comments

Good answer. For more troubleshooting scenarios, also please see: docs.aws.amazon.com/documentdb/latest/developerguide/…
Also, here is a quick video on how to get started: youtube.com/watch?v=qk98rR08szU
Hey, thanks for the answer. Being a networking noob that I am, can you ELI5 what rule I specifically need to add to allow my local NodeJS app to connect to DynamoDB? Currently, I have Inbound Rules - 'All TCP'
It needs to also be know that DocumentDB by default is in a VPC. You cannot connect your locally running application to the DB. You need to create an EC2 instance, SSL into that instance and then install the mongo shell. You can then use the CLI to communicate with you DB.
1

Removing the cluster- from the URL seems to work for me.

Comments

0

In the mongo uri we need to repalce ssl to tls and add tlsCAFile in connection options

var MongoClient = require('mongodb').MongoClient;
var connection_string = "mongodb://USERNAME:PASSWORD@docdb-2019-04-23-12-55-44.cluster-abcdefghij.eu-west-1.docdb.amazonaws.com:27017/?tls=true&&replicaSet=rs0&readPreference=secondaryPreferred";

MongoClient.connect(
        connection_string, {
            tlsCAFile: `rds-combined-ca-bundle.pem`
        },
        function (err,client) {
            console.log(err+" , "+ client);
        });

Referance: https://docs.aws.amazon.com/documentdb/latest/developerguide/connect_programmatically.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.