For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

Get Started with Terraform and the MongoDB Atlas Provider

You can use Terraform to manage your MongoDB Atlas infrastructure as code with the Atlas Terraform provider. The Atlas Terraform provider automates infrastructure deployments by simplifying the process to provision, manage, and control Atlas infrastructure as code.

This guide teaches you how to create and destroy Atlas clusters in an existing MongoDB Atlas organization.

Note

The Atlas Terraform provider supports any MongoDB version that Atlas supports in your project and region. To learn how to change your cluster's MongoDB version, see Upgrade a major version.

Once you have configured a test environment, continue to experiment with a Terraform-managed infrastructure with additional resources from MongoDB and HashiCorp.

Atlas Infinite clusters support the Atlas Terraform provider. To learn more about Atlas Infinite, see MongoDB Atlas Infinite: Overview.

Before deploying MongoDB Atlas with Terraform, you must:

1
mkdir terraform-proj
cd terraform-proj
2

Create the main.tf, provider.tf, variables.tf, and versions.tf files.

touch main.tf provider.tf variables.tf versions.tf
3

Add the following content to provider.tf:

provider "mongodbatlas" {
client_id = var.client_id
client_secret = var.client_secret
}

Add the following content to variables.tf:

variable "client_id" {
description = "Atlas Service Account client ID"
type = string
}
variable "client_secret" {
description = "Atlas Service Account client secret"
type = string
sensitive = true
}
variable "org_id" {
description = "Atlas organization ID"
type = string
}

Add the following content to versions.tf:

terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
version = "~> 2.2"
}
}
required_version = "~> 1.0"
}

Note

Your Atlas Service Account client ID and client secret are secrets. Consider storing them as environment variables.

4

Note

The following example uses the project module to create the project, and the mongodbatlas_advanced_cluster resource to create a Free cluster.

Add the following content to your main.tf file:

module "project" {
source = "terraform-mongodbatlas-modules/project/mongodbatlas"
name = "tenant-example-project"
org_id = var.org_id
}
resource "mongodbatlas_advanced_cluster" "this" {
project_id = module.project.id
name = "TenantCluster"
cluster_type = "REPLICASET"
replication_specs = [
{
region_configs = [
{
provider_name = "TENANT"
backing_provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
electable_specs = {
instance_size = "M0"
}
}
]
}
]
}

If you work with a different cloud provider or region, you can update the following fields to match your environment.

Field
New Value

backing_provider_name

Your provider. Possible values are: "AWS", "AZURE", or "GCP".

region_name

See Cloud Providers and Regions for all the regions you can use.

Important

You can set database_edition on the mongodbatlas_advanced_cluster resource when you create a dedicated cluster. You can't change it afterward, and you can't scale a dedicated cluster back to a Free or Flex cluster. database_edition is unavailable for Free clusters, such as the M0 cluster in this example, and for Flex clusters.

The mongodbatlas_advanced_cluster resource, and both the singular (mongodbatlas_advanced_cluster) and plural (mongodbatlas_advanced_clusters) data sources, expose database_edition and the read-only effective_database_edition attribute. Because database_edition is optional, use effective_database_edition to confirm which edition Atlas assigned, even if you didn't set it yourself.

To create a dedicated cluster on Atlas Infinite, set database_edition to "INFINITE" and set compute_enabled in auto_scaling, as the following example shows:

resource "mongodbatlas_advanced_cluster" "infinite" {
project_id = module.project.id
name = "InfiniteCluster"
cluster_type = "REPLICASET"
database_edition = "INFINITE"
replication_specs = [
{
region_configs = [
{
provider_name = "AWS"
priority = 7
region_name = "US_EAST_1"
electable_specs = {
instance_size = "M10"
node_count = 2
}
auto_scaling = {
compute_enabled = true
compute_max_instance_size = "M60_GEN_2"
storage_config = {
shard_size_limit_gb = 10240
}
}
}
]
}
]
}

Dedicated Atlas Infinite clusters support the REPLICASET cluster type only.

compute_max_instance_size is required when compute_enabled is true. M30+ Atlas Infinite clusters are available on AWS Gen2 only, so append _GEN_2 to the tier name, as in M60_GEN_2. To learn more, see Gen2 Dedicated Clusters.

An Atlas Infinite cluster has two electable compute nodes, a primary and a standby, so set node_count to 2. To learn more, see Replication and Failover.

To serve more reads or to isolate analytics queries from your operational workload, add read-only or analytics nodes by setting read_only_specs or analytics_specs alongside electable_specs in the same region_configs object. To learn more, see Workload Isolation on Atlas Infinite clusters.

storage_config.shard_size_limit_gb is required only if you include storage_config. If you omit storage_config, Atlas applies the default maximum storage limit. To learn more, see Max Storage Limit.

Note

storage_config.shard_size_limit_gb sets the maximum amount of data the cluster can store. To learn more, see the Terraform provider documentation.

5

You can output information from your Terraform configuration to your terminal window. This is useful for values you won't know until Atlas creates the resources, such as your connection string.

If you want to display your parameters after you deploy your project, add some output lines of code to your main.tf file.

6

To initialize your project, run the following command:

terraform init

This command also downloads and installs the MongoDB Atlas Provider, if you haven't already.

7

To view your execution plan, run the following command:

terraform plan

Terraform details the changes that it plans to make. If the output is not what you expect, then there might be an issue in your main.tf file.

8

To deploy your infrastructure, run the following command:

terraform apply

When prompted Do you want to perform these actions?, enter yes.

Note

New Atlas resources can take a few minutes to provision. The Atlas Terraform provider updates you every ten seconds until it's complete.

After terraform apply completes, confirm the cluster is deployed.

  • A new cluster appears in the Atlas UI under your project.

  • If you added output values to main.tf, the connection string appears in the Terraform output.

To delete all of the resources created in your Terraform directory, run the following command:

terraform destroy

Warning

If you delete all your resources, you can't recover them.

When prompted Do you really want to destroy all resources?, enter yes.

After you deploy your cluster, you can:

Additional resources: