Open In App

Creating an Atlas Cluster from a Terraform Template in MongoDB

Introducing automation into database management can greatly enhance efficiency and consistency. MongoDB Atlas, a popular cloud database service, offers a convenient platform for managing MongoDB databases. By using Terraform, an Infrastructure as Code (IaC) tool, users can automate the provisioning and management of MongoDB Atlas clusters.

In this article, We will learn the process of creating an Atlas Cluster from a Template using Terraform, providing a structured approach to setting up and configuring MongoDB Atlas clusters in a scalable and repeatable manner



What is Terraform?

Prerequisites

Before going, Please ensure that you have the following prerequisites:

Steps to Create an Atlas Cluster

1. Create an Atlas Terraform File using the Template

Use the Atlas template for Terraform files included with the MongoDB for VS Code to configure an Atlas cluster:



2. Update the Atlas Terraform Configuration to Configure Our Cluster

To update the Atlas Terraform configuration to configure your cluster, you need to provide values for each of the attributes mentioned. Below is a guide to help you update the configuration:

3. Update the Local Variables

Warning: The local variables contain sensitive information. Do not check these values in to a repository that is available publicly.

Provide values for the following local variables:

Example: Maximizing Security with Input Variables File

Use an Input Variables File to Maximize security

To maximize security, consider taking the following steps :

a. Create an Input Variables File:

Begin by creating an input variables file (e.g., vars.tfvars) where you’ll define your sensitive data. This file will securely store your MongoDB Atlas API keys.

# vars.tfvars

variable "mongodb_atlas_api_pub_key" {
default = "my-public-key"
}

variable "mongodb_atlas_api_pri_key" {
default = "my-private-key"
}

b. Exclude Input Variables File from Version Control:

For enhanced security, ensure that the input variables file is excluded from your repository. Add the filename (vars.tfvars) to your .gitignore file to prevent it from being tracked by version control systems.

# .gitignore
vars.tfvars

c. Reference Variables in Main Configuration File:

In your main configuration file (main.tf), reference the variables defined in the input variables file. Prefix the variable names with vars. to access them.

provider "mongodbatlas" {
public_key = vars.mongodb_atlas_api_pub_key
private_key = vars.mongodb_atlas_api_pri_key
}

4. Adding Optional Configuration Options to the Terraform Main Configuration File

When configuring your MongoDB Atlas clusters using Terraform, you may want to include optional configurations to tailor the setup according to your specific requirements. Here’s a brief note with sample code on how to add optional configuration options to your Terraform .tf file:

# main.tf

provider "mongodbatlas" {
// Specify your MongoDB Atlas provider configuration here
// For example:
public_key = var.mongodb_atlas_api_pub_key
private_key = var.mongodb_atlas_api_pri_key
}

resource "mongodbatlas_cluster" "my_cluster" {
// Define your MongoDB Atlas cluster configuration here
// Add any optional configurations as needed
// For example:
name = "my-cluster"
cluster_type = "REPLICASET"
provider_instance_size_name = "M10"
disk_size_gb = 40
// Add more configurations as required
}

5. Create the Atlas Cluster using Terraform

After we create a Terraform file using the template, create the Atlas cluster:

1. Go to the directory containing your main.tf file.

2. Execute terraform init to install necessary providers.

terraform init

The following output indicates that the MongoDB Atlas Terraform Provider is installed and ready for use:

3. Run the terraform plan command to view what happens when you apply the configuration

terraform plan

Here’s the combined breakdown of the Terraform execution plan output:

------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# mongodbatlas_cluster.my_cluster will be created
+ resource "mongodbatlas_cluster" "my_cluster" {
...
}

# mongodbatlas_database_user.my_user will be created
+ resource "mongodbatlas_database_user" "my_user" {
...
}

# mongodbatlas_project.my_project will be created
+ resource "mongodbatlas_project" "my_project" {
...
}

# mongodbatlas_project_ip_whitelist.my_ipaddress will be created
+ resource "mongodbatlas_project_ip_whitelist" "my_ipaddress" {
...
}

Plan: 4 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions
will be performed if "terraform apply" is subsequently run.

6. Delete the Atlas Cluster using Terraform

Deleting a cluster destroys databases, collections, and documents stored on it and all other resources defined in the Terraform configuration in which you configured the cluster. Proceed with caution. To delete the Atlas cluster:

Note: The terraform destroy command might take several minutes to complete. The following output indicates that the Atlas cluster and all associated resources are deleted:

Destroy complete! Resources: 4 destroyed.

Conclusion

In conclusion, by understanding the Terraform to automate the creation of MongoDB Atlas clusters significantly fast the deployment process. It enhances efficiency, ensures consistency in configurations, and enables scalability. By following the steps outlined in this guide, users can easily manage and maintain their MongoDB Atlas clusters, improving overall infrastructure management. This approach not only saves time and effort but also enhances the reliability and security of MongoDB Atlas deployments.


Article Tags :