Deploying a MongoDB Cluster using Terraform: A Step-by-Step Guide

Sumanta Mukhopadhyay
4 min readFeb 25, 2023

MongoDB is a popular NoSQL database that is widely used by businesses of all sizes. However, setting up and managing a MongoDB cluster can be a complex and time-consuming process. That’s where Terraform comes in. Terraform is an infrastructure as code (IAC) tool that enables you to deploy and manage cloud resources with ease. In this article, we’ll show you how to deploy a MongoDB cluster using Terraform.

Step 1: Define the Infrastructure Requirements

The first step in deploying a MongoDB cluster with Terraform is to define the infrastructure requirements. This includes defining the virtual machines, network security groups, and storage accounts required for your MongoDB deployment. Here’s an example of what your Terraform configuration file might look like:

resource "azurerm_virtual_network" "mongodb_vnet" {
name = "mongodb_vnet"
address_space = ["10.0.0.0/16"]
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
}

resource "azurerm_subnet" "mongodb_subnet" {
name = "mongodb_subnet"
address_prefix = "10.0.0.0/24"
virtual_network_name = "${azurerm_virtual_network.mongodb_vnet.name}"
resource_group_name = "${var.resource_group_name}"
}

resource "azurerm_network_security_group" "mongodb_nsg" {
name = "mongodb_nsg"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
}

resource "azurerm_storage_account" "mongodb_sa" {
name = "mongodbsa"
resource_group_name = "${var.resource_group_name}"
location = "${var.location}"
account_tier = "Standard"
account_replication_type = "GRS"
}

This example defines a virtual network, a subnet, a network security group, and a storage account required for your MongoDB deployment. You can modify this configuration file to fit your specific requirements.

Step 2: Configure and Provision the MongoDB Instances

Once you’ve defined your infrastructure requirements, the next step is to configure and provision the MongoDB instances. You can do this by using a configuration management tool like Ansible or Chef to install and configure MongoDB on the virtual machines. Here’s an example of what your Ansible playbook might look like:

- hosts: mongodb_servers
become: yes
tasks:
- name: Install MongoDB
apt:
name: mongodb
state: latest
tags:
- mongodb

This example uses Ansible to install MongoDB on the virtual machines defined in your Terraform configuration file.

Step 3: Configure and Provision the Replica Set

The next step is to configure and provision the replica set for your MongoDB cluster. This involves defining the primary and secondary nodes and configuring the replication process. Here’s an example of what your MongoDB replica set configuration might look like:

rs.initiate({
_id: "myReplicaSet",
version: 1,
members: [
{ _id: 0, host: "mongodb1:27017" },
{ _id: 1, host: "mongodb2:27017" },
{ _id: 2, host: "mongodb3:27017" }
]
})

This example defines a replica set with three nodes: mongodb1, mongodb2, and mongodb3.

Step 4: Deploy Additional Resources

Finally, you can deploy any necessary additional resources, such as monitoring and backup solutions, to your MongoDB cluster using Terraform. Here’s an example of what your Terraform configuration file might look like to deploy Azure Backup:

resource "azurerm_backup_policy_file_share" "mongodb_backup_policy" {
name = "mongodb_backup_policy"
resource_group_name = "${var.resource_group_name}"
backup_management_type = "AzureIaasVM"
backup_schedule {
frequency = "Daily"
time = "23:00"
}
retention_daily {
count = 7
}
}

This example deploys Azure Backup with a policy that runs daily backups and retains the backups for seven days.

Step 5: Deploy the MongoDB Cluster

Once you’ve completed the previous steps, you can deploy your MongoDB cluster using Terraform. Here’s an example of what your Terraform configuration file might look like to deploy your MongoDB cluster:

resource "azurerm_virtual_machine" "mongodb_server" {
count = 3
name = "mongodb_server${count.index+1}"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
network_interface_ids = ["${element(azurerm_network_interface.mongodb_nic.*.id, count.index)}"]
storage_os_disk {
name = "mongodb_os_disk${count.index+1}"
create_option = "FromImage"
caching = "ReadWrite"
managed_disk_type = "Standard_LRS"
}
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
os_profile {
computer_name = "mongodb_server${count.index+1}"
admin_username = "adminuser"
admin_password = "Password1234"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags = {
environment = "dev"
}
}

resource "azurerm_network_interface" "mongodb_nic" {
count = 3
name = "mongodb_nic${count.index+1}"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
ip_configuration {
name = "mongodb_ipconfig${count.index+1}"
subnet_id = "${azurerm_subnet.mongodb_subnet.id}"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = "${azurerm_public_ip.mongodb_public_ip.id}"
}
}

resource "azurerm_public_ip" "mongodb_public_ip" {
name = "mongodb_public_ip"
location = "${var.location}"
resource_group_name = "${var.resource_group_name}"
allocation_method = "Dynamic"
}

This example deploys a MongoDB cluster with three virtual machines, each with its network interface and public IP address.

Conclusion

In this article, we’ve shown you how to deploy a MongoDB cluster using Terraform. By following these steps, you can quickly and easily provision your MongoDB infrastructure on your chosen cloud platform. While this is just a basic example, you can modify these configurations to suit your specific requirements. With Terraform, you can automate the deployment and management of your MongoDB infrastructure, saving time and reducing the risk of errors.

--

--