DEV Community

Cover image for Automate MongoDB Atlas Deployment with Terraform
Joe Nyirenda
Joe Nyirenda

Posted on

Automate MongoDB Atlas Deployment with Terraform

If you're working with MongoDB Atlas and looking to scale or standardize deployments across environments, Terraform is your friend.

In this video , I’ll walk you through how to automate your MongoDB Atlas deployments using Terraform—so you can spend less time in the UI and more time building.

Why Terraform?

Terraform lets you define and provision infrastructure using code. With the official MongoDB Atlas provider, you can version, audit, and reuse your configurations. That means faster, safer, and repeatable deployments—especially helpful when working in teams or managing multiple environments.

What You’ll Need

  • A MongoDB Atlas account
  • An organization/project API key with the right roles
  • Terraform installed locally
  • A basic understanding of HCL (HashiCorp Configuration Language)
  provider "mongodbatlas" {
  public_key  = var.mongodb_atlas_public_key
  private_key = var.mongodb_atlas_private_key
}

resource "mongodbatlas_project" "project" {
  name   = "my-terraform-project"
  org_id = var.mongodb_atlas_org_id
}

resource "mongodbatlas_cluster" "cluster" {
  project_id   = mongodbatlas_project.project.id
  name         = "my-cluster"
  provider_name = "AWS"
  region_name   = "US_EAST_1"
  cluster_type  = "REPLICASET"
  backing_provider_name = "AWS"
  auto_scaling_disk_gb_enabled = true
  mongo_db_major_version = "6.0"
  provider_instance_size_name = "M10"
}
Enter fullscreen mode Exit fullscreen mode

Add variables and terraform.tfvars as needed. Then run the following commands:

terraform init
terraform validate
terraform plan
terraform apply -auto-approve
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Automating MongoDB Atlas deployment with Terraform helps reduce human error and improves repeatability. Whether you're deploying one cluster or dozens, this approach scales with you.

Let me know what else you would like to automate in MongoDB Atlas.

Happy building!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay