How To Create And Use AWS Document DB
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:
- Create Document DB cluster.
- Setup SSH Tunnel using AWS EC2 Instance.
- Create EC2 Instance.
- Connecting to Document DB.
Let’s Create A Cluster:
Open AWS Document DB Console.
DOCUMENT DB CONSOLE

Choose: Launch Amazon DocumentDB
Before Creating a cluster, First we need to create a parameter group.
Choose: Parameter Groups

Click Create
Fill in the details and create.


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.

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,

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

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.

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


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.

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

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 ubuntu@54.254.138.18
Open New Terminal And Connect To Document DB:
mongo --host 127.0.0.1:27017 --username dbusername --password dbpassword

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.