Search code examples
typescriptamazon-web-servicestypeormaws-documentdb

Trying to connect programmatically to AWS DocumentDB in the same vpc but connection fails


I’m trying to connect on my Docker application using TypeORM, which is running on EC2, to DocumentDB cluster. Both EC2 and DocumentDB are running on the same VPC.

However, it throws MongoServerSelectionError.

Here is my configuration:

{
  uri: 'mongodb://(username):(password)@~~.docdb.amazonaws.com/(database)',
  sslValidate: true,
  sslCA: [readFileSync(path.resolve('/path/to/ca'))],
}

Here is detailed error message:

{"name":"MongoServerSelectionError","reason":{"type":"Unknown","setName":null,"maxSetVersion":null,"maxElectionId":null,"servers":
{},"stale":false,"compatible":true,"compatibilityError":null,"logicalSessionTimeoutMinutes":null,"heartbeatFrequencyMS":10000,"localThresholdMS":15,"c
ommonWireVersion":null}}
MongoServerSelectionError: connection <monitor> to (IP address) timed out

The EC2 instance is created by Elastic Beanstalk service.

Edit: I checked works fine when using mongo cli.


Solution

  • I fixed it. It was just a simple TypeORM configuration error.

    This solved my issue:

    {
      url: 'mongodb://~~.docdb.amazonaws.com',
      username: '(username)',
      password: '(password)',
      database: '(database)',
      sslValidate: true,
      sslCA: [readFileSync(path.resolve('/path/to/ca'))],
    }