1

I have created an ecs fargate task and there are two containers(node.js app container and mongodb container) in it. The problem is when I create service from that task definition,Node.js app can't connect to mongodb container with username and password.It said AuthenticationFailed with code:18.The service runs in awsvpc network mode.

I have configure my mongodb container with env variables like this.

MONGO_INITDB_ROOT_USERNAME=username
MONGO_INITDB_ROOT_PASSWORD=password
MONGO_INITDB_DATABASE=my-db

And in node.js app container,

MONGODB_USERNAME = username
MONGODB_PASSWORD = password
MONGODB_URL = localhost

I use localhost for MONGODB_URL because AWS Docs says when you run your fargate task in awsvpc network mode,Containers that belong to the same task can communicate over the localhost interface.

and the connection string in node.js code is like this.

mongoose.connect(`mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@${process.env.MONGODB_URL}:27017/my-db?authSource=admin`,
  {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  },
  (err) => {
    if (err) {
      console.error('FAILED TO CONNECT TO MONGODB');
      console.error(err);
    } else {
      console.log('CONNECTED TO MONGODB!!');
      app.listen(80);
    }
  }
);

But the node.js app container can't connect to mongodb,It always says authentication failed and exit.Please can you help me what I'm missing.

Here is the logs from node.js container.

FAILED TO CONNECT TO MONGODB
mongodb://username:password@localhost:27017/my-db?authSource=admin
MongoError: Authentication failed.
    at MessageStream.messageHandler (/app/node_modules/mongodb/lib/cmap/connection.js:299:20)
    at MessageStream.emit (node:events:512:28)
    at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (node:internal/streams/readable:766:22)
    at Socket.emit (node:events:512:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed'
}
  • 1
    use plain text instead links – dododo Mar 01 '23 at 10:43
  • `AuthenticationFailed` would indicate to me that there is something wrong with the username or password, not a network issue. – Mark B Mar 01 '23 at 13:25
  • you should specify authMechanism, by default it's not aws. Also, you may need to convert (escape) credentials you provide as userName or password. See this question for details https://stackoverflow.com/questions/75591618/connect-mongodb-from-aws-lambda-python-using-iam/75592915#75592915 – dododo Mar 01 '23 at 14:56
  • Maybe this one: https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Mar 01 '23 at 15:16
  • there is no special characters in username and password,they are just simple string.I can connect to the mongodb from my machine's terminal using mongo --host ecs_service_ip:27017 -u username -p password --authenticationDatabase admin.The problem is node.js container in that service can't connect to the mongodb container. – Thantzin Soe Mar 02 '23 at 09:34
  • Check the mongod log. 'AuthenticationFailed' should be indicating that the TCP connection succeeded but the mongod didn't accept the credentials. If that is the case, the log will have more detail. – Joe Mar 03 '23 at 04:00

0 Answers0