This application is a Java application that uses the MongoDB Java Driver and AWS SDK for Java to interact with MongoDB Atlas. It assumes an AWS IAM role and fetches documents from a MongoDB collection in a loop, sleeping for 60 seconds between each iteration.
- Java 8 or later
- MongoDB Atlas account
- AWS account with IAM role setup
- Clone this repository to your local machine.
- Open the project in your preferred IDE.
- Update the MongoDB Atlas connection details and AWS IAM role details in the
main
method of theApp
class.
Run the application with the following command:
mvn exec:java -Dexec.mainClass=com.example.App -Dexec.args="<region> <roleArn> <roleSessionName>"
Where:
<region>
is the Amazon region (for example,us-east-1
).<roleArn>
is the Amazon Resource Name (ARN) of the role to assume (for example,arn:aws:iam::000008047983:role/s3role
).<roleSessionName>
is an identifier for the assumed role session (for example,mysession
).
The application will connect to the specified MongoDB Atlas cluster, assume the specified AWS IAM role, and fetch documents from the movies
collection in the sample_mflix
database. It will continue to fetch documents every 60 seconds until the application is stopped.
The App
class is the entry point of the application. It takes command-line arguments for the AWS region, role ARN, and role session name. It then creates an AWS STS client, assumes the specified role, and uses the returned credentials to create a MongoDB client. The application then enters a loop where it fetches a document from the movies
collection and prints it to the console. The application sleeps for 60 seconds between each iteration.
The MongoAwsCredentialSupplier
and CredentialsSupplier
classes are helper classes used to refresh AWS credentials when they expire.
Contributions are welcome! Please ensure any PRs include documentation, tests, and pass checks.
This project is licensed under the MIT License - see the LICENSE.md file for details.