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.

Joseph Idziorek
  • 4,853
  • 6
  • 23
  • 37
Gordon Truslove
  • 724
  • 2
  • 10
  • 19
  • 1
    Have you set your security group for your DocumentDB database to allow connections from wherever this code is running? – hephalump Apr 25 '19 at 12:38
  • Excellent question. hmmm... let me go and see if I can figure out how to do that. – Gordon Truslove Apr 25 '19 at 14:40
  • It worked. If you'd like to add that as an answer, then I'll tick it. – Gordon Truslove Apr 25 '19 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](https://docs.aws.amazon.com/documentdb/latest/developerguide/getting-started.launch-cluster.html) which talks about setting up the rules for security groups. – sayantancs May 28 '20 at 00:07

3 Answers3

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.

hephalump
  • 5,860
  • 1
  • 22
  • 23
  • Good answer. For more troubleshooting scenarios, also please see: https://docs.aws.amazon.com/documentdb/latest/developerguide/troubleshooting.html – Joseph Idziorek Apr 25 '19 at 15:48
  • Also, here is a quick video on how to get started: https://www.youtube.com/watch?v=qk98rR08szU – Joseph Idziorek Apr 25 '19 at 17:15
  • 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' – philosopher Dec 05 '20 at 13:05
  • 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. – Trapper Davis Jan 09 '22 at 19:59
1

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

Mike
  • 11
  • 1
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

Alok
  • 7,734
  • 8
  • 55
  • 100