title: Error connecting web server on Elastic Beanstalk with database server on MongoDB Atlas
body: <p>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 </p>
  
  <pre><code>Listening at http://:::3001
  Unable to connect to the mongodb instance. Error:  { MongoNetworkError: failed to connect to server [&lt;server&gt;.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to &lt;server&gt;.mongodb.net:27017 closed]
      at Pool.&lt;anonymous&gt; (/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.&lt;anonymous&gt; (/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
  </code></pre>
  
  <p>I am using mongoose as follows </p>
  
  <pre><code>mongoose.connect('mongodb+srv://&lt;UserName&gt;:&lt;Password&gt;@&lt;cluster&gt;.mongodb.net/test?retryWrites=true&amp;w=majority',
  { useNewUrlParser: true, useUnifiedTopology: true })
  .catch(function (reason) {
      console.log('Unable to connect to the mongodb instance. Error: ', reason);
  });
  </code></pre>
  
  <p>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. </p>