Docs Menu

Terraform を使用してテンプレートから Atlas クラスターを作成

項目一覧

このページでは、Visual Studio Code 用の MongoDB 拡張機能に含まれる Terraform ファイルの Atlas テンプレートを使用して、Shared Tier Atlas クラスターを作成する方法について説明します。

テンプレートを読み込んだ後、クラスターを構成し、Atlas アカウントの詳細を指定します。次に、Terraform コマンドを使用して Atlas クラスターを計画、適用、破棄します。

Visual Studio Code 用の MongoDB 拡張機能に含まれる Terraform ファイルの Atlas テンプレートを使用する前に、次の操作を行う必要があります。

VS Code 拡張機能に含まれる Terraform ファイルの Atlas テンプレートを使用して、Atlas クラスターを構成します。

1
オペレーティング システム
方式
アクション

Any

Visual Studio Code メニュー

File > New File

MacOS

キーボード ショートカット

Command + N を押します

Windows および Linux

キーボード ショートカット

Control + N を押します

2

注意

main.tfは、Terraform モジュールへのエントリ ポイントに推奨されるファイル名です。

3

VSCode 用の MongoDB 拡張機能は、Shared Tier Atlas クラスターを作成するために、MongoDB Atlas Terraform プロバイダーを使用してサンプル構成をファイルに入力します。

4

カーソルが移動して、mongodbatlas_project リソースの name 引数の値を選択します。

クラスターを構成するには、次の引数に値を指定します。

Tip

Tab キーを押して、テンプレート内で更新する次の引数を選択します。

属性
mongodbatlas_project
.name

この構成によって作成される Atlas プロジェクトの名前。

mongodbatlas_cluster
.name

この構成によって作成される Atlas クラスターの名前。

mongodbatlas_cluster
.backing_provider_name

この構成で作成される Atlas クラスターがホストされるプロバイダー。許容される次の値のいずれかを選択します。

  • AWS

  • AZURE

  • GCP

mongodbatlas_cluster
.provider_region_name

この構成で作成される Atlas クラスターが配置されるリージョン。選択したリージョンが、配置するインスタンス サイズの Atlas クラスターをサポートしていることを確認します。

デフォルトでは、テンプレートは共有層クラスター M2M5 をプロビジョニングします。

各プロバイダーとリージョンがサポートするインスタンス サイズの詳細については、Atlas ドキュメントの次のセクションを参照してください。

mongodbatlas_cluster
.provider_instance_size_name

この構成によって作成される Atlas クラスターのインスタンスのサイズ。

次のいずれかを行います。

  • テンプレートに含まれる共有層インスタンス サイズのいずれかを選択します: M2 または M5

  • 選択したプロバイダーとリージョンに対して Atlas がサポートする別のインスタンス サイズを入力します。

各プロバイダーとリージョンがサポートするインスタンス サイズの詳細については、Atlas ドキュメントの次のセクションを参照してください。

mongodbatlas_cluster
.disk_size_gbs

この構成で作成される Atlas クラスターのディスク サイズ。選択したインスタンス サイズの最大ディスク サイズ以下の値を指定してください。

  • M2 クラスターの場合は、2 と入力します。

  • M5 クラスターの場合は、5 と入力します。

各プロバイダーが各インスタンス サイズに対してサポートするディスク サイズの詳細については、Atlas ドキュメントの次のセクションを参照してください。

5

警告

local 変数には機密情報が含まれています。これらの値を、公開されているリポジトリにチェックインしないでください

次の local 変数に値を指定します。

変数

mongodb_atlas_api_pub_key

Atlas 公開 API キー。

mongodb_atlas_api_pri_key

Atlas プライベート API キー。

mongodb_atlas_org_id

プロジェクトを作成する Atlas 組織 ID。

mongodb_atlas_database_username

Atlas がクラスター用に作成する MongoDB データベース ユーザーのユーザー名。

mongodb_atlas_database_user_password

mongodb_atlas_database_username で指定された MongoDB データベース ユーザーのパスワード。

mongodb_atlas_whitelistip

Atlas クラスターにアクセスできる IP アドレスまたは CIDR ブロック。

入力変数ファイルを使用してセキュリティを最大化する

セキュリティを最大限に高めるには、次の手順を実行することを検討してください。

  1. 入力変数 ファイルで local 変数を定義します。

    variable "mongodb_atlas_api_pub_key" {
    default = "my-public-key"
    }
    variable "mongodb_atlas_api_pri_key" {
    default = "my-private-key"
    }
  2. 入力変数ファイルをリポジトリから除外します。たとえば、リポジトリの .gitignore ファイルにファイル名を追加します。

  3. main.tf ファイル内の入力変数ファイルから変数を参照するには、先頭に vars. を付けます。

    provider "mongodbatlas" {
    public_key = vars.mongodb_atlas_api_pub_key
    private_key = vars.mongodb_atlas_api_pri_key
    }
6

サポートされている構成オプションの完全なリストについては、「MongoDB Atlas Terraform プロバイダーのドキュメント」を参照してください。

注意

Shared Tier Atlas クラスターは、MongoDB Atlas Terraform プロバイダーで利用可能なすべての構成をサポートしていません。サポートされていない構成のリストについては、Atlas のドキュメントを参照してください。

7

テンプレートを使用して Terraform ファイルを作成したら、Atlas クラスターを作成します。

1
2
terraform init

次の出力は、MongoDB Atlas Terraform プロバイダーがインストールされ、使用できる状態であることを示しています。

1Initializing the backend...
2
3Initializing provider plugins...
4- Checking for available provider plugins...
5- Downloading plugin for provider "mongodbatlas" (terraform-providers/mongodbatlas) 0.5.1...
6
7The following providers do not have any version constraints in configuration,
8so the latest version was installed.
9
10To prevent automatic upgrades to new major versions that may contain breaking
11changes, it is recommended to add version = "..." constraints to the
12corresponding provider blocks in configuration, with the constraint strings
13suggested below.
14
15* provider.mongodbatlas: version = "~> 0.5"
16
17Terraform has been successfully initialized!
3
terraform plan

次の出力は、Terraform 構成を適用した場合に何が起こるかを示しています。

1Refreshing Terraform state in-memory prior to plan...
2The refreshed state will be used to calculate this plan, but will not be
3persisted to local or remote state storage.
4
5
6------------------------------------------------------------------------
7
8An execution plan has been generated and is shown below.
9Resource actions are indicated with the following symbols:
10 + create
11
12Terraform 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
110Plan: 4 to add, 0 to change, 0 to destroy.
111
112------------------------------------------------------------------------
113
114Note: You didn't specify an "-out" parameter to save this plan, so Terraform
115can't guarantee that exactly these actions will be performed if
116"terraform apply" is subsequently run.

情報が正しい場合は次の手順に進みます。

4
terraform apply

構成を適用するかどうかを確認するプロンプトが表示されたら、yes と入力します。

注意

terraform apply コマンドが完了するまでに数分かかる場合があります。

次の出力は、Atlas クラスターが作成されたことを示しています。 表示される接続文字列を使用して、VS Code 拡張機能を使用してこの配置への 接続を作成できます。

1Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
2
3Outputs:
4
5connection_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 クラスターを削除するには、次の手順に従います。

1
2
terraform destroy

構成で定義されているリソースを破棄するかどうかを確認するプロンプトが表示されたら、yes と入力します。

注意

terraform destroy コマンドが完了するまでに数分かかる場合があります。

次の出力は、Atlas クラスターと関連するすべてのリソースが削除されたことを示しています。

Destroy complete! Resources: 4 destroyed.

項目一覧