对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

开始使用 Terraform 和MongoDB Atlas Provider

您可以使用 Terraform 通过Atlas Terraform提供商以代码形式管理MongoDB Atlas基础架构。 Atlas Terraform提供商通过简化以代码形式预配、管理和控制Atlas基础架构的进程,实现基础架构部署的自动化。

本指南将向您介绍如何在现有MongoDB Atlas组织中创建和销毁Atlas集群。

注意

Atlas Terraform提供商支持Atlas在您的项目和地区中支持的任何MongoDB版本。要学习;了解如何更改集群的MongoDB版本,请参阅升级主要版本。

配置好测试环境后,请继续使用来自MongoDB和 HashiCorp 的额外资源对 Terraform 托管的基础架构进行试验。

Atlas Infinite 集群支持Atlas Terraform提供商。要学习;了解有关Atlas Infinite 的更多信息,请参阅MongoDB Atlas Infinite:概述。

在使用 Terraform 部署MongoDB Atlas之前,您必须:

  • 创建Atlas帐户。

  • 获取您作为 的 组织ID Organization Owner。

    提示

    您可以在 Atlas 用户界面中组织的 General Settings 下找到您的组织 ID。

  • 配置服务帐户 (SA) 身份验证。需要“组织所有者”角色才能查看“组织ID”、管理服务帐户以及创建或删除集群和其他项目资源。

1
mkdir terraform-proj
cd terraform-proj
2

创建main.tf 、 provider.tf 、 variables.tf和versions.tf文件。

touch main.tf provider.tf variables.tf versions.tf
3

将以下内容添加到 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和客户端密钥是密钥。考虑将它们存储为环境变量。

4

注意

以下示例使用项目模块创建项目,并使用 mongodbatlas_advanced_cluster资源创建免费集群。

将以下内容添加到 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"
}
}
]
}
]
}

如果您与不同的云提供商或地区合作,则可以更新以下字段以匹配您的环境。

字段
新值

backing_provider_name

您的提供商。 可能的值为: "AWS" 、 "AZURE"或"GCP" 。

region_name

有关您可以使用的所有区域,请参阅云提供商和区域。

重要

创建专用集群时,可以在 mongodbatlas_advanced_cluster资源上设立database_edition。您无法事后更改它,也无法将专用集群扩展回免费或灵活集群。 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 上创建专用集群,请将 database_edition设立为 "INFINITE",并在 auto_scaling 中设立compute_enabled,如以下示例所示:

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
}
}
}
]
}
]
}

专用Atlas无限集群仅支持REPLICASET集群类型。

compute_max_instance_size 当 compute_enabled 为 true 时为必填项。 M30+ Atlas无限集群仅在 AWS 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无限集群上的工作负载隔离性。

storage_config.shard_size_limit_gb 仅当包含 storage_config 时才为必填项。如果省略 storage_config, Atlas将应用默认的最大存储限制。要学习;了解更多信息,请参阅最大存储限制。

注意

storage_config.shard_size_limit_gb 设置集群可以存储的最大数据量。要学习;了解更多信息,请参阅 Terraform提供商文档。

5

您可以将 Terraform 配置中的信息输出到终端窗口。 这对于在Atlas创建资源之前不知道的值非常有用,例如 连接string 。

如果要在部署项目后显示参数,请将一些输出代码行添加到 main.tf文件中。

6

要初始化项目,请运行以下命令:

terraform init

此命令还会下载并安装MongoDB Atlas Provider(如果尚未安装)。

7

要查看执行计划,请运行以下命令:

terraform plan

Terraform 详细说明了其计划进行的更改。 如果输出不符合您的预期,则您的main.tf文件可能存在问题。

8

要部署基础架构,请运行以下命令:

terraform apply

当出现Do you want to perform these actions?提示时,输入yes 。

注意

预配新的Atlas资源可能需要几分钟时间。 Atlas Terraform提供商程序每十秒更新一次,直到完成。

terraform apply 完成后,确认集群已部署。

  • 新集群会出现在项目下的 Atlas 用户界面中。

  • 如果已将输出值添加到 main.tf,则连接字符串会出现在 Terraform 输出中。

要删除在 Terraform目录中创建的所有资源,请运行以下命令:

terraform destroy

警告

如果删除了所有资源,则无法恢复。

当出现Do you really want to destroy all resources?提示时,输入yes 。

部署集群后,您可以:

其他资源: