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 Code的MongoDB扩展中包含的用于Terraform文件的Atlas模板来创建共享层Atlas集群。
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.加载模板后,您可以配置集群并提供有关Atlas帐户的详细信息。然后,您可以使用Terraform命令来规划、应用和销毁Atlas集群。
Prerequisites先决条件
Before you use the Atlas template for Terraform files included with the MongoDB Extension for Visual Studio Code, you must:在将Atlas模板用于Visual Studio Code的MongoDB扩展中包含的Terraform文件之前,您必须:
Install MongoDB for VS Code Extension为VS Code扩展安装MongoDBInstall Terraform安装TerraformHave an具有Atlas account.Atlas帐户。Have an具有Atlas organization.Atlas组织。Have an API key in an organization with the在具有组织所有者或组织项目创建者角色的组织中拥有API键。Organization OwnerorOrganization Project Creatorrole.
Procedures过程
Create an Atlas Terraform File using the Template使用模板创建Atlas地形文件
Use the Atlas template for Terraform files included with the VS Code Extension to configure an Atlas cluster:使用VS Code扩展中包含的用于Terraform文件的Atlas模板来配置Atlas集群:
In Visual Studio Code, create a new file.在Visual Studio Code中,创建一个新文件。
| Any | Visual Studio Code | |
| MacOS | Command + N | |
| Windows and Linux | Control + N |
Save the file. Name it main.tf.保存文件。将其命名为main.tf。
main.tf.Note
main.tf is the recommended filename for the entry point to a Terraform module.是Terraform模块入口点的推荐文件名。
In the main.tf file, type atlas, then press the Enter or Return key.在main.tf文件中,键入atlas,然后按Enter或Return键。
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 Shared Tier Atlas cluster.VSCode的MongoDB扩展使用MongoDB Atlas Terraform提供程序用示例配置填充文件,以创建共享层Atlas集群。
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键选择模板中应更新的下一个参数。
mongodbatlas_project.name | |
mongodbatlas_cluster.name | |
mongodbatlas_cluster.backing_provider_name |
|
mongodbatlas_cluster.provider_region_name |
|
mongodbatlas_cluster.provider_instance_size_name |
|
mongodbatlas_cluster.disk_size_gbs |
|
Update the local variables.更新local变量。
local variables.Warning
The local variables contain sensitive information. Do not check these values in to a repository that is available publicly.local变量包含敏感信息。不要将这些值签入公开可用的存储库。
Provide values for the following 为以下local variables:local变量提供值:
mongodb_atlas_api_pub_key | |
mongodb_atlas_api_pri_key | |
mongodb_atlas_org_id | |
mongodb_atlas_database_username | |
mongodb_atlas_database_user_password | mongodb_atlas_database_username.mongodb_atlas_database_username。 |
mongodb_atlas_whitelistip |
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.local变量。variable "mongodb_atlas_api_pub_key" {
default = "my-public-key"
}
variable "mongodb_atlas_api_pri_key" {
default = "my-private-key"
}Exclude the input variables file from your repository. For example, add the filename to the从存储库中排除输入变量文件。例如,将文件名添加到存储库的.gitignorefile for your repository..gitignore文件中。Reference variables from the input variables file in the通过在main.tffile by prefacing them withvars..main.tf文件中的输入变量文件前添加变量来引用变量。 .provider "mongodbatlas" {
public_key = vars.mongodb_atlas_api_pub_key
private_key = vars.mongodb_atlas_api_pri_key
}
Add optional configuration options to the main.tf file.将可选配置选项添加到main.tf文件中。
main.tf file.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. 共享层Atlas集群不支持MongoDB Atlas Terraform提供程序提供的所有配置。For a list of unsupported configurations, see the Atlas documentation.有关不支持的配置列表,请参阅Atlas文档。
Create the Atlas Cluster using Terraform使用Terraform创建Atlas集群
After you create a Terraform file using the template, create the Atlas cluster:使用模板创建Terraform文件后,创建Atlas集群:
Run the terraform init command to install the required providers.运行terraform init命令以安装所需的提供程序。
terraform init command to install the required providers.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!Run the terraform plan command to view what happens when you apply the configuration.运行terraform plan命令查看应用配置时会发生什么。
terraform plan command to view what happens when you apply the configuration.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_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.如果信息正确,请继续下一步。
Run the terraform apply command to create an Atlas cluster from the Terraform configuration.运行terraform apply命令,从terraform配置创建Atlas集群。
terraform apply command to create an Atlas cluster from the Terraform configuration.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 VS Code Extension with the displayed connection strings:您可以使用显示的连接字符串使用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集群:
Run the terraform destroy command to install the required providers.运行terraform destroy命令以安装所需的提供程序。
terraform destroy command to install the required providers.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.