Docs 菜单

使用 Terraform 从模板创建 Atlas 集群

在此页面上

本页概述了如何使用 MongoDB Extension for Visual Studio Code 中包含的用于 Terraform 文件的 Atlas 模板来创建 Shared Tier Atlas 集群。

加载模板后,您可以配置集群并提供有关 Atlas 帐户的详细信息。然后,您可以使用 Terraform 命令规划、应用和销毁 Atlas 集群。

在使用 MongoDB Extension for Visual Studio Code 中包含的用于 Terraform 文件的 Atlas 模板之前,您必须:

使用 VS Code 扩展中包含的用于 Terraform 文件的 Atlas 模板来配置 Atlas 集群:

1
操作系统
方法
操作

Any

Visual Studio Code 菜单

File > New File

macOS

键盘快捷键

Command + N

Windows 和 Linux

键盘快捷键

Control + N

2

注意

main.tf 是用作 Terraform 模块入口点的推荐文件名。

3

MongoDB Extension for VSCode 通过使用 MongoDB Atlas Terraform 提供程序创建共享层 Atlas 集群的示例配置填充文件。

4

移动游标以选择 mongodbatlas_project 资源的 name 参数的值。

为以下参数提供值以便配置集群:

提示

Tab 键选择模板中应更新的下一个参数。

属性
mongodbatlas_project
.name

此配置创建的 Atlas 项目的名称。

mongodbatlas_cluster
.name

此配置创建的 Atlas 集群的名称。

mongodbatlas_cluster
.backing_provider_name

用于托管此配置创建的 Atlas 集群的提供商。选择以下可被接受的值之一:

  • AWS

  • AZURE

  • GCP

mongodbatlas_cluster
.provider_region_name

部署此配置创建的 Atlas 集群的区域。确保所选区域支持您想部署的 Atlas 集群的实例规格。

默认情况下,模板预配共享层集群:M2M5

有关每个提供商和区域所支持的实例规格的详细信息,请参阅 Atlas 文档中的以下部分:

mongodbatlas_cluster
.provider_instance_size_name

此配置创建的 Atlas 集群的实例规格。

你可以:

  • 选择以下模板中包含的某一共享层实例大小:M2M5

  • 输入 Atlas 对您所选提供商与区域提供支持的其他实例规格。

有关每个提供商和区域所支持的实例规格的详细信息,请参阅 Atlas 文档中的以下部分:

mongodbatlas_cluster
.disk_size_gbs

此配置创建的 Atlas 集群所需的磁盘空间。确保您提供的值等于或小于所选实例规格信息中的磁盘最大容量。

  • 对于 M2 集群,输入 2

  • 对于 M5 集群,输入 5

有关每个提供商所支持的每种实例规格对应的磁盘大小详情,请参阅 Atlas 文档中的以下部分:

5

警告

local 变量包含敏感信息。不要将这些值签入公开可用的存储库。

为以下 local 变量提供值:

变量

mongodb_atlas_api_pub_key

Atlas 公用 API 密钥。

mongodb_atlas_api_pri_key

Atlas 私有 API 密钥。

mongodb_atlas_org_id

要在其中创建项目的 Atlas 组织 ID。

mongodb_atlas_database_username

Atlas 为您集群创建的 MongoDB 数据库用户的用户名。

mongodb_atlas_database_user_password

mongodb_atlas_database_username 中命名的 MongoDB 数据库用户的密码。

mongodb_atlas_whitelistip

可访问您 Atlas 集群的 IP 地址或 CIDR 区块。

例子

使用输入变量文件最大限度地提高安全性

为了最大限度地提高安全性,请考虑采取以下步骤:

  1. 输入变量文件中定义 local 变量。

    variable "mongodb_atlas_api_pub_key" {
    default = "my-public-key"
    }
    variable "mongodb_atlas_api_pri_key" {
    default = "my-private-key"
    }
  2. 排除存储库中的输入变量文件。例如,将文件名添加到存储库的 .gitignore 文件中。

  3. 引用 main.tf 文件中输入变量文件中的变量,方法是使用 vars. 作为前缀。

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

有关支持的配置选项的完整列表,请参阅 MongoDB Atlas Terraform Provider 文档

注意

共享层 Atlas 集群不支持 MongoDB Atlas Terraform 提供程序提供的所有配置。有关不支持的配置列表,请参阅 Atlas 文档

7

使用模板创建 Terraform 文件后,创建 Atlas 集群:

1
2
terraform init

以下输出表明 MongoDB Atlas Terraform Provider 已安装并可供使用:

1Initializing the backend...
2
3Initializing provider plugins...
4- Checking for available provider plugins...
5- Downloading plugin for provider "mongodbatlas" (terraform-providers/mongodbatlas) 0.5.1...
6
7The following providers do not have any version constraints in configuration,
8so the latest version was installed.
9
10To prevent automatic upgrades to new major versions that may contain breaking
11changes, it is recommended to add version = "..." constraints to the
12corresponding provider blocks in configuration, with the constraint strings
13suggested below.
14
15* provider.mongodbatlas: version = "~> 0.5"
16
17Terraform has been successfully initialized!
3
terraform plan

以下输出显示了应用 Terraform 配置时所出现的情况:

1Refreshing Terraform state in-memory prior to plan...
2The refreshed state will be used to calculate this plan, but will not be
3persisted to local or remote state storage.
4
5
6------------------------------------------------------------------------
7
8An execution plan has been generated and is shown below.
9Resource actions are indicated with the following symbols:
10 + create
11
12Terraform will perform the following actions:
13
14 # mongodbatlas_cluster.my_cluster will be created
15 + resource "mongodbatlas_cluster" "my_cluster" {
16 + advanced_configuration = (known after apply)
17 + auto_scaling_disk_gb_enabled = false
18 + backing_provider_name = "AWS"
19 + backup_enabled = false
20 + bi_connector = (known after apply)
21 + cluster_id = (known after apply)
22 + cluster_type = (known after apply)
23 + connection_strings = (known after apply)
24 + disk_size_gb = 2
25 + encryption_at_rest_provider = (known after apply)
26 + id = (known after apply)
27 + mongo_db_major_version = "4.2"
28 + mongo_db_version = (known after apply)
29 + mongo_uri = (known after apply)
30 + mongo_uri_updated = (known after apply)
31 + mongo_uri_with_options = (known after apply)
32 + name = "atlasClusterName"
33 + num_shards = 1
34 + paused = (known after apply)
35 + pit_enabled = (known after apply)
36 + project_id = (known after apply)
37 + provider_backup_enabled = false
38 + provider_disk_iops = (known after apply)
39 + provider_disk_type_name = (known after apply)
40 + provider_encrypt_ebs_volume = (known after apply)
41 + provider_instance_size_name = "M2"
42 + provider_name = "TENANT"
43 + provider_region_name = "providerRegionName"
44 + provider_volume_type = (known after apply)
45 + replication_factor = (known after apply)
46 + snapshot_backup_policy = (known after apply)
47 + srv_address = (known after apply)
48 + state_name = (known after apply)
49
50 + labels {
51 + key = (known after apply)
52 + value = (known after apply)
53 }
54
55 + replication_specs {
56 + id = (known after apply)
57 + num_shards = (known after apply)
58 + zone_name = (known after apply)
59
60 + regions_config {
61 + analytics_nodes = (known after apply)
62 + electable_nodes = (known after apply)
63 + priority = (known after apply)
64 + read_only_nodes = (known after apply)
65 + region_name = (known after apply)
66 }
67 }
68 }
69
70 # mongodbatlas_database_user.my_user will be created
71 + resource "mongodbatlas_database_user" "my_user" {
72 + auth_database_name = "admin"
73 + id = (known after apply)
74 + password = (sensitive value)
75 + project_id = (known after apply)
76 + username = "jww"
77 + x509_type = "NONE"
78
79 + labels {
80 + key = (known after apply)
81 + value = (known after apply)
82 }
83
84 + roles {
85 + collection_name = (known after apply)
86 + database_name = "admin"
87 + role_name = "atlasAdmin"
88 }
89 }
90
91 # mongodbatlas_project.my_project will be created
92 + resource "mongodbatlas_project" "my_project" {
93 + cluster_count = (known after apply)
94 + created = (known after apply)
95 + id = (known after apply)
96 + name = "atlasProjectName"
97 + org_id = "5d3716bfcf09a21576d7983e"
98 }
99
100 # mongodbatlas_project_ip_whitelist.my_ipaddress will be created
101 + resource "mongodbatlas_project_ip_whitelist" "my_ipaddress" {
102 + aws_security_group = (known after apply)
103 + cidr_block = (known after apply)
104 + comment = "My IP Address"
105 + id = (known after apply)
106 + ip_address = "204.210.139.18"
107 + project_id = (known after apply)
108 }
109
110Plan: 4 to add, 0 to change, 0 to destroy.
111
112------------------------------------------------------------------------
113
114Note: You didn't specify an "-out" parameter to save this plan, so Terraform
115can't guarantee that exactly these actions will be performed if
116"terraform apply" is subsequently run.

如果信息正确,请继续执行下一步。

4
terraform apply

当系统提示你确认要应用的配置时,键入 yes

注意

terraform apply 命令可能需要几分钟才能完成。

以下输出表示 Atlas 集群已创建。您可以使用 VS Code 扩展以及显示的连接字符串创建与此部署的连接

1Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
2
3Outputs:
4
5connection_strings = [
6 [
7 {
8 "aws_private_link" = {}
9 "aws_private_link_srv" = {}
10 "private" = ""
11 "private_srv" = ""
12 "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"
13 "standard_srv" = "mongodb+srv://myCluster-xxxxx.mongodb.net"
14 },
15 ],
16]

警告

删除集群会破坏其中存储的数据库、集合和文档,以及配置集群的 Terraform 配置中定义的所有其他资源。

请谨慎操作。

要删除 Atlas 集群:

1
2
terraform destroy

当系统提示你确认是否要销毁配置中定义的资源时,键入 yes

注意

terraform destroy 命令可能需要几分钟才能完成。

以下输出表示 Atlas 集群和其所有的关联资源均已删除:

Destroy complete! Resources: 4 destroyed.

在此页面上