5 Different Ways to Deploy a Free Database with MongoDB Atlas
Rate this quickstart
You might have already known that MongoDB offers a free tier through M0 clusters on MongoDB Atlas, but did you know that there are numerous ways to deploy depending on your infrastructure needs? To be clear, there's no wrong way to deploy a MongoDB Atlas cluster, but there could be an easier way to fit your operations needs.
In this article, we're going to have a quick look at the various ways you can deploy a MongoDB Atlas cluster using tools like Terraform, CloudFormation, CLIs, and simple point and click.
If you're a fan of point and click deployments like I am, the web UI for MongoDB Atlas will probably fit your needs. Let's take a quick look at how to deploy a new cluster with a database using the UI found within the MongoDB Cloud Dashboard.
Within the Databases tab for your account, if you don't have any databases or clusters, you'll be presented with the opportunity to build one using the "Build a Database" button.
Since we're keeping things free for this article, let's choose the "Shared" option when presented on the next screen. If you think you'll need something else, don't let me stop you!
After selecting "Shared" from the options, you'll be able to create a new cluster by first selecting your cloud service provider and region.
You can use the defaults, or select a provider or region that you would prefer to use. Your choice has no impact on how you will end up working with your cluster. However, choosing a provider and location that matches your other services could render performance improvements.
After selecting the "Create Cluster" button, your cluster will deploy. This could take a few minutes depending on your cluster size.
At this point, you can continue exploring Atlas, create a database or two, and be on your way to creating great applications. A good next step after deploying your cluster would be adding entries to your access list. You can learn how to do that here.
Let's say you prefer a more CLI-driven approach.
The MongoDB CLI can be useful if you want to do script-based deployments or if you prefer to do everything from the command line.
To install the MongoDB CLI, check out the installation documentation and follow the instructions. You'll also need to have a MongoDB Cloud account created.
If this is your first time using the MongoDB CLI, check out the configuration documentation to learn how to add your credentials and other information.
For this example, we're going to use the quick start functionality that the CLI offers. From the CLI, execute the following:
1 mongocli atlas quickstart
Using the quick start approach, you'll be presented with a series of questions regarding how you want your Atlas cluster configured. This includes the creation of users, network access rules, and other various pieces of information.
A similar option to using the CLI for creating MongoDB Atlas clusters is to use the Atlas Admin API. One difference here is that you don't need to download or install any particular CLI and you can instead use HTTP requests to get the job done using anything capable of making HTTP requests.
Take the following HTTP request, for example, one that can still be executed from the command prompt:
1 curl --location --request POST 'https://mongodb.prakticum-team.ru/proxy/cloud.mongodb.com/api/atlas/v1.0/groups/{GROUP_ID}/clusters?pretty=true' \ 2 --user "{PUBLIC_KEY}:{PRIVATE_KEY}" --digest \ 3 --header 'Content-Type: application/json' \ 4 --data-raw '{ 5 "name": "MyCluster", 6 "providerSettings": { 7 "providerName": "AWS", 8 "instanceSizeName": "M10", 9 "regionName": "US_EAST_1" 10 } 11 }'
The above cURL request is a trimmed version, containing just the required parameters, taken from the Atlas Admin API documentation. You can try the above example after switching the
GROUP_ID
, PUBLIC_KEY
, and PRIVATE_KEY
placeholders with those found in your Atlas dashboard. The GROUP_ID
is the project id representing where you'd like to create your cluster. The PUBLIC_KEY
and PRIVATE_KEY
are the keys for a particular project with proper permissions for creating clusters.The same cURL components can be executed in a programming language or even a tool like Postman. The Atlas Admin API is not limited to just cURL using a command line.
While you can use the Atlas Admin API to create users, apply access rules, and similar, it would take a few different HTTP requests in comparison to what we saw with the CLI because the CLI was designed to make these kinds of interactions a little easier.
For information on the other optional fields that can be used in the request, refer to the documentation.
There's a chance that your organization is already using an infrastructure-as-code (IaC) solution such as Terraform. The great news is that we have a Terraform provider for MongoDB Atlas that allows you to create a free Atlas database easily.
Take the following example Terraform configuration:
1 locals { 2 mongodb_atlas_api_pub_key = "PUBLIC_KEY" 3 mongodb_atlas_api_pri_key = "PRIVATE_KEY" 4 mongodb_atlas_org_id = "ORG_ID" 5 mongodb_atlas_project_id = "PROJECT_ID" 6 } 7 8 terraform { 9 required_providers { 10 mongodbatlas = { 11 source = "mongodb/mongodbatlas" 12 version = "1.1.1" 13 } 14 } 15 } 16 17 provider "mongodbatlas" { 18 public_key = local.mongodb_atlas_api_pub_key 19 private_key = local.mongodb_atlas_api_pri_key 20 } 21 22 resource "mongodbatlas_cluster" "my_cluster" { 23 project_id = local.mongodb_atlas_project_id 24 name = "terraform" 25 26 provider_name = "TENANT" 27 backing_provider_name = "AWS" 28 provider_region_name = "US_EAST_1" 29 provider_instance_size_name = "M0" 30 } 31 32 output "connection_strings" { 33 value = mongodbatlas_cluster.my_cluster.connection_strings.0.standard_srv 34 }
If you added the above configuration to a main.tf file and swapped out the information at the top of the file with your own, you could execute the following commands to deploy a cluster with Terraform:
1 terraform init 2 terraform plan 3 terraform apply
The configuration used in this example was taken from the Terraform template accessible within the Visual Studio Code Extension for MongoDB. However, if you'd like to learn more about Terraform with MongoDB, check out the official provider information within the Terraform Registry.
If your applications are all hosted in AWS, then CloudFormation, another IaC solution, may be one you want to utilize.
If you're interested in a script-like configuration for CloudFormation, Cloud Product Manager Jason Mimick wrote a thorough tutorial titled Get Started with MongoDB Atlas and AWS CloudFormation. However, like I mentioned earlier, I'm a fan of a point and click solution.
A point and click solution can be accomplished with AWS CloudFormation! Navigate to the MongoDB Atlas on AWS page and click "How to Deploy."
You'll have a few options, but the simplest option is to launch the Quick Start for deploying without VPC peering.
The next steps involve following a four-part configuration and deployment wizard.
The first step consists of selecting a configuration template.
Unless you know your way around CloudFormation, the defaults should work fine.
The second step of the configuration wizard is for defining the configuration information for MongoDB Atlas. This is what was seen in other parts of this article.
Replace the fields with your own information, including the public key, private key, and organization id to be used with CloudFormation. Once more, these values can be found and configured within your MongoDB Atlas Dashboard.
The final stage of the configuration wizard is for defining permissions. For the sake of this article, everything in the final stage will be left with the default provided information, but feel free to use your own.
Once you review the CloudFormation configuration, you can proceed to the deployment, which could take a few minutes.
As I mentioned, if you'd prefer not to go through this wizard, you can also explore a more scripted approach using the CloudFormation and AWS CLI.
You just got an introduction to some of the ways that you can deploy MongoDB Atlas clusters. Like I mentioned earlier, there isn't a wrong way, but there could be a better way depending on how you're already managing your infrastructure.
If you get stuck with your MongoDB Atlas deployment, navigate to the MongoDB Community Forums for some help!