0

I can connect to documentDB if my node.js app is in ec2 instance based from this page: https://docs.aws.amazon.com/documentdb/latest/developerguide/connect_programmatically.html

But is it possible to connect to documentDb if I run my node.js app from my local pc?

Edit: This is some of my code with sample connection string. I use mongoose for the connection.

const connectionString = 'mongodb://user:password@sampledb.cluster-asdf.ap-northeast-1.docdb.amazonaws.com:27017/mydb?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false';

mongoose.Promise = bluebird;
mongoose.connect(connectionString, {
    useMongoClient: true,
    reconnectTries: 1000,
    autoReconnect: true,
    connectTimeoutMS: 99999,
  }
);
noyruto88
  • 767
  • 2
  • 18
  • 40
  • Could you share the code which you are running? It would give us an idea which could be a solution to your specific case. – Ashniu123 Sep 15 '20 at 05:29
  • 1
    I see that you have not provided the ssl files while connecting though it is mentioned that ssl is true. Can you confirm? – Ashniu123 Sep 15 '20 at 09:05

2 Answers2

6

@noyruto88, Amazon DocumentDB (with MongoDB compatibility) clusters are deployed within an Amazon Virtual Private Cloud (Amazon VPC). They can be accessed directly by Amazon EC2 instances, Cloud9 environments or other AWS services that are deployed in the same Amazon VPC. You can use this sample (https://github.com/aws-samples/amazon-documentdb-samples/blob/master/samples/connect-and-query/sample_nodejs_documentdb.js) if your client is in the same VPC as the DocumentDB cluster.

If you are looking to connect from your local pc, you can use SSH tunneling (also known as port forwarding) to connect to your Amazon DocumentDB cluster. Instructions on how to create a SSH tunnel can be found here - https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html

meet-bhagdev
  • 2,608
  • 18
  • 22