Skip to main content
If you’ve ever tried setting up MongoDB database on a server, you know it can feel like a maze of configurations and steps. The truth is, it doesn’t have to be that complicated. By running MongoDB on an AWS EC2 instance and letting Clouddley simplify deployment, you can get everything up and running much faster. In this tutorial, we’ll cover how to deploy a MongoDB database on an EC2 instance so you have a reliable database ready for your applications.

Prerequisites

Create an AWS EC2 Instance

Create an EC2 instance to host your app. With AWS CLI, you can quickly create one. First, create a VPC and security group if you don’t want to use the default ones.
  • Create a VPC by running the command::
aws ec2 create-vpc --cidr-block 10.0.0.0/16
You should see an output like below:

Create a VPC on AWS


  • Create a security group by running the command::
aws ec2 create-security-group \
    --group-name new-security-group \
    --description "Security group for EC2 with port open" \
    --vpc-id <vpc_id> \
    --region us-east-1
Replace the vpc_id with the one created in the previous step.
You should see an output like this:

Create a Security group on AWS


  • Allow inbound traffic on port 22 (SSH) by running the command:
aws ec2 authorize-security-group-ingress \
    --group-id <sg_id> \
    --protocol tcp \
    --port 22 \
    --cidr 0.0.0.0/0
This command allows SSH access to your instance.
Replace the sg_id with the one created in the previous step.
  • To create an EC2 instance, you need to have an SSH key pair. If you don’t have one, you can generate and import one below.
If you don’t have an SSH key pair on your local machine, follow the steps below.
  • Step 1: To generate an SSH key, run the command:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Press Enter to accept the default file location and choose a passphrase if you want one.
  • Step 2: To add your SSH key to AWS, run:
aws ec2 import-key-pair \
  --key-name my-key \
  --public-key-material file://~/.ssh/id_rsa.pub
Replace the path to your public key if it’s different.
  • Finally, create the EC2 instance:
aws ec2 run-instances \
  --image-id ami-xxxxxxxx \
  --count 1 \
  --instance-type t2.micro \
  --key-name my-key \
  --security-group-ids <sg_id>
  • Replace ami-xxxxxxxx with regions’s compatible AMI
  • Replace my-key with your key pair name
  • Replace sg_id with your security group id
You should see the output below:

Create a EC2 instance on AWS


Once the instance is running, note the Public IP address of the instance. You’ll need it later.

Deploy

Now you have your AWS EC2 instance set up, it’s time to deploy a MongoDB database using Clouddley. Step 1: Access Databases
  • Log in to your Clouddley account.
  • Navigate to the Databases on the left sidebar.
  • Click on Deploy Database

Accessing Databases


Step 2: Configure your virtual machine(VM)
  • In the choose or add server dropdown, select your VM if it’s listed. If not, select + Add Virtual Machine and enter your EC2 instance IP address as the VM host, along with the VM User and SSH port.
  • After adding the details, verify the connection with the Clouddley CLI (recommended) or SSH.
  • Open your local machine’s command line, then connect to the remote VM you want to configure with Clouddley. Use this command to SSH into your EC2 instance:
ssh root@<your-instance-ip>
  • Install Clouddley CLI by running the command:
curl -L https://raw.githubusercontent.com/clouddley/cli/main/install.sh | sh
  • To add the SSH public key, run the command:
clouddley add key
Using the CLI, you can deploy resources, manage configurations, and automate tasks efficiently.
  • Click Verify to check the connection.
  • Once verified, click on Next.

Configure virtual machine on Clouddley


Step 3: Configure your database name
  • Enter a name for your MongoDB database.
  • Click on Next to proceed.

Defining a unique name for the MongoDB database


Step 4: Select your database type and version
  • Choose MongoDB as your database type.
  • Select the version of MongoDB you want to deploy.
    Ensure you select a version that is compatible with your application requirements.
  • Enable delete protection to prevent accidental deletions.
  • Click on Deploy DB

Selecting the MongoDB database type and specifying the version


Step 5: Confirm deployment
  • Once your deployment is complete, you will see a confirmation message Deployed successfully!

MongoDB database successfully deployed


Step 6: Open database port on AWS EC2 instance
  • Check the port Clouddley has configured for your PostgreSQL database. Then, add a new rule to your AWS security group to allow traffic for that port. For example, if Clouddley configured port 17315, run:
aws ec2 authorize-security-group-ingress \
    --group-id <sg_id> \
    --protocol tcp \
    --port 17315 \
    --cidr 0.0.0.0/0
Replace the sg_id with the one created earlier.
You can now connect your MongoDB database to your application.

Manage your database

The Database dashboard makes it easy to manage your MongoDB database on an AWS EC2 instance. You can deploy, monitor, and maintain your database. Here’s what you can do:
  • View real-time logs to check performance and troubleshoot fast.
  • Reset passwords securely with built-in authentication and encryption.
  • Prevent accidental deletions with built-in delete protection.
  • Manage the database users and access control.

Database dashboard

Everything you need to manage your database is right where you need it.

Conclusion

That’s all it takes, your MongoDB database is now deployed on AWS with the help of Clouddley, running smoothly on an EC2 instance. Instead of getting stuck in setup details, you now have a clean foundation to store and manage data. From here, you can connect your applications, scale as needed, and trust that your database is ready to support whatever you build next. Got feedback or ideas to improve the experience? We’d love to hear from you. We can’t wait to see what you deploy next!

Getting started with Clouddley?

A backend infrastructure for your own compute. Run apps, databases, brokers, and AI workloads on your VMs, bare metal, or VPS.

Additional Resources