- Newest
- Most votes
- Most comments
The issue you're experiencing with mongosh (MongoDB Shell version 2.3.4) not working for IAM authentication while the older mongo shell (version 5.0.30) works is likely due to differences in how these versions handle the MONGODB-AWS authentication mechanism.
To resolve this issue and use IAM authentication with the latest mongosh, you need to make a few adjustments:
-
Ensure you have the latest version of mongosh installed.
-
When connecting, you need to explicitly specify the authentication mechanism and source. Modify your connection string to include the following parameters:
- authMechanism=MONGODB-AWS
- authSource=$external
-
Make sure you have the AWS CLI configured with the correct credentials or that you're running in an environment with the appropriate IAM role attached (e.g., EC2 instance with an IAM role).
-
Your connection string should look something like this:
mongosh "mongodb://<your-docdb-endpoint>:27017/?authMechanism=MONGODB-AWS&authSource=$external"
-
If you're passing credentials explicitly (not recommended for production use), the format would be:
mongosh "mongodb://<ACCESS_KEY>:<SECRET_KEY>@<your-docdb-endpoint>:27017/?authMechanism=MONGODB-AWS&authSource=$external"
-
Remember to use TLS/SSL when connecting to Amazon DocumentDB. You may need to include the
tls=trueandtlsCAFileparameters in your connection string, pointing to the Amazon DocumentDB certificate file.
If you're still encountering issues after making these changes, double-check your IAM permissions and ensure that your IAM user or role has the necessary permissions to access the DocumentDB cluster.
Also, verify that your Amazon DocumentDB cluster is running version 5.0 or later, as IAM authentication is only supported in these versions.
By following these steps, you should be able to successfully authenticate to Amazon DocumentDB using IAM with the latest mongosh version.
Sources
Authentication using IAM identity - Amazon DocumentDB
Use IAM authentication with Amazon DocumentDB (with MongoDB compatibility) | AWS Database Blog
Amazon DocumentDB announces IAM database authentication - AWS
Relevant content
- asked 2 years ago
- asked a year ago
- asked 8 months ago
- asked a month ago
- AWS OFFICIALUpdated 8 days ago

Here are the 2 command strings I used:
#Authentication failure mongosh 'mongodb://<ACCESS_KEY>:<SECRET_KEY>@<your-docdb-endpoint>:27017/allowed_db_1?authSource=%24external&authMechanism=MONGODB-AWS' --tlsCAFile global-bundle.pem --tls
#Authentication Success mongo 'mongodb://<ACCESS_KEY>:<SECRET_KEY>@<your-docdb-endpoint>:27017/allowed_db_1?authSource=%24external&authMechanism=MONGODB-AWS' --tlsCAFile global-bundle.pem --tls
The same holds true for Role based authentication.