Docs HomeMongoDB for VS Code

Create an Atlas Cluster from a Template using Terraform使用Terraform从模板创建Atlas集群

This page outlines how to use the Atlas template for Terraform files included with the MongoDB Extension for Visual Studio Code to create Shared Tier Atlas clusters.本页概述了如何使用Visual Studio CodeMongoDB扩展中包含的Terraform文件的Atlas模板来创建共享层Atlas集群。

After loading the template, you configure the cluster and provide details about your Atlas account. 加载模板后,您可以配置集群并提供有关Atlas帐户的详细信息。You then use Terraform commands to plan, apply, and destroy your Atlas cluster.然后使用Terraform命令来规划、应用和销毁Atlas集群。

Prerequisites过程

Before you use the Atlas template for Terraform files included with the MongoDB Extension for Visual Studio Code, you must:在使用Visual Studio CodeMongoDB扩展附带的Terraform文件的Atlas模板之前,您必须:

Procedures过程

Create an Atlas Terraform File using the Template使用模板创建Atlas地形文件

Use the Atlas template for Terraform files included with the MongoDB for VS Code to configure an Atlas cluster:使用MongoDB for VS Code中包含的Terraform文件的Atlas模板来配置Atlas集群:

1

In Visual Studio Code, create a new file.在Visual Studio Code中,创建一个新文件。

Operating System操作系统Method方法Actions操作
AnyVisual Studio Code MenuFile > New File
MacOSKeyboard ShortcutPress Command + N
Windows and LinuxKeyboard ShortcutPress Control + N
2

Save the file. Name it main.tf.保存文件。将其命名为main.tf

Note

main.tf is the recommended filename for the entry point to a Terraform module.是Terraform模块入口点的推荐文件名。

3

In the main.tf file, type atlas, then press the Enter or Return key.main.tf文件中,键入atlas,然后按Enter或Return键。

The MongoDB Extension for VSCode populates the file with an example configuration using the MongoDB Atlas Terraform provider to create a Shared Tier Atlas cluster.VSCode的MongoDB扩展使用MongoDB Atlas Terraform提供程序的示例配置填充文件,以创建共享层Atlas集群。

4

Update the Atlas Terraform configuration to configure your cluster.更新Atlas Terraform配置以配置集群。

The cursor moves to select the value of the name argument of the mongodbatlas_project resource.游标移动以选择mongodbatlas_project资源的name参数的值。

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.Tab键选择模板中应更新的下一个参数。

Attribute属性Value
mongodbatlas_project
.name
Name of the Atlas project that this configuration creates.此配置创建的Atlas项目的名称。
mongodbatlas_cluster
.name
Name of the Atlas cluster that this configuration creates.此配置创建的Atlas集群的名称。
mongodbatlas_cluster
.backing_provider_name
Provider on which the Atlas cluster that this configuration creates is hosted. Choose one of the following accepted values:托管此配置创建的Atlas集群的提供程序。选择以下可接受的值之一:
  • AWS
  • AZURE
  • GCP
mongodbatlas_cluster
.provider_region_name

Region to which the Atlas cluster that this configuration creates is deployed. 此配置创建的Atlas集群部署到的区域。Ensure that the region you chose supports Atlas clusters of the instance size you want to deploy.确保您选择的区域支持要部署的实例大小的Atlas集群。
By default the template provisions Shared Tier clusters:默认情况下,模板规定共享层集群: M2 and M5.
For details about the instance sizes that each provider and region supports, see the following sections in the Atlas documentation:有关每个提供程序和区域支持的实例大小的详细信息,请参阅Atlas文档中的以下部分:

mongodbatlas_cluster
.provider_instance_size_name

Instance size of the Atlas cluster that this configuration creates.此配置创建的Atlas集群的实例大小。
Either:两者之一:

  • Choose one of the Shared Tier instance sizes included in the template: M2 or M5.选择模板中包含的共享层实例大小之一:M2M5
  • Enter another instance size that Atlas supports for your chosen provider and region.输入Atlas为您选择的提供商和地区支持的另一个实例大小。

For details about the instance sizes that each provider and region supports, see the following sections in the Atlas documentation:有关每个提供程序和区域支持的实例大小的详细信息,请参阅Atlas文档中的以下部分:

mongodbatlas_cluster
.disk_size_gbs

Disk size of the Atlas cluster that this configuration creates. 此配置创建的Atlas集群的磁盘大小。Ensure that you provide a value that is equal to or less than the maximum disk size for the instance size you chose.确保提供的值等于或小于所选实例大小的最大磁盘大小。

  • For M2 clusters, enter 2.
  • For M5 clusters, enter 5.

For details about the disk sizes that each provider supports for each instance size, see the following sections in the Atlas documentation:有关每个提供程序支持的每个实例大小的磁盘大小的详细信息,请参阅Atlas文档中的以下部分:

5

Update the local variables.更新local变量。

Warning

The local variables contain sensitive information. local变量包含敏感信息。Do not check these values in to a repository that is available publicly.不要将这些值签入到公开可用的存储库中。

Provide values for the following local variables:提供以下local变量的值:

Variable变量Value
mongodb_atlas_api_pub_keyAtlas public API key.Atlas公共API密钥。
mongodb_atlas_api_pri_keyAtlas private API key.Atlas私有API密钥。
mongodb_atlas_org_idAtlas organization ID in which you want to create a project.要在其中创建项目的Atlas组织ID。
mongodb_atlas_database_usernameUsername of the MongoDB database user that Atlas creates for your cluster.Atlas为集群创建的MongoDB数据库用户的用户名。
mongodb_atlas_database_user_passwordPassword for the MongoDB database user named in mongodb_atlas_database_username.mongodb_atlas_database_username中命名的MongoDB数据库用户的密码。
mongodb_atlas_whitelistipIP address or CIDR block from which your Atlas cluster is accessible.可以访问Atlas集群的IP地址或CIDR块。
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.输入变量文件中定义local变量。

    variable "mongodb_atlas_api_pub_key" {
    default = "my-public-key"
    }

    variable "mongodb_atlas_api_pri_key" {
    default = "my-private-key"
    }
  2. Exclude the input variables file from your repository. 从存储库中排除输入变量文件。For example, add the filename to the .gitignore file for your repository.例如,将文件名添加到存储库的.gitignore文件中。
  3. Reference variables from the input variables file in the main.tf file by prefacing them with vars..引用main.tf文件中输入变量文件中的变量,方法是在它们前面加上vars.

    provider "mongodbatlas" {
    public_key = vars.mongodb_atlas_api_pub_key
    private_key = vars.mongodb_atlas_api_pri_key
    }
6

Add optional configuration options to the main.tf file.将可选配置选项添加到main.tf文件中。

For a complete list of supported configuration options, see the MongoDB Atlas Terraform Provider documentation.有关支持的配置选项的完整列表,请参阅MongoDB Atlas Terraform Provider文档

Note

Shared Tier Atlas clusters don't support all configurations available with the MongoDB Atlas Terraform provider. Shared Tier Atlas集群不支持MongoDB Atlas Terraform提供商提供的所有配置。For a list of unsupported configurations, see the Atlas documentation.有关不支持的配置的列表,请参阅Atlas文档

7

Save the main.tf file.保存main.tf文件。

Create the Atlas Cluster using Terraform使用Terraform创建Atlas Cluster

After you create a Terraform file using the template, create the Atlas cluster:使用模板创建Terraform文件后,创建Atlas群集:

1

Navigate to the directory in which you saved your main.tf file.导航到保存main.tf文件的目录。

2

Run the terraform init command to install the required providers.运行terraform init命令以安装所需的提供程序。

terraform init

The following output indicates that the MongoDB Atlas Terraform Provider is installed and ready for use:以下输出表明MongoDB Atlas Terraform Provider已安装并可供使用:

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "mongodbatlas" (terraform-providers/mongodbatlas) 0.5.1...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.mongodbatlas: version = "~> 0.5"

Terraform has been successfully initialized!
3

Run the terraform plan command to view what happens when you apply the configuration.运行terraform plan命令以查看应用配置时发生的情况。

terraform plan

The following output shows what happens when you apply the Terraform configuration:以下输出显示了应用Terraform配置时发生的情况:

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# mongodbatlas_cluster.my_cluster will be created
+ resource "mongodbatlas_cluster" "my_cluster" {
+ advanced_configuration = (known after apply)
+ auto_scaling_disk_gb_enabled = false
+ backing_provider_name = "AWS"
+ backup_enabled = false
+ bi_connector = (known after apply)
+ cluster_id = (known after apply)
+ cluster_type = (known after apply)
+ connection_strings = (known after apply)
+ disk_size_gb = 2
+ encryption_at_rest_provider = (known after apply)
+ id = (known after apply)
+ mongo_db_major_version = "4.2"
+ mongo_db_version = (known after apply)
+ mongo_uri = (known after apply)
+ mongo_uri_updated = (known after apply)
+ mongo_uri_with_options = (known after apply)
+ name = "atlasClusterName"
+ num_shards = 1
+ paused = (known after apply)
+ pit_enabled = (known after apply)
+ project_id = (known after apply)
+ provider_backup_enabled = false
+ provider_disk_iops = (known after apply)
+ provider_disk_type_name = (known after apply)
+ provider_encrypt_ebs_volume = (known after apply)
+ provider_instance_size_name = "M2"
+ provider_name = "TENANT"
+ provider_region_name = "providerRegionName"
+ provider_volume_type = (known after apply)
+ replication_factor = (known after apply)
+ snapshot_backup_policy = (known after apply)
+ srv_address = (known after apply)
+ state_name = (known after apply)

+ labels {
+ key = (known after apply)
+ value = (known after apply)
}

+ replication_specs {
+ id = (known after apply)
+ num_shards = (known after apply)
+ zone_name = (known after apply)

+ regions_config {
+ analytics_nodes = (known after apply)
+ electable_nodes = (known after apply)
+ priority = (known after apply)
+ read_only_nodes = (known after apply)
+ region_name = (known after apply)
}
}
}

# mongodbatlas_database_user.my_user will be created
+ resource "mongodbatlas_database_user" "my_user" {
+ auth_database_name = "admin"
+ id = (known after apply)
+ password = (sensitive value)
+ project_id = (known after apply)
+ username = "jww"
+ x509_type = "NONE"

+ labels {
+ key = (known after apply)
+ value = (known after apply)
}

+ roles {
+ collection_name = (known after apply)
+ database_name = "admin"
+ role_name = "atlasAdmin"
}
}

# mongodbatlas_project.my_project will be created
+ resource "mongodbatlas_project" "my_project" {
+ cluster_count = (known after apply)
+ created = (known after apply)
+ id = (known after apply)
+ name = "atlasProjectName"
+ org_id = "5d3716bfcf09a21576d7983e"
}

# mongodbatlas_project_ip_whitelist.my_ipaddress will be created
+ resource "mongodbatlas_project_ip_whitelist" "my_ipaddress" {
+ aws_security_group = (known after apply)
+ cidr_block = (known after apply)
+ comment = "My IP Address"
+ id = (known after apply)
+ ip_address = "204.210.139.18"
+ project_id = (known after apply)
}

Plan: 4 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Proceed to the next step if the information is correct.如果信息正确,请继续下一步。

4

Run the terraform apply command to create an Atlas cluster from the Terraform configuration.运行terraform apply命令,从terraform配置创建Atlas集群。

terraform apply

Type yes when prompted to confirm that you want to apply the configuration.当系统提示您确认要应用配置时,请键入yes

Note

The terraform apply command might take several minutes to complete.terraform apply命令可能需要几分钟才能完成。

The following output indicates that your Atlas cluster is created. 以下输出表示您的Atlas集群已创建。You can create a connection to this deployment using the MongoDB for VS Code with the displayed connection strings:您可以使用MongoDB for VS Code创建连接到此部署,并显示连接字符串:

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

Outputs:

connection_strings = [
[
{
"aws_private_link" = {}
"aws_private_link_srv" = {}
"private" = ""
"private_srv" = ""
"standard" = "mongodb://myCluster-shard-00-00-xxxxx.mongodb.net:27017,myCluster-shard-00-01-xxxxx.mongodb.net:27017,myCluster-shard-00-02-xxxxx.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=myCluster-shard-0"
"standard_srv" = "mongodb+srv://myCluster-xxxxx.mongodb.net"
},
],
]

Delete the Atlas Cluster using Terraform使用Terraform删除Atlas集群

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.删除集群会破坏存储在集群上的数据库、集合和文档,以及配置集群的Terraform配置中定义的所有其他资源。

Proceed with caution.小心操作。

To delete the Atlas cluster:要删除Atlas群集,请执行以下操作:

1

Navigate to the directory in which you saved your main.tf file.导航到保存main.tf文件的目录。

2

Run the terraform destroy command to install the required providers.运行terraform destroy命令以安装所需的提供程序。

terraform destroy

Type yes when prompted to confirm that you want to destroy the resources defined in the configuration.当系统提示您确认要销毁配置中定义的资源时,请键入yes

Note

The terraform destroy command might take several minutes to complete.terraform destroy命令可能需要几分钟才能完成。

The following output indicates that the Atlas cluster and all associated resources are deleted:以下产出表明Atlas集群和所有相关资源已被删除:

Destroy complete! Resources: 4 destroyed.