connect to a documnetDB cluster on my vpc from nodeJS

Viewed 220

I am trying to connect to a documnetDB cluster on my vpc from my nodeJS Express project using ssh tunneling. and getting this error:

Mongoose connection disconnected

db.js:69
MongooseTimeoutError {message: "Server selection timed out after 15000 ms", name: "MongooseTimeoutError", reason: MongoNetworkError: getaddrinfo ENOTFOUND xxxxx-xxx…, stack: "MongooseTimeoutError: Server selection timed out a…", Symbol(mongoErrorContextSymbol): Object}
db.js:38
message:"Server selection timed out after 15000 ms"
name:"MongooseTimeoutError"
reason:MongoNetworkError: getaddrinfo ENOTFOUND xxxx-xxxx-xx-xx-xx-xx-xxx.xxxxxxx.us-east-2.docdb.amazonaws.com xxxx-xxxx-xx-xx-xx-xx-xxx.xxxxxxx.us-east-2.docdb.amazonaws.com 
stack:"MongooseTimeoutError: Server selection timed out after 15000 ms\n    at new MongooseTimeoutError 

Before the code a few things: I am using a package called ssh-tunnel I disabled TLS for simplicity I succeed to connect via MongoDB compass with exactly the same params I got here

I am woried about the fact that in the error the dstHost i am trying to connect to missing some of the address I provided in the ssh-tunnel configuration for example instead of xxxx-xxxx-xx-xx-xx-xx-xxx.cluster-xxxxxxx.us-east-2.docdb.amazonaws.com we get xxxx-xxxx-xx-xx-xx-xx-xxx.xxxxxxx.us-east-2.docdb.amazonaws.com Here are the code lines:

const config = require('./config');
const mongoose = require('mongoose'); 
const fs = require('fs');
const tunnel = require('tunnel-ssh');


const tunnelConfig = {
  username:'xxx-xxxx',
  // agent: process.env.SSH_AUTH_SOCK,
  host:'xxx-x-xx-xx-x.us-east-2.compute.amazonaws.com',
  privateKey: fs.readFileSync('./documentDBKeyPrivate.pem'),
  port:22,
  dstHost:'xxxxx-xxxx-xx-xx-xx-xx-xx.xxxxx-xxxxxxxx.us-east-2.docdb.amazonaws.com',
  dstPort:27017,
  localHost:'localhost',
  localPort: 27018
};
let dbURI;

if(process.env.NODE_ENV === 'DEV') {
  dbURI = config.DB_CONN_STRING_DEV;

  const sshTunnel = tunnel(tunnelConfig, (error, tnl) => {
    if (error) {
      console.log("SSH connection error: " + error);
  }
  // Create the database connection 
  mongoose.connect(dbURI, 
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      serverSelectionTimeoutMS: 15000,
      poolSize: 10,
    }).catch(err => console.log(err)); 
  }) 
0 Answers
Related