> ## Documentation Index
> Fetch the complete documentation index at: https://blog.clouddley.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Deploy a MongoDB Database on an AWS EC2 Instance using Clouddley

> Learn how to deploy a MongoDB Database on an AWS EC2 Instance using Clouddley.

If you’ve ever tried setting up [MongoDB](https://www.mongodb.com/) 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](https://clouddley.com) 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

* A [Clouddley account](https://clouddley.com/)
* An [AWS account](https://aws.amazon.com/)
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) installed and configured on your local machine
* [AWS Access Keys](https://docs.aws.amazon.com/keyspaces/latest/devguide/create.keypair.html) with permissions to create and manage EC2 instances
* [Git installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) locally
* Basic familiarity with the Linux command line

## 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::

```bash theme={null}
aws ec2 create-vpc --cidr-block 10.0.0.0/16
```

You should see an output like below:

![](https://mintcdn.com/clouddley/wuHX6szeuQPUk6oa/images/aws-create-vpc.png?fit=max\&auto=format\&n=wuHX6szeuQPUk6oa\&q=85\&s=8365b482684727bfca822e9dac2bf06f)

Create a VPC on AWS

* Create a security group by running the command::

```bash theme={null}
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
```

\[!NOTE] Replace the `vpc_id` with the one created in the previous step.
You should see an output like this:

![](https://mintcdn.com/clouddley/wuHX6szeuQPUk6oa/images/aws-create-sg.png?fit=max\&auto=format\&n=wuHX6szeuQPUk6oa\&q=85\&s=4dc2f133f251e8bf2ef23726d2c7c800)

Create a Security group on AWS

* Allow inbound traffic on port 22 (SSH) by running the command:

```bash theme={null}
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.

> [!NOTE]
>
> 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.

> **How to Generate and Import SSH Keys to AWS**
>
> 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:
>
> ```bash theme={null}
> ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
> ```
>
> 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:
>
> ```bash theme={null}
> aws ec2 import-key-pair \
>   --key-name my-key \
>   --public-key-material file://~/.ssh/id_rsa.pub
> ```
>
> > [!NOTE]
> >
> > Replace the path to your public key if it's different.

* Finally, create the EC2 instance:

```bash theme={null}
aws ec2 run-instances \
  --image-id ami-xxxxxxxx \
  --count 1 \
  --instance-type t2.micro \
  --key-name my-key \
  --security-group-ids <sg_id>
```

> [!NOTE]
>
> * 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:

![](https://mintcdn.com/clouddley/wuHX6szeuQPUk6oa/images/aws-create-ec2.png?fit=max\&auto=format\&n=wuHX6szeuQPUk6oa\&q=85\&s=76480a7889b778d09ba60224dd87010f)

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](https://app.clouddley.com/auth/signin).
* Navigate to the **Databases** on the left sidebar.
* Click on **Deploy Database**

![](https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-dashboard.png?fit=max\&auto=format\&n=p_iVNvg5jCvbMG-4\&q=85\&s=98b7fbb13523d355fa5d87886511629f)

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.**

> **How to Install Clouddley CLI to Verify your AWS EC2 Instance**
>
> * 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:
>
> ```bash theme={null}
> ssh root@<your-instance-ip>
> ```
>
> * Install Clouddley CLI by running the command:
>
> ```bash theme={null}
> curl -L https://raw.githubusercontent.com/clouddley/cli/main/install.sh | sh
> ```
>
> * To add the SSH public key, run the command:
>
> ```bash theme={null}
> 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**.

![](https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-configure-vm.png?fit=max\&auto=format\&n=p_iVNvg5jCvbMG-4\&q=85\&s=d18addba7ce0b3a86338d06f5790100d)

Configure virtual machine on Clouddley

**Step 3: Configure your database name**

* Enter a **name** for your MongoDB database.
* Click on **Next** to proceed.

![](https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-mongo-name.png?fit=max\&auto=format\&n=p_iVNvg5jCvbMG-4\&q=85\&s=4980b188248843697349301e5d833718)

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.
  \[!TIP] Ensure you select a version that is compatible with your application requirements.
* Enable delete protection to prevent accidental deletions.
* Click on **Deploy DB**

![](https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-mongo.png?fit=max\&auto=format\&n=p_iVNvg5jCvbMG-4\&q=85\&s=a72fda0fdc306b993c1322d0f9e4d449)

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!`

![](https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-success.png?fit=max\&auto=format\&n=p_iVNvg5jCvbMG-4\&q=85\&s=27451216c04bccb1a40f0d4d41acb4d0)

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:

```bash theme={null}
aws ec2 authorize-security-group-ingress \
    --group-id <sg_id> \
    --protocol tcp \
    --port 17315 \
    --cidr 0.0.0.0/0
```

\[!NOTE] 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.

![](https://mintcdn.com/clouddley/p_iVNvg5jCvbMG-4/images/Dbs-mongodb-overview.png?fit=max\&auto=format\&n=p_iVNvg5jCvbMG-4\&q=85\&s=94fb96542ffa700cd8a2a2e2c255ee66)

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](https://clouddley.productlane.com/roadmap). We can’t wait to see what you deploy next!

> **[Getting started with Clouddley?](https://app.clouddley.com/auth/signup)**
>
> A backend infrastructure for your own compute. Run apps, databases, brokers, and AI workloads on your VMs, bare metal, or VPS.

## Additional Resources

* [Clouddley Documentation](https://docs.clouddley.com/)
* [MongoDB Documentation](https://www.mongodb.com/docs/)
* [AWS EC2 Documentation](https://aws.amazon.com/ec2/)

![Faith Kovi](https://res.cloudinary.com/dkbbdg1ko/image/upload/faith-kovi-headshot_hbezxg)

By

[Faith Kovi](https://x.com/Vera__Kaka)

(Copied!)

Updated on September 13, 2025


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.