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.
Prerequisites
Before you use the Atlas template for Terraform files included with the MongoDB Extension for Visual Studio Code, you must:
Have an Atlas account.
Have an Atlas organization.
Have a service account in an |service| organization with the
Organization OwnerorOrganization Project Creatorrole.
Procedures
Create an Atlas Terraform File using the Template
Use the Atlas template for Terraform files included with the VS Code Extension to configure an Atlas cluster:
In the main.tf file, type atlas, then press the Enter or Return key.
The MongoDB Extension for VSCode populates the file with an example configuration using the MongoDB Atlas Terraform provider to create a Flex cluster.
Update the Atlas Terraform configuration to configure your cluster.
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 |
|---|---|
| Name of the Atlas project that this configuration creates. |
| Name of the Atlas Flex cluster that this configuration creates. |
| Provider on which the Atlas Flex cluster that this configuration creates is hosted. Choose one of the following accepted values:
|
| 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: |
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:
Variable | Value |
|---|---|
| Atlas service account client ID. |
| Atlas service account client secret. |
| Atlas organization ID in which you want to create a project. |
| Username of the MongoDB database user that Atlas creates for your cluster. |
| Password for the MongoDB database user named in |
| 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:
Define the
localvariables in an input variables file.variable "mongodb_atlas_client_id" { default = "my-client-id" } variable "mongodb_atlas_client_secret" { default = "my-client-secret" } Exclude the input variables file from your repository. For example, add the filename to the
.gitignorefile for your repository.Reference variables from the input variables file in the
main.tffile by prefacing them withvar..provider "mongodbatlas" { client_id = var.mongodb_atlas_client_id client_secret = var.mongodb_atlas_client_secret }
Add optional configuration options to the main.tf file.
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.
Create the Atlas Cluster using Terraform
After you create a Terraform file using the template, create the Atlas cluster:
Run the terraform apply command to create an Atlas cluster from the Terraform configuration.
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
Delete the Atlas Cluster using Terraform
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:
Run the terraform destroy command to delete the resources defined in the configuration.
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.