Amazon Document DB is with MongoDB Compatibility.

MongoDB is an open-source, nonrelational database that provides support for JSON-like, document-oriented storage systems.

This guide helps us to create and use Document DB in AWS

What is Document DB?

A document database is a type of nonrelational database that is designed to store and query data as JSON-like documents.

Document databases make it easier for developers to store and query data in a database by using the same document-model format they use in their application code.

We divide this section into 4 Parts:

  1. Create Document DB cluster.
  2. Setup SSH Tunnel using AWS EC2 Instance.
  3. Create EC2 Instance.
  4. Connecting to Document DB.

Let’s create a Cluster:

Open AWS Document DB Console.
DOCUMENT DB CONSOLE

aws document db

Choose: Launch Amazon DocumentDB

Before Creating a cluster, First we need to create a parameter group.

Choose: Parameter Groups

aws document db

Click Create

Fill in the details and create.

aws document db
aws document db

Choose the parameter group you have created.

Now, We need to turn off some additional security options from parameter group.

TLS & TTL_MONITOR should be disabled.

aws document db

Now its time to create your first DB cluster.

Choose Clusters and select Create

We have to choose the Instance class and instance count as per the requirement,

aws document db

Now configure master/Admin username and Password for the DB Cluster.

aws document db

Fill in the required details and then click Show Advanced Settings, Now we need to associated the parameter group we have created earlier.

You can configure Network setting if required, For now we will use the default VPC and subnets.

aws document db

testcluster is the DB parameter group which i have created.I will attach it to my cluster.

Now hit Create Cluster.

aws document db
aws document db

We have successfully created our first Document DB cluster.Now we should be able to connect to the new cluster.

Connecting Cluster:

To connect to DB cluster we need credentials and endpoints of the DB cluster.

If you click the cluster , You can find the details of the cluster created.

aws document db

Now, Let’s try to connect using the above connection strings.

aws document db

It says that we cannot connect to cluster, This is because Document DB doesn’t support public endpoints to connect to our cluster from localhost.

To fix this issue we need to setup SSH Tunnel.So Let’s setup a ssh tunnel and then we will try to connect to the cluster.

SSH Tunnel:

For this we need an EC2 Instance.I have created a seperate article on how to create an EC2 instance using AWS management Console.

Create EC2 instance from Console

Create SSH tunnel Connection:

Now we have SSH tunnel setup in place.Lets hop into using that tunnel to connect to our newly created DB cluster.

Here we are going to use the key pair we have created while launching EC2 Instance.

ssh -i testinstance1.pem -4 -N -L 27107:testcluster.cluster-cqenbvyhffql.ap-southeast-1.docdb.amazonaws.com:27017 [email protected]

Open new terminal and connect to Document DB:

mongo --host 127.0.0.1:27017 --username dbusername --password dbpassword
aws document db

Now that we have successfully logged into the Document DB Cluster.

Let’s run some MongoDB commands:

Let’s Create a database and a Collection (table):

rs0:PRIMARY> use testdb;
switched to db testdb
rs0:PRIMARY> db.createCollection('test');
{ "ok" : 1 }

rs0:PRIMARY> show tables
test

Now, We all know how to create a Document DB , connect and to manage it.

Hope you have liked it, Please check out my other articles.