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.
Docs Menu

Create an Atlas Cluster from a Template using Terraform

This page outlines how to use the Atlas template for Terraform files included with the MongoDB Extension for Visual Studio Code to create Flex clusters.

After loading the template, you configure the cluster and provide details about your Atlas account. You then use Terraform commands to plan, apply, and destroy your Atlas cluster.

Before you use the Atlas template for Terraform files included with the MongoDB Extension for Visual Studio Code, you must:

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

1
Operating System
Method
Actions

Any

Visual Studio Code Menu

File > New File

MacOS

Keyboard Shortcut

Press Command + N

Windows and Linux

Keyboard Shortcut

Press Control + N

2

Note

main.tf is the recommended filename for the entry point to a Terraform module.

3

The MongoDB Extension for VSCode populates the file with an example configuration using the MongoDB Atlas Terraform provider to create a Flex cluster.

4

The cursor moves to select the value of the name argument of the mongodbatlas_project resource.

Provide values for the following arguments to configure your cluster:

Tip

Press the Tab key to select to the next argument in the template that you should update.

Attribute
Value

mongodbatlas_project
.name

Name of the Atlas project that this configuration creates.

mongodbatlas_advanced_cluster
.name

Name of the Atlas Flex cluster that this configuration creates.

mongodbatlas_advanced_cluster
.replication_specs.
region_configs.
backing_provider_name

Provider on which the Atlas Flex cluster that this configuration creates is hosted. Choose one of the following accepted values:

  • AWS

  • AZURE

  • GCP

mongodbatlas_advanced_cluster
.replication_specs.
region_configs.
region_name

Region to which the Atlas Flex cluster that this configuration creates is deployed. Ensure that the region you chose supports Flex clusters.

For details about the regions that each provider supports, see the following sections in the Atlas documentation:

5

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:

Variable
Value

mongodb_atlas_client_id

Atlas service account client ID.

mongodb_atlas_client_secret

Atlas service account client secret.

mongodb_atlas_org_id

Atlas organization ID in which you want to create a project.

mongodb_atlas_database_username

Username of the MongoDB database user that Atlas creates for your cluster.

mongodb_atlas_database_user_password

Password for the MongoDB database user named in mongodb_atlas_database_username.

mongodb_atlas_accesslistip

IP address or CIDR block from which your Atlas cluster is accessible.

Note

The template authenticates to Atlas with a service account. API keys are also supported, but service accounts are the recommended authentication method. To learn about both methods, see the MongoDB Atlas Terraform Provider authentication guide.

Example

Use an Input Variables File to Maximize security

To maximize security, consider taking the following steps:

  1. Define the local variables in an input variables file.

    variable "mongodb_atlas_client_id" {
    default = "my-client-id"
    }
    variable "mongodb_atlas_client_secret" {
    default = "my-client-secret"
    }
  2. Exclude the input variables file from your repository. For example, add the filename to the .gitignore file for your repository.

  3. Reference variables from the input variables file in the main.tf file by prefacing them with var..

    provider "mongodbatlas" {
    client_id = var.mongodb_atlas_client_id
    client_secret = var.mongodb_atlas_client_secret
    }
6

For a complete list of supported configuration options, see the MongoDB Atlas Terraform Provider documentation.

For production deployments, consider the official MongoDB Atlas Cluster module, which bundles the cluster resource with production best-practice defaults.

Note

Flex clusters don't support all configurations available with the MongoDB Atlas Terraform provider. For a list of unsupported configurations, see the Atlas documentation.

7

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

1
2

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

terraform init
3

The following output shows what happens when you apply the Terraform configuration:

terraform plan

Proceed to the next step if the information is correct.

4

Type yes when prompted to confirm that you want to apply the configuration.

Note

The terraform apply command might take several minutes to complete.

The following output indicates that your Atlas cluster is created. You can create a connection to this deployment using the VS Code Extension with the displayed connection string:

terraform apply

Warning

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:

1
2
terraform destroy

Type yes when prompted to confirm that you want to destroy the resources defined in the configuration.

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.