0

I am trying to connect to AWS Document DB (MongoDB) in rails using "mongoid" gem (7.5.1) which internally uses mongo gem (2.18)

dev:
  clients:
    default:
      database: my_database_name
      hosts:
        - docdb.dev.mysite.internal:27017
      options:
        read:
          mode: :primary
        auth_mech: :aws
        max_pool_size: 1

I followed the documentation for both mongoid and mongo gem and they have mandated to use auth_mechanism as "aws" when we want to provide authentication credentials using AWS IAM. With the above config/mongoid.yml , when I try to connect , getting below error

User  (mechanism: aws) is not authorized to access my_database_name (auth source: $external, used mechanism: MONGODB-AWS, used server: docdb.dev.mysite.internal:27017 (PRIMARY)): [73]: 
Invalid character $ in database name: $external (modern retry, attempt 1) (Mongo::Auth::Unauthorized)

Seems like when auth_mech is set to aws, mongo gem sets the auth_source as $external. Based on the documentation here https://www.mongodb.com/docs/ruby-driver/current/reference/authentication/#authentication-mechanisms , auth source is the database where users authentication credentials are stored.

Please suggest the way to make it work.

Vipul Kumar
  • 259
  • 1
  • 2
  • 12
  • I don't know if Mongoid will work with Document DB. After searching I did not find any examples of it working. You may want to contact AWS support to see if it works with Mongoid. Trying to get it to work with IAM appears to be more difficult, and a great question for support. – B Seven Jan 08 '23 at 21:49
  • Here is a related SO question: https://stackoverflow.com/questions/55232190/how-to-use-aws-document-db-with-ruby-on-rails-compatibility-with-version-3-6 . Also, I see that Document DB is "compatible" with Mongo 4.0, so you may need to specify a protocol version for the Mongo driver. https://docs.aws.amazon.com/documentdb/latest/developerguide/compatibility.html – B Seven Jan 08 '23 at 21:51

0 Answers0