使用 Terraform 从模板创建 Atlas 集群
本页概述了如何使用 MongoDB Extension for Visual Studio Code 中包含的用于 Terraform 文件的 Atlas 模板来创建 Shared Tier Atlas 集群。
加载模板后,您可以配置集群并提供有关 Atlas 帐户的详细信息。然后,您可以使用 Terraform 命令规划、应用和销毁 Atlas 集群。
先决条件
在使用 MongoDB Extension for Visual Studio Code 中包含的用于 Terraform 文件的 Atlas 模板之前,您必须:
拥有 Atlas 账户。
有一个 Atlas 组织。
在具有
Organization Owner
或Organization Project Creator
角色的组织中拥有 API 密钥。
步骤
使用模板创建 Atlas Terraform 文件
使用 VS Code 扩展中包含的用于 Terraform 文件的 Atlas 模板来配置 Atlas 集群:
在 main.tf
文件中,键入 atlas
,然后按 Enter 或 Return 键。
MongoDB Extension for VSCode 通过使用 MongoDB Atlas Terraform 提供程序创建共享层 Atlas 集群的示例配置填充文件。
更新 Atlas Terraform 配置以配置集群。
移动游标以选择 mongodbatlas_project
资源的 name
参数的值。
为以下参数提供值以便配置集群:
提示
按 Tab 键选择模板中应更新的下一个参数。
属性 | 值 |
---|---|
mongodbatlas_project .name | 此配置创建的 Atlas 项目的名称。 |
mongodbatlas_cluster .name | 此配置创建的 Atlas 集群的名称。 |
mongodbatlas_cluster .backing_provider_name | 用于托管此配置创建的 Atlas 集群的提供商。选择以下可被接受的值之一:
|
mongodbatlas_cluster .provider_region_name | 部署此配置创建的 Atlas 集群的区域。确保所选区域支持您想部署的 Atlas 集群的实例规格。 默认情况下,模板预配共享层集群: 有关每个提供商和区域所支持的实例规格的详细信息,请参阅 Atlas 文档中的以下部分: |
mongodbatlas_cluster .provider_instance_size_name | 此配置创建的 Atlas 集群的实例规格。 你可以:
有关每个提供商和区域所支持的实例规格的详细信息,请参阅 Atlas 文档中的以下部分: |
mongodbatlas_cluster .disk_size_gbs | 此配置创建的 Atlas 集群所需的磁盘空间。确保您提供的值等于或小于所选实例规格信息中的磁盘最大容量。
有关每个提供商所支持的每种实例规格对应的磁盘大小详情,请参阅 Atlas 文档中的以下部分: |
更新 local
变量。
警告
local
变量包含敏感信息。不要将这些值签入公开可用的存储库。
为以下 local
变量提供值:
变量 | 值 |
---|---|
| Atlas 公用 API 密钥。 |
| Atlas 私有 API 密钥。 |
| 要在其中创建项目的 Atlas 组织 ID。 |
| Atlas 为您集群创建的 MongoDB 数据库用户的用户名。 |
| 在 |
| 可访问您 Atlas 集群的 IP 地址或 CIDR 区块。 |
例子
使用输入变量文件最大限度地提高安全性
为了最大限度地提高安全性,请考虑采取以下步骤:
在输入变量文件中定义
local
变量。variable "mongodb_atlas_api_pub_key" { default = "my-public-key" } variable "mongodb_atlas_api_pri_key" { default = "my-private-key" } 排除存储库中的输入变量文件。例如,将文件名添加到存储库的
.gitignore
文件中。引用
main.tf
文件中输入变量文件中的变量,方法是使用vars.
作为前缀。provider "mongodbatlas" { public_key = vars.mongodb_atlas_api_pub_key private_key = vars.mongodb_atlas_api_pri_key }
向 main.tf
文件添加可选的配置选项。
有关支持的配置选项的完整列表,请参阅 MongoDB Atlas Terraform Provider 文档。
注意
共享层 Atlas 集群不支持 MongoDB Atlas Terraform 提供程序提供的所有配置。有关不支持的配置列表,请参阅 Atlas 文档。
使用 Terraform 创建 Atlas 集群
使用模板创建 Terraform 文件后,创建 Atlas 集群:
运行 terraform init
命令以安装所需的提供者。
terraform init
以下输出表明 MongoDB Atlas Terraform Provider 已安装并可供使用:
1 Initializing the backend... 2 3 Initializing provider plugins... 4 - Checking for available provider plugins... 5 - Downloading plugin for provider "mongodbatlas" (terraform-providers/mongodbatlas) 0.5.1... 6 7 The following providers do not have any version constraints in configuration, 8 so the latest version was installed. 9 10 To prevent automatic upgrades to new major versions that may contain breaking 11 changes, it is recommended to add version = "..." constraints to the 12 corresponding provider blocks in configuration, with the constraint strings 13 suggested below. 14 15 * provider.mongodbatlas: version = "~> 0.5" 16 17 Terraform has been successfully initialized!
运行 terraform plan
命令以查看应用配置时发生的情况。
terraform plan
以下输出显示了应用 Terraform 配置时所出现的情况:
1 Refreshing Terraform state in-memory prior to plan... 2 The refreshed state will be used to calculate this plan, but will not be 3 persisted to local or remote state storage. 4 5 6 ------------------------------------------------------------------------ 7 8 An execution plan has been generated and is shown below. 9 Resource actions are indicated with the following symbols: 10 + create 11 12 Terraform 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 110 Plan: 4 to add, 0 to change, 0 to destroy. 111 112 ------------------------------------------------------------------------ 113 114 Note: You didn't specify an "-out" parameter to save this plan, so Terraform 115 can't guarantee that exactly these actions will be performed if 116 "terraform apply" is subsequently run.
如果信息正确,请继续执行下一步。
运行 terraform apply
命令,以便从 Terraform 配置中创建一个 Atlas 集群。
terraform apply
当系统提示你确认要应用的配置时,键入 yes
。
注意
terraform apply
命令可能需要几分钟才能完成。
以下输出表示 Atlas 集群已创建。您可以使用 VS Code 扩展以及显示的连接字符串创建与此部署的连接:
1 Apply complete! Resources: 4 added, 0 changed, 0 destroyed. 2 3 Outputs: 4 5 connection_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 集群
警告
删除集群会破坏其中存储的数据库、集合和文档,以及配置集群的 Terraform 配置中定义的所有其他资源。
请谨慎操作。
要删除 Atlas 集群: