您可以使用 Terraform 通过Atlas Terraform提供商以代码形式管理MongoDB Atlas基础架构。 Atlas Terraform提供商通过简化以代码形式预配、管理和控制Atlas基础架构的进程,实现基础架构部署的自动化。
本指南介绍了如何在现有的 MongoDB Atlas 组织中创建和销毁 Atlas 集群。
注意
Atlas Terraform 提供商支持您的项目和区域中 Atlas 支持的任何 MongoDB 版本。要学习如何更改集群的 MongoDB 版本,请参阅 升级主要版本。
配置好测试环境后,请继续使用来自MongoDB和 HashiCorp 的额外资源对 Terraform 托管的基础架构进行试验。
Atlas Infinite 集群支持 Atlas Terraform 提供商。要了解更多信息,请参阅 使用 Terraform 创建 Atlas Infinite 集群 和 MongoDB Atlas Infinite:概述。
先决条件
在使用 Terraform 部署MongoDB Atlas之前,您必须:
创建Atlas帐户。
获取您作为 的 组织ID
Organization Owner。提示
您可以在 Atlas 用户界面中组织的 General Settings 下找到您的组织 ID。
配置 服务帐户 (SA) 身份验证。查看组织 ID、管理服务帐户以及创建或删除集群和其他项目资源需要 Organization Owner 角色。
使用 Terraform 部署MongoDB Atlas
填充支持文件。
将以下内容添加到 provider.tf:
provider "mongodbatlas" { client_id = var.client_id client_secret = var.client_secret}将以下内容添加到 variables.tf:
variable "client_id" { description = "Atlas Service Account client ID" type = string}
variable "client_secret" { description = "Atlas Service Account client secret" type = string sensitive = true}
variable "org_id" { description = "Atlas organization ID" type = string}将以下内容添加到 versions.tf:
terraform { required_providers { mongodbatlas = { source = "mongodb/mongodbatlas" version = "~> 2.2" } } required_version = "~> 1.0"}注意
您的 Atlas 服务账户客户端 ID 和客户端密钥属于机密信息。考虑将它们存储为 环境变量。
配置main.tf 内容。
注意
以下示例使用 项目 模块 来创建项目,并使用 mongodbatlas_advanced_cluster 资源来创建 Free 集群。
将以下内容添加到您的 main.tf 文件中:
module "project" { source = "terraform-mongodbatlas-modules/project/mongodbatlas" name = "tenant-example-project" org_id = var.org_id}
resource "mongodbatlas_advanced_cluster" "this" { project_id = module.project.id name = "TenantCluster" cluster_type = "REPLICASET"
replication_specs = [ { region_configs = [ { provider_name = "TENANT" backing_provider_name = "AWS" region_name = "US_EAST_1" priority = 7 electable_specs = { instance_size = "M0" } } ] } ]}如果您使用其他云提供商或区域,可以更新以下字段以匹配您的环境。
字段 | 新值 |
|---|---|
| 您的提供商。 可能的值为: |
| 有关您可以使用的所有区域,请参阅云提供商和区域。 |
重要
在创建专用集群时,您可以设置 database_edition 资源上的 mongodbatlas_advanced_cluster。之后无法更改,也无法将专用集群扩展回免费集群或 Flex 集群。database_edition 不适用于免费集群(例如本示例中的 M0 集群)和 Flex 集群。
mongodbatlas_advanced_cluster 资源以及单数 (mongodbatlas_advanced_cluster) 和复数 (mongodbatlas_advanced_clusters) 数据源都公开了 database_edition 和只读 effective_database_edition 属性。由于 database_edition 是可选的,请使用 effective_database_edition 来确认 Atlas 分配的版本,即使您自己没有设置它。
要创建专用的 Atlas Infinite 集群,请参阅 使用 Terraform 创建 Atlas Infinite 集群。
使用 Terraform 创建 Atlas Infinite 集群
以下示例使用 Terraform 创建启用了计算自动伸缩的 Atlas Infinite 集群:
resource "mongodbatlas_advanced_cluster" "infinite" { project_id = module.project.id name = "InfiniteCluster" cluster_type = "REPLICASET" database_edition = "INFINITE"
replication_specs = [ { region_configs = [ { provider_name = "AWS" priority = 7 region_name = "US_EAST_1" electable_specs = { instance_size = "M10" node_count = 2 } auto_scaling = { compute_enabled = true compute_max_instance_size = "M60_GEN_2" storage_config = { shard_size_limit_gb = 10240 } } } ] } ]}使用 Terraform 创建 Atlas Infinite 集群时,适用以下要求和限制:
必需设置:在
mongodbatlas_advanced_cluster资源上将database_edition设置为"INFINITE",并在auto_scaling下设置显式的compute_enabled值(true或false)。Atlas 在创建集群时需要此值。不可变数据库版本:集群创建后,您无法更改其数据库版本。
公共预览版限制:所有其他 Atlas Infinite 公共预览版限制均适用于您使用 Terraform 部署的集群。要了解更多信息,请参阅公共预览版中不支持的功能。
集群类型:Dedicated Atlas Infinite 集群仅支持
REPLICASET集群类型。自动伸缩最大层级:当
compute_enabled为true时,需要compute_max_instance_size。M30层级及以上的 Atlas Infinite 集群在 Amazon Web Services Gen2 硬件上运行,因此请将_GEN_2附加到层级名称中,例如M60_GEN_2。要了解更多信息,请参阅 Gen2 专用集群。可选举节点:Atlas Infinite 集群有两个可选举计算节点,一个主节点 (primary node in the replica set)和一个备用节点,因此将
node_count设置为2。要了解更多信息,请参阅复制和故障转移。只读节点和分析节点:要提供更多读取服务或将分析查询与操作工作负载隔离开来,请在同一个
region_configs对象中设置read_only_specs或analytics_specs以及electable_specs。要了解更多信息,请参阅 Atlas Infinite 集群上的工作负载隔离性。存储限制:仅当您包含
storage_config时才需要storage_config.shard_size_limit_gb。如果您省略storage_config,Atlas 将应用默认的最大存储限制。如要了解更多信息,请参阅最大存储限制。
注意
storage_config.shard_size_limit_gb 设置集群可以存储的最大数据量。要了解更多信息,请参阅 Terraform 提供商文档。
验证部署
terraform apply 完成后,确认集群已部署。
新集群会出现在项目下的 Atlas 用户界面中。
如果已将输出值添加到
main.tf,则连接字符串会出现在 Terraform 输出中。
终止MongoDB Atlas实例
要删除在 Terraform目录中创建的所有资源,请运行以下命令:
terraform destroy警告
如果删除了所有资源,则无法恢复。
当出现Do you really want to destroy all resources?提示时,输入yes 。
后续步骤
部署集群后,您可以:
扩展到专用集群:查看集群模块和Atlas Terraform 提供商 文档以获取更完整的配置。
连接到集群:使用集群对象或 Terraform 输出值中的连接字符串。
其他资源: