Connect to MongoDB on aws vpc through Node JS

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Connect to MongoDB on aws vpc through Node JS



I set up a mongo db with a ssh tunnel that was deployed on a new vpc (Amazon Linux), I followed the instruction here:



https://docs.aws.amazon.com/quickstart/latest/mongodb/step2.html



I'm able to connect to my db through PuTTY, Mongo Compass, and Robo3T, but I can't find out a way to connect to my db through Node JS. I tried to use 'tunnel-ssh' package, but I can't still connect to my db. I got this error :


AssertionError [ERR_ASSERTION]: null == { MongoNetworkError: failed to connect to server [PrimaryReplicaNode0-PrivateIPs:27017] on first connect [MongoNetworkError: connect ETIMEDOUT 10...



Here is my code:


import MongoClient, ObjectID from 'mongodb';
import format from 'util';
import tunnel from 'tunnel-ssh';
import assert from 'assert';

const user = encodeURIComponent('username');
const password = encodeURIComponent('password');
const authMechanism = 'SCRAM-SHA-1';
const url = format('mongodb://%s:%s@PrimaryReplicaNode0-PrivateIPs:27017/?authMechanism=%s&authSource=admin&ssl=false', user, password, authMechanism);

const sshConfig =
usesrname: 'ec2-user',
host: 'LinuxBastion-PublicDNS(IPv4)',
port: 22,
dstHost: 'PrimaryReplicaNode0-PrivateIPs',
dstPort: 27017,
privateKey:require('fs').readFileSync('ssh/key-pair.pem'),
localHost:'127.0.0.1',
localPort: 3000
;
var tnl = tunnel (sshConfig, (error, server) =>
if (error)
console.log("SSH connection error: " + error);


MongoClient.connect(url, useNewUrlParser: true , (err, client) =>
assert.equal(null, err);
console.log("Connected correctly to server!");
tnl.close();




I googled for a whole week, but everything's still negative, so anyone has experiences on this one, please help me out a bit.



Thank you.





Never mind, mistyping only
– Justin
Aug 11 at 7:43









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard