Error connecting web server on Elastic Beanstalk with database server on MongoDB Atlas

Viewed 362

I am trying to connect my web servers on Elastic Beanstalk with my database servers on MongoDB Atlas, but somehow I get the following error

Listening at http://:::3001
Unable to connect to the mongodb instance. Error:  { MongoNetworkError: failed to connect to server [<server>.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to <server>.mongodb.net:27017 closed]
    at Pool.<anonymous> (/var/app/current/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:198:13)
    at connect (/var/app/current/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at callback (/var/app/current/node_modules/mongodb-core/lib/connection/connect.js:109:5)
    at runCommand (/var/app/current/node_modules/mongodb-core/lib/connection/connect.js:129:7)
    at Connection.errorHandler (/var/app/current/node_modules/mongodb-core/lib/connection/connect.js:321:5)
    at Object.onceWrapper (events.js:286:20)
    at Connection.emit (events.js:198:13)
    at TLSSocket.<anonymous> (/var/app/current/node_modules/mongodb-core/lib/connection/connection.js:350:12)
    at Object.onceWrapper (events.js:286:20)
    at TLSSocket.emit (events.js:198:13)
    at _handle.close (net.js:606:12)
    at TCP.done (_tls_wrap.js:388:7)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }
Listening at http://:::3001
(node:11482) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Listening at http://:::3001

I am using mongoose as follows

mongoose.connect('mongodb+srv://<UserName>:<Password>@<cluster>.mongodb.net/test?retryWrites=true&w=majority',
{ useNewUrlParser: true, useUnifiedTopology: true })
.catch(function (reason) {
    console.log('Unable to connect to the mongodb instance. Error: ', reason);
});

The problem is not in the IP Whitelist on MongoDB Atlas, since I already ssh'd into the server on elastic beanstalk and from there connected to the Mongo Servers on Atlas directly. The problem is in the code, although I can't seem to see it.

1 Answers

I had the same issue. Add 0.0.0.0/0 to your IP whitelist.

enter image description here

Related