BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Deploying MongoDB Atlas With Terraform with Azure

Karen Zhang7 min read • Published Jun 18, 2024 • Updated Jun 18, 2024
AtlasMulti-Cloud
Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
MongoDB Atlas is a fully managed cloud database. It handles the tricky parts of setting up, managing, and fixing your MongoDB databases on any major cloud platform (like AWS, Azure, or GCP). It's the easiest way to get started, run, and grow your MongoDB databases in the cloud.
Terraform is an open-source infrastructure as code (IaC) tool used to manage and provision infrastructure across various platforms, including cloud providers and on-premise data centers. Terraform automates your infrastructure's creation, modification, and destruction, ensuring consistency and repeatability. Terraform has a MongoDB Atlas provider that interacts with the resources supported by MongoDB Atlas.
In this tutorial, we’ll guide you through setting up MongoDB Atlas on the Azure platform and utilizing Terraform for deployment. The source code used in this article can be found in the GitHub repository.

Prerequisites

Before we get started, you will need to have the following accounts/tools set up:

Setting up the environment

To set up the environment, we will first generate MongoDB Atlas API access key, and then set up a billing method.

Generate MongoDB Atlas API access keys

After establishing an account on MongoDB Atlas and logging in, the next step is to generate an API key to verify the Terraform MongoDB Atlas Provider. To do this, select the access manager for the organization and create the access key. Follow the documentation to learn more about creating an API key.
Capture where to create an API key(screenshot from the Atlas UI representing the Organization Access Manager section)
Capture where to create an API key (screenshot from the Atlas UI representing the Organization Access Manager section)
Give your API key a clear name that reflects its purpose, like “Terraform Deployment API Key.” Next, we’ll choose the permissions needed for your Terraform tasks. Both Organization Owner and Organization Project Creator roles allow for enough access to complete this, but the Organization Project Creator role should be chosen following the principle of least privilege. To learn more about the user roles, follow the Organization Roles and Project Roles.
Capture for generating an API key* (screenshot from the Atlas UI representing creating API key section)
Capture for generating an API key* (screenshot from the Atlas UI representing creating API key section)
Copy your private key and public key, then set up the environment variables:
Capture for adding current IP to Access List Entry (screenshot from the Atlas UI representing adding access list entry section)
Next, we will add an API key access list entry to ensure that API calls originating from IPs or CIDR ranges can give access. We use our current IP address and save.

Setting up a billing method

MongoDB allows a free M0 cluster tier that you do not have to pay for. However, if you want to deploy any other clusters, we will have to set up a billing method. Follow the documentation about setting up a billing method.

Creating Terraform configuration files — variables.tf, terraform.tfvars, and main.tf files

We are now going to create a variables.tf file for declaring Terraform variables, a terraform.tfvars file for defining variable values and secrets, and a main.tf for the main Terraform configuration. In Terraform, these configuration files (variables.tf, terraform.tfvars, and main.tf) are typically created within the root directory of your Terraform project. You can find the source code in the GitHub repository.
First, we define the MongoDB Atlas and Azure Providers in main.tf:
The code block below defines the variables used. Variables include identifiers for the project and environment, the size of the computing resources allocated, the cloud platform to use (AWS, GCP, Azure), the specific region within that platform, the desired version of MongoDB, and the IP address(es) allowed to access the database cluster.
In the terraform.tfvars file, we specify the use of MongoDB version 7.0, a M10-sized cluster in the US_EAST_2 region on Azure. If you are outside North America, you can refer to the regions supported following the documentation for Microsoft Azure.
Please note that Atlas supports specific Azure regions and availability zones, and some regions do not support free clusters (M0) and shared clusters (M2/M5).

Configuring a MongoDB Atlas project

To create a project using Terraform, we will need the MongoDB Atlas Organization ID with at least the Organization Project Creator role. To get this information, we can go to Settings on the left-hand side menu bar in the UI, and we can find Organization Settings with the Organization ID. Please refer to the documentation on Organizations for further reference.
So next, in our main.tffile, we will use the mongodbatlas_project resource from Terraform MongoDB Atlas Provider to create our project. We add this to main.tf:

Creating a MongoDB Atlas user, password, and IP access list

We must add a corresponding database user to our MongoDB Atlas project. Refer to the documentation for available user roles in case you want to customize the user’s Role-Based Access Control.
The code block below creates a database user with a random password, and to review the password, you will need to put the terraform output -json user_password command in your terminal.

Provisioning the MongoDB Atlas cluster

We will now use the mongodbatlas_cluster resource to create a MongoDB Atlas cluster. The resource lets you create, edit, and delete clusters.
The code block shown above specifies a cluster named “test” with a replica set deployment type, which is ideal for applications requiring high availability and data redundancy.
The cluster is configured with a single shard and is set to automatically scale its disk storage as needed. The naming convention for the cluster incorporates variables that allow for environment-specific customization, such as ${var.atlas_project_name}-${var.environment}-cluster.
The configuration also specifies a replica set with three electable nodes in a configurable region, ensuring that the primary node can be effectively elected. The MongoDB major version is set to "7.0," aligning the cluster with the latest stable release.

Defining Terraform outputs

You can display information from your Terraform setup directly onto the terminal when running Terraform commands. This feature is particularly handy for obtaining values that are only determined after the resources have been created, such as a randomly generated database user password or the connection string needed for your Atlas cluster. The following code is in your main.tf file and will ensure that these values are shown on the terminal once Terraform has finished executing.
We are now ready to run terraform init. This command will initialize Terraform and download Terraform Azure and MongoDB Atlas providers.
Capture for running Terraform Init
Capture for running Terraform Init
The screenshot above shows that Terraform has been successfully initialized.

Reviewing proposed Terraform deployment

Next, we will be using terraform plan to review the proposed changes.
Capture for running Terraform Plan
Capture for running Terraform Plan
The screenshot above shows that Terraform will perform the following updates.
Capture the results of running the Terraform Plan
Capture the results of running the Terraform Plan
It shows that Terraform will create the following five resources: including mongodbatlas_cluster.test, mongodbatlas_database_user.db-user, mongodbatlas_project.atlas-project, mongodbatlas_project_ip_access_list.ip, and random_password.db-user-password.

Executing to create the resources

Now, we will execute the terraform apply command to initiate the deployment of the infrastructure. Upon reviewing and confirming that everything appears correct, respond with “yes” to give Terraform the go-ahead to proceed with the build.
Screenshot representing terraform apply
Screenshot representing terraform apply
Capture for running Terraform Apply
Capture for running Terraform Apply
It might take about 10 minutes for the cluster to deploy. Once it successfully deploys, you will be able to view it in Atlas UI. Below, we can see that an M10 cluster has been deployed to Azure in the East US 2 region.
Capture for successfully deploying a cluster(screenshot from the Atlas UI representing the deployed cluster
Capture for successfully deploying a cluster(screenshot from the Atlas UI representing the deployed cluster)
Congratulations! You have now successfully deployed an M10 cluster to Azure.

Cleaning up using Terraform destroy

When ready to destroy the infrastructure created, we can use terraform destroy to safely tear down the infrastructure. Type “yes” to destroy all the resources deployed. Be extra careful, though, if you are trying to destroy resources in the production environment.
Capture for running Terraform Destroy
Capture for running Terraform Destroy

Conclusion

In this tutorial, we learned how to deploy a MongoDB Atlas cluster to Azure. The integration of MongoDB Atlas with Terraform on Azure offers a powerful and flexible approach to cloud database management. By leveraging Terraform’s infrastructure as code capabilities, we have streamlined the deployment process for MongoDB Atlas, ensuring that our infrastructure is both reproducible and easily manageable.

Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
News & Announcements

Deprecating MongoDB Atlas GraphQL and Hosting Services


Mar 12, 2024 | 2 min read
Tutorial

Getting Started with MongoDB Atlas, NodeJS, and Azure App Service


Apr 02, 2024 | 5 min read
Article

Build a Newsletter Website With the MongoDB Data Platform


Sep 23, 2022 | 9 min read
Tutorial

How to Archive Data to Cloud Object Storage with MongoDB Online Archive


May 31, 2023 | 9 min read
Table of Contents