Docs Home → MongoDB for VS Code
Create an Atlas Cluster from a Template using Terraform使用Terraform从模板创建Atlas集群
On this page本页内容
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模板之前,您必须:
Install MongoDB for VS Code安装MongoDB for VS CodeInstall Terraform安装Terraform- Have an
Atlas account.Atlas帐户。 - Have an
Atlas organization.Atlas组织。 Have an API key in an organization with the在具有Organization Owner
orOrganization Project Creator
role.Organization Owner
或Organization Project Creator
角色的组织中拥有API密钥。
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集群:
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 AtlasVSCode的MongoDB扩展使用MongoDB Atlas Terraform provider to create a Shared Tier Atlas cluster.
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:为以下参数提供值以配置群集:
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.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
变量的值:
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数据库用户的密码。 |
mongodb_atlas_whitelistip |
Use an Input Variables File to Maximize security使用输入变量文件最大限度地提高安全性
To maximize security, consider taking the following steps:为了最大限度地提高安全性,请考虑采取以下步骤:
Define the在输入变量文件local
variables in an input variablesfile.
中定义
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例如,将文件名添加到存储库的.gitignore
file for your repository..gitignore
文件中。Reference variables from the input variables file in the引用main.tf
file by prefacing them withvars.
.main.tf
文件中输入变量文件中的变量,方法是在它们前面加上vars.
。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文档。
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文档。
Create the Atlas Cluster using Terraform使用Terraform创建Atlas Cluster
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_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.如果信息正确,请继续下一步。
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
。
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集群
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
。
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.