MongoDB replica set

I am trying to set a MongoDB replica set.

The idea is, having 4 instances in aws: 1 – Nodejs app, a simple webpage, configured to connect to a DB_HOST on port 27017 2 – MongoDB Primary 3 – MongoDB replica 4 – MongoDB replica

My NodeJs app works without any problem if i access its IP in my browser, i can see the homepage, but if i try to put :27017/posts, it keeps loading and than gives the error.

This is how i configured the project with terraform and bash scripts, as i need npm to run the app.

mongodb conf:

# mongod.conf  # for documentation of all options, see: #  # Where and how to store data. storage:   dbPath: /var/lib/mongodb   journal:     enabled: true #  engine: #  mmapv1: #  wiredTiger:  # where to write logging data. systemLog:   destination: file   logAppend: true   path: /var/log/mongodb/mongod.log  # network interfaces net:   port: <%= @port %>   bindIp: <%= @bindIp %>   #processManagement:  #security:  #operationProfiling:  #replication: replication:   replSetName: rs0  #sharding:  ## Enterprise-Only Options:  #auditLog:  #snmp: 

def variables:

default['mongodb']['port'] = 27017 default['mongodb']['bindIp'] = ' 

this is the script to configure IP for mongodb primary and secondaries:

#!/bin/bash  sudo echo ' rs0' >> /etc/hosts  sudo systemctl enable mongod sudo systemctl start mongod  mongo mongodb:// --eval "rs.initiate( { _id : 'rs0', members: [{ _id: 0, host: '' }]})" mongo mongodb:// --eval "rs.add( '' )" mongo mongodb:// --eval "rs.add( '' )" mongo mongodb:// --eval "db.isMaster().primary" mongo mongodb:// --eval "rs.slaveOk()"   sleep 60; sudo systemctl restart metricbeat sudo systemctl restart filebeat  sleep 180; sudo filebeat setup -e \   -E output.logstash.enabled=false \   -E output.elasticsearch.hosts=[''] \   -E && sudo metricbeat setup 

As the nodejs app need a DB_HOST variable to start seeding the mongodb, i had to set this script.

#!/bin/bash sleep 120 cd /home/ubuntu/AppFolder/app  export DB_HOST=mongodb://,,   node /home/ubuntu/AppFolder/app/seeds/seed.js  sudo npm install sudo npm start &  sudo filebeat modules enable nginx 

Once i spin up my instances with terraform, and log inside the Database primary or any of them, and try a simple command as rs.status() nothing happens, the console waits for me to imput something else. and in the mongodb.log i got this REPL MESSAGE

2020-05-10T01:56:27.135+0000 I REPL     [initandlisten] Did not find local voted for document at startup. 2020-05-10T01:56:27.135+0000 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset 

i can see that my configuration has been reconized and it does show in the log rs0 but right after i get the above message. And this happens on all 3 mongodb instance. (Mongodb service is up and running).

On the other hand, if i ssh inside my APP instance, in the logs i get this

 DeprecationWarning: `openSet()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See  /home/ubuntu/AppFolder/app/node_modules/mongodb/lib/replset.js:365           process.nextTick(function() { throw err; })                                         ^ MongoError: no primary found in replicaset or invalid replica set name     at /home/ubuntu/AppFolder/app/node_modules/mongodb-core/lib/topologies/replset.js:560:28     at Server.<anonymous> (/home/ubuntu/AppFolder/app/node_modules/mongodb-core/lib/topologies/replset.js:312:24)     at Object.onceWrapper (events.js:315:30)     at emitOne (events.js:116:13)     at Server.emit (events.js:211:7)     at /home/ubuntu/AppFolder/app/node_modules/mongodb-core/lib/topologies/server.js:300:14     at /home/ubuntu/AppFolder/app/node_modules/mongodb-core/lib/connection/pool.js:469:18     at _combinedTickCallback (internal/process/next_tick.js:132:7)     at process._tickCallback (internal/process/next_tick.js:181:9) sudo: unable to resolve host ip-10-0-103-40 npm WARN lifecycle solution-code@1.0.0~postinstall: cannot run in wd solution-code@1.0.0 node seeds/seed.js (wd=/home/ubuntu/AppFolder/app) npm WARN solution-code@1.0.0 No repository field. 

Sorry for the long post i just wanted to be sure to share all the required info and thank you in advance for your help.