BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

MongoDB Atlas with Terraform

Samuel Molling9 min read • Published Jan 23, 2024 • Updated Jan 23, 2024
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
In this tutorial, I will show you how to start using MongoDB Atlas with Terraform and create some simple resources. This first part is simpler and more introductory, but in the next article, I will explore more complex items and how to connect the creation of several resources into a single module. The tutorial is aimed at people who want to maintain their infrastructure as code (IaC) in a standardized and simple way. If you already use or want to use IaC on the MongoDB Atlas platform, this article is for you.
What are modules?
They are code containers for multiple resources that are used together. They serve several important purposes in building and managing infrastructure as code, such as:
  1. Code reuse.
  2. Organization.
  3. Encapsulation.
  4. Version management.
  5. Ease of maintenance and scalability.
  6. Sharing in the community.
Everything we do here is contained in the provider/resource documentation.
Note: We will not use a backend file. However, for productive implementations, it is extremely important and safer to store the state file in a remote location such as an S3, GCS, Azurerm, etc…

Creating a project

In this first step, we will dive into the process of creating a project using Terraform. Terraform is a powerful infrastructure-as-code tool that allows you to manage and provision IT resources in an efficient and predictable way. By using it in conjunction with MongoDB Atlas, you can automate the creation and management of database resources in the cloud, ensuring a consistent and reliable infrastructure.
To get started, you'll need to install Terraform in your development environment. This step is crucial as it is the basis for running all the scripts and infrastructure definitions we will create. After installation, the next step is to configure Terraform to work with MongoDB Atlas. You will need an API key that has permission to create a project at this time.
To create an API key, you must:
  1. Select Access Manager at the top of the page, and click Organization Access.
  2. Click Create API Key. Organization Access Manager for your organization
  3. Enter a brief description of the API key and the necessary permission. In this case, I put it as Organization Owner. After that, click Next. Screen to create your API key
  4. Your API key will be displayed on the screen. Screen with information about your API key
  5. Release IP in the Access List (optional): If you have enabled your organization to use API keys, the requestor's IP must be released in the Access List; you must include your IP in this list. To validate whether it is enabled or not, go to Organization Settings -> Require IP Access List for the Atlas Administration API. In my case, it is disabled, as it is just a demonstration, but in case you are using this in an organization, I strongly advise you to enable it. Validate whether the IP Require Access List for APIs is enabled in Organization Settings
After creating an API key, let's start working with Terraform. You can use the IDE of your choice; I will be using VS Code. Create the files within a folder. The files we will need at this point are:
  • In this file, we will define the main resource, mongodbatlas_project. Here, you will configure the project name and organization ID, as well as other specific settings, such as teams, limits, and alert settings.
  • This file is where we define the provider we are using — in our case, mongodbatlas. Here, you will also include the access credentials, such as the API key.
  • terraform.tfvars: This file contains the variables that will be used in our project — for example, the project name, team information, and limits, among others.
  • Here, we define the variables mentioned in the terraform.tfvars file, specifying the type and, optionally, a default value.
  • This file is used to specify the version of Terraform and the providers we are using.
The file is the heart of our Terraform project. In it, you start with the data source declaration mongodbatlas_roles_org_id to obtain the org_id, which is essential for creating the project. Next, you define the mongodbatlas_project resource with several settings. Here are some examples:
  • name and org_id are basic settings for the project name and organization ID.
  • Dynamic blocks are used to dynamically configure teams and limits, allowing flexibility and code reuse.
  • Other settings, like with_default_alerts_settings and is_data_explorer_enabled, are options for customizing the behavior of your MongoDB Atlas project.
In the file, we will then add our project resource, called mongodbatlas_project.
In the provider file, we will define the provider we are using and the API key that will be used. As we are just testing, I will specify the API key as a variable that we will input into our code. However, when you are using it in production, you will not want to pass the API key in the code in exposed text, so it is possible to pass it through environment variables or even AWS secret manager.
In the file, we will specify the variables that we are waiting for a user to pass. As I mentioned earlier, the API key is an example.
As you can see, we are placing several variables. The idea is that we can reuse the module to create different projects that have different specifications. For example, one needs the data from the databases to be visible on the Atlas Platform, so we put the is_data_explorer_enabled variable as True. On the other hand, if, for some security reason, the company does not want users to use the platform for data visualization, it is possible to specify it as False, and the Collections button that we have when we enter the cluster within Atlas will disappear.
In the file, we specify the version of Terraform we use and the necessary providers with their respective versions. This configuration is crucial so that the code always runs in the same version, avoiding inconsistencies and incompatibilities that may arise with updates.
Here is what our providers file will look like:
  • required_version = ">= 0.12": This line specifies that your Terraform project requires, at a minimum, Terraform version 0.12. By using >=, you indicate that any version of Terraform from 0.12 onward is compatible with your project. This offers some flexibility by allowing team members and automation systems to use newer versions of Terraform as long as they are not older than 0.12.
  • required_providers: This section lists the providers required for your Terraform project. In your case, you are specifying the mongodbatlas provider.
  • source = "mongodb/mongodbatlas": This defines the source of the mongodbatlas provider. Here, mongodb/mongodbatlas is the official identifier of the MongoDB Atlas provider in the Terraform Registry.
  • version = "1.14.0": This line specifies the exact version of the mongodbatlas provider that your project will use, which is version 1.14.0. Unlike Terraform configuration, where we specify a minimum version, here you are defining a provider-specific version. This ensures that everyone using your code will work with the same version of the provider, avoiding discrepancies and issues related to version differences.
Finally, we have the variable file that will be included in our code, .tfvars.
We are specifying the value of the name variable, which is the name of the project and the public/private key of our provider. You may wonder, "Where are the other variables that we specified in the and files?" The answer is: These variables were specified with a default value within the file — for example, the limits value:
We are saying that if no information is passed in .tfvars, the default value is empty. This means that it will not create any limit rules for our project. If we want to specify a limit, we just put the following variable in .tfvars:
Now is the time to apply. =D
We run a terraform init in the terminal in the folder where the files are located so that it downloads the providers, modules, etc…
Now that init has worked, let's run the plan and evaluate what will happen. You can run the plan, like this terraform plan:
Show! It was exactly the output we expected to see: the creation of a project resource, with some limits and is_data_explorer_enabled set to False. Let's apply this!
When you run the command terraform apply, you will be asked for approval with yes or no. Type yes.
Now, let's look at Atlas to see if the project was created successfully...
MongoDB Atlas view of our project
It worked!
In this tutorial, we saw how to create our first API key. We created a project using Terraform and our first module. In an upcoming article, we’ll look at how to create a cluster and user using Terraform and Atlas.
To learn more about MongoDB and various tools, I invite you to enter the Developer Center to read the other articles.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial

How to Deploy MongoDB Atlas with AWS CloudFormation

Mar 20, 2024 | 6 min read

Part #1: Build Your Own Vector Search with MongoDB Atlas and Amazon SageMaker

Feb 07, 2024 | 4 min read

UDF Announcement for MongoDB to BigQuery Dataflow Templates

Apr 02, 2024 | 4 min read

Building an AI Agent With Memory Using MongoDB, Fireworks AI, and LangChain

Apr 23, 2024 | 21 min read
Table of Contents
  • Creating a project