AWS Document DB integration with spring data mongodb repository.
Choose the branch based on below versions.
Branch/Version | Spring Boot |
---|---|
master | 2.5.5 |
2.1.2 | 2.1.2.RELEASE |
Once AWS DocumentDB cluster is provisioned, update below property in application.properties file or pass it as an environment variable to point to cluster endpoint
spring.data.mongodb.uri=${mongo_uri}
To connect through SSL, set below environment variable pointing to location of the certificate.
java -DsslCertificate=<<PATH TO SSL CERTIFICATE>> -jar app.jar
This can be downloaded from SSL certs and copy it to base directory. Alternatively, you can provide absolute path to the variable sslCertificate
.
- Clone the repository & run maven build
./mvnw clean install -DskipTests
- Run using java command
java -DsslCertificate=./ssl/rds-combined-ca-bundle.pem -DSPRING_DATA_MONGO_URI=mongodb://admin:admin@mongodb.cluster-123456.us-east-1.docdb.amazonaws.com:27017/admin?ssl=true&ssl_ca_certs=rds-combined-ca-us-gov-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false -jar target/aws-documentdb-*.jar
Note: Do not forget to update mongo uri as per cluster endpoint generated.
Connecting to an Amazon DocumentDB Cluster from Outside an Amazon VPC By design, you access Amazon DocumentDB (with MongoDB compatibility) resources from an Amazon EC2 instance within the same Amazon VPC as the Amazon DocumentDB resources. However, suppose that your use case requires that you or your application access your Amazon DocumentDB resources from outside the cluster's Amazon VPC. In that case, you can use SSH tunneling (also known as "port forwarding") to access your Amazon DocumentDB resources.
Read More here: Outside VPC
CA bundle can be found here SSL CERTS
Download the bundles from AWS
wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
- Build Docker image
docker build -t aws-document-db .
- Run docker image
docker run --name demo -e SPRING_DATA_MONGODB_URI=<<insert-mongo-uri-here>> -e JAVA_OPTS="-DsslCertificate=./ssl/rds-combined-ca-bundle.pem" aws-document-db
-Cleanup the docker resources
docker stop demo && docker rm demo