This document provides instructions on how to install and set up Java (JDK 1.8) and an IDE (IntelliJ IDEA) on your computer to be able compile the project source code and run DocumentDB tests.
- Instructions
- Troubleshooting
- Go to this page to download Java SE Development Kit 8 (JDK 1.8) for your system and follow installation steps (you need to have or create an Oracle account to download).
- Confirm correct version (1.8) of Java has been installed by running the command
java -versionorjavac -versionin the command line or terminal.
- Go to this page to download IntelliJ IDEA Community Edition for your system and follow the installation steps (click next and continue with default settings)
- The plugins SpotBugs and Gradle need to be installed in the IDE which can be done via the top menu toolbar under IntelliJ IDEA → Preferences → Plugins (search in the Marketplace).
-
If you do not have an SSH key associated with your GitHub account, follow the steps on this page to add a new SSH key to your GitHub account.
-
In the Amazon documentdb-jdbc repository, copy the SSH URL to your clipboard when you click the download code button as shown in the image below.
-
Open the command line or terminal and navigate to the folder/directory where you would like to clone the repository to and run the following command
git clone XwhereXis the SSH URL to the repository copied to your clipboard in the above step.
-
In IntelliJ, open Gradle tool window: navigate to the top menu toolbar View → Tool Windows → Gradle.
-
In the Gradle tool window, build the project by double-clicking on gradle task documentdb-jdbc → Tasks → shadow → shadowJar.
-
After build and compilation, a .jar file should be created in your repository in the subfolder documentdb-jdbc/build/libs.
This section describes how to connect to a DocumentDB cluster using an SSH tunnel and details the setup needed to run tests against the DocumentDB instance.
Since DocumentDB is currently a VPC-only service, you need to set up an SSH tunnel using an EC2 instance running in the same VPC as your DocumentDB cluster (Note that SSH tunnelling will not work for connecting in replica set mode).
-
Mongo shell (comes with MongoDB server installation but can be downloaded separately)
-
Download Mongo shell on macOS using HomeBrew by running following commands in the terminal:
brew tap mongodb/brew brew install mongodb-community-shell
-
-
AWS account (or a downloaded private key granting access to the cluster)
- Walkthrough on creating an EC2 instance and SSH-ing into it
- Walkthrough on creating a documentDB cluster with SSH tunnel
If you already have an instance of an Amazon EC2 running in the same VPC as your DocumentDB cluster, you can skip these steps as long as you have the PEM file for the instance's key pair.
-
Navigate to AWS EC2 Dashboard and select Launch an Instance.
-
Select the Amazon Linux 2 AMI (HVM), SSD Volume Type as the machine image and t2.micro as the instance type. Leave everything as default and launch the instance.
- NOTE: This will create the instance in your default VPC. You can select a different VPC if necessary but should remember to create your DocumentDB cluster there as well.
-
You will be prompted to provide or generate a key pair for the instance. Create a new key pair with a meaningful name and download it. Make sure to keep track of this file.
-
Give the instance a meaningful name and wait for the instance to have a state of running.
-
Move the key pair file (a PEM file) into your ssh directory.
mv downloads/<key-pair-name>.pem ~/.ssh/ -
Modify its permissions.
chmod 400 ~/.ssh/<key-pair-name>.pem -
SSH into the instance to verify the setup!
ssh -i ~/.ssh/<key-pair-name>.pem <ec2-username>@<public-IPv4-DNS-name>Where is the EC2 username chosen by you when creating a EC2 instance. You can find the public dns name of your instance from the EC2 dashboard. Select your instance and copy the public IPv4 DNS from the instance memory.
Example output:
-
Navigate to the DocumentDB dashboard. From the sidebar, select Parameter Groups and click Create.
-
Flip the TLS and TTL Monitor switches to disabled. You can leave the TLS switch as enabled if you want to connect with TLS right away.
-
From the sidebar, select Clusters and click Create.
-
Give the instance a meaningful identifier and choose t3.medium as the instance class and 1 as the number of instances.
-
Fill in the authentication section and take note of the master username and password entered. You will use these to connect later.
-
Under Advanced Settings:
-
Create the cluster and wait for it to finish spinning up.
-
Once the cluster is available, we can create the SSH tunnel using our EC2 instance. Use the same key pair file and public DNS name used to SSH into the EC2 instance. Use the cluster endpoint from the configuration tab of your cluster.
ssh -i ~/.ssh/<key-pair-name>.pem -N -L 27017:<cluster-endpoint>:27017 <ec2-username>@<public-IPv4-DNS-name> -
Open another terminal window and connect to the cluster using the
mongoshell. Note that we will use localhost rather than the cluster endpoint since we have set up the SSH tunnel.mongo --host 127.0.0.1:27017 --username <master-username> --password <master-password>
When connecting to a TLS-enabled cluster you can follow the same steps to set up an SSH tunnel but will need to also download the Amazon DocumentDB Certificate Authority (CA) file before trying to connect.
-
Download the CA file.
wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem -
Set up the SSH tunnel. See step 3 in section Setting Up Environment Variables for command to start SSH port-forwarding tunnel.
-
Open another terminal window and connect using the
mongoshell. Note that since we are using an SSH tunnel to access the cluster from localhost, the server certificate does not match the hostname. It is expecting a hostname likesample-cluster.xxxxxxxxxxxx.us-east-1.docdb.amazonaws.com:27017not127.0.0.1:27017. We have to pass the--tlsAllowInvalidHostnamesflag or the handshake will fail.mongo --host 127.0.0.1:27017 --username <master-username> --password <master-password> --tls --tlsCAFile rds-combined-ca-bundle.pem --tlsAllowInvalidHostnames
Connecting without TLS is very straightforward. We essentially follow the same steps as when connecting using the
mongo shell.
-
Setup the SSH tunnel. See step 3 in section Setting Up Environment Variables for command to start SSH port-forwarding tunnel.
-
Create a test or simple main to run.
-
Use either the Driver Manager, Data Source class or Connection class to establish a connection to
localhost:27017. Make sure to set the hostname, username, password and target database. The target database can be anything as long as it is a valid MongoDB database name (no spaces, no special chars). See example directly using the connection class:@Test public void sshTunnelTestWithoutSSL() throws SQLException { final DocumentDbConnectionProperties properties = new DocumentDbConnectionProperties(); properties.setHostname("localhost:27017"); properties.setUser("<user name>"); properties.setPassword("<password>"); properties.setDatabase("<target database name>"); final DocumentDbConnection connection = new DocumentDbConnection(properties); connection.close(); }
Connecting with TLS programmatically is slightly different from how we did it with the mongo shell.
-
Create a test or simple main to run.
-
Use either the Driver Manager, Data Source class or Connection class to establish a connection to
localhost:27017. Make sure to set hostname, username, password, target database and tls parameters. The target database can be anything as long as it is a valid MongoDB database name (no spaces, no special chars). See example directly using the connection class:@Test public void sshTunnelTestWithSSL() throws SQLException { final DocumentDbConnectionProperties properties = new DocumentDbConnectionProperties(); properties.setHostname("localhost:27017"); properties.setTlsEnabled("true"); properties.setTlsAllowInvalidHostnames("true"); properties.setUser("<user name>"); properties.setPassword("<password>"); properties.setDatabase("<target database name>"); final DocumentDbConnection connection = new DocumentDbConnection(properties); connection.close(); }
-
Create and set the Environment Variables:
DOC_DB_USER_NAME=<secret-username> DOC_DB_PASSWORD=<secret-password> DOC_DB_LOCAL_PORT=27019 DOC_DB_USER=<ec2-username>@<public-IPv4-DNS-name> DOC_DB_HOST=<cluster-endpoint> DOC_DB_PRIV_KEY_FILE=~/.ssh/<key-pair-name>.pem -
Ensure the private key file .pem is in the location set by the environment variable
DOC_DB_PRIV_KEY_FILE. -
Start an SSH port-forwarding tunnel:
ssh [-f] -N -i ~/.ssh/<key-pair-name>.pem -L $DOC_DB_LOCAL_PORT:$DOC_DB_HOST:27017 $DOC_DB_USER- The
-Lflag defines the port forwarded to the remote host and remote port. Adding the-Nflag means do not execute a remote command, you will not get a shell in this case. The-fswitch instructs SSH to run in the background.
- The
- Modify the /gradle.properties file in the source code and uncomment the following line:
runRemoteIntegrationTests=false
-
Confirm project SDK is Java Version 1.8 via the IntelliJ top menu toolbar under File → Project Structure → Platform Settings -> SDK and reload the JDK home path by browsing to the path and click apply and ok. Restart IntelliJ IDEA.
-
If above step does not solve the issue, try to see if reinstalling JDK 1.8 fixes the problem.
-
For Mac OS/Linux, uninstall JDK using commands below, restart your computer and install JDK 1.8 as describe in the Instructions section above.
sudo rm -rf /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin sudo rm -rf /Library/Java/JavaVirtualMachines sudo rm -rf /Library/Application\ Support/Oracle/Java sudo rm -rf /Library/PreferencePanes/JavaControlPanel.prefPane
-
-
DocumentDB instance and EC2 should be deployed in the same region and use the same VPC. Check Region and VPC.
-
Check Security Policy for EC2 instance allows inbound connections and ensure it is configured as shown in the picture below. Go to EC2 Dashboard → Network & Security Group in the left menu → Security Group.









