Vishnu S
@vishnus17

@vishnus17

Secure MongoDB with AWS IAM passwordless authentication example

Photo by Rubaitul Azad on Unsplash

Secure MongoDB with AWS IAM passwordless authentication example

Vishnu S's photo
Vishnu S
·Aug 15, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Configure AWS environment
  • Configure MongoDB Atlas

In this article, we will walk through on how to setup MongoDB AWS IAM passwordless authentication mechanism to connect to your MongoDB Atlas cluster.

MongoDB introduced the MONGODB-AWS authentication mechanism for MongoDB version 4.4 and above. It uses your Amazon Web Services IAM (Identity and Access Management) credentials to authenticate your user.

The connection string for MongoDB-AWS mechanism is generated with a security token for which you can define the time to live. This way you can ensure that the access is limited and not taken advantage of for anything else.

image_2022-08-15_005913325.png When you pass a connection string for MongoDB-AWS, the driver will seamlessly use the temporary credentials for you. It is also designed for the most sensitive security situations. The secret key is never directly passed to the MongoDB Atlas clusters and it’s never persisted by the driver.

Configure AWS environment

  1. Set up a VPC in AWS so that you can launch an instance in it and use it to connect to the MongoDB Atlas cluster.
  2. Next, launch an instance in the same VPC and install MongoDB client.
  3. Create and attach an IAM role to this instance so that you can use the role to configure MongoDB-AWS IAM authentication and connect to the database from the instance. image_2022-08-15_150142816.png
  4. Copy the ARN of the role you created so that we can start setting up MongoDB.

Configure MongoDB Atlas

  1. In the MongoDB Atlas dashboard, go to Database Access under Security and choose Add New Database User. image_2022-08-15_014402416.png
  2. Choose AWS IAM, select the AWS IAM type as IAM Role, paste the ARN you copied in Step 4 in the associated field and click Add user. image_2022-08-15_151304236.png
  3. Now let's try connecting to our MongoDB.
    Go to the Database section and click on Connect. image_2022-08-15_014952028.png
  4. If you choose to connect your application to your cluster using MongoDB’s native drivers, you will be able to see a connection string format and notice that it has an AWS_SESSION_TOKEN in the end. This is a part of the temporary credentials that will be required when you try connecting to MongoDB.
    Read the discussion here for more info. image_2022-08-15_015355268.png
  5. Since you have an EC2 role assigned to the instance, you don't need to pass the temporary credentials manually. It'll be done automatically when you connect to the MongoDB cluster using the Mongo client.
    mongosh "mongodb+srv://<your_db_url>"
    
    SSH into your instance and simply run the above command after replacing db_url with appropriate value to connect to your Atlas cluster. image_2022-08-15_153300078.png
  6. If you want to connect to your Atlas cluster from elsewhere, make sure you have the required permission to generate a session token and then run:
    aws sts get-session-token --duration-seconds 900
    
    This will generate temporary credentials which you can use for a fixed time (Here, 900 seconds) to connect to your cluster. Make sure to change the IAM Role ARN appropriately before you run the command(Step 2).
  7. After you generate the credentials, you can replace the values
    <your_db_url>, <AWS access key>, <AWS secret key>, <session token (for AWS IAM Roles)> with the values from the previous step.
    mongosh "mongodb+srv://<your_db_url>/?authSource=%24external&authMechanism=MONGODB-AWS" --apiVersion 1 --username <AWS access key> --password <AWS secret key> --awsIamSessionToken <session token (for AWS IAM Roles)>
    

Great job! Now you will be able to access your MongoDB Atlas cluster using AWS IAM passwordless mechanism.

References:
MongoDB YouTube
Blog by Alfred J. Stanley.
MongoDB documentation

See you in the next article.
Vishnu S.

Did you find this article valuable?

Support Vishnu S by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this