Skip to content

Commit 7b9fa80

Browse files
authored
(DOCSP-44262) Adds Terraform examples for projects and clusters (#10)
1 parent 87692f7 commit 7b9fa80

9 files changed

+287
-4
lines changed

source/hierarchy.txt

Lines changed: 132 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ These examples also apply other recommended configurations, including:
7878
Create the Projects
7979
~~~~~~~~~~~~~~~~~~~
8080

81-
Run the following command for each application and environment pair:
81+
Run the following command for each application and environment pair. Change the IDs and names to use your values:
8282

8383
.. include:: /includes/examples/cli-example-create-projects.rst
8484

@@ -97,15 +97,16 @@ These examples also apply other recommended configurations, including:
9797
.. tab:: Dev and Test Environments
9898
:tabid: devtest
9999

100-
For your development and testing environments, run the following command for each project that you created:
100+
For your development and testing environments, run the following command for each project that you created. Change
101+
the IDs and names to use your values:
101102

102103
.. include:: /includes/examples/cli-example-create-clusters-devtest.rst
103104

104105
.. tab:: Staging and Prod Environments
105106
:tabid: stagingprod
106107

107108
For your staging and production environments, create the following ``cluster.json`` file for each project that you
108-
created:
109+
created. Change the IDs and names to use your values:
109110

110111
.. include:: /includes/examples/cli-json-example-create-clusters.rst
111112

@@ -121,5 +122,132 @@ These examples also apply other recommended configurations, including:
121122
.. tab:: Terraform
122123
:tabid: Terraform
123124

124-
Content here
125+
.. note::
126+
127+
Before you
128+
can create resources with Terraform, you must:
129+
130+
- :atlas:`Create your paying organization
131+
</billing/#configure-a-paying-organization>` and :atlas:`create an API key </configure-api-access/>` for the
132+
paying organization. Store your API key as environment
133+
variables by running the following command in the terminal:
134+
135+
.. code-block::
136+
137+
export MONGODB_ATLAS_PUBLIC_KEY="<insert your public key here>"
138+
export MONGODB_ATLAS_PRIVATE_KEY="<insert your private key here>"
139+
140+
- `Install Terraform
141+
<https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli>`__
142+
143+
Create the Projects and Deployments
144+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145+
146+
.. tabs::
147+
148+
.. tab:: Dev and Test Environments
149+
:tabid: devtest
150+
151+
For your development and testing environments, create the
152+
following files for each application and environment
153+
pair. Place the files for each application and environment
154+
pair in their own directory. Change the IDs and names to use your values:
155+
156+
main.tf
157+
```````
158+
159+
.. include:: /includes/examples/tf-example-main-devtest.rst
160+
161+
variables.tf
162+
````````````
163+
164+
.. include:: /includes/examples/tf-example-variables.rst
165+
166+
terraform.tfvars
167+
````````````````
168+
169+
.. include:: /includes/examples/tf-example-tfvars-devtest.rst
170+
171+
provider.tf
172+
```````````
173+
174+
.. include:: /includes/examples/tf-example-provider.rst
175+
176+
After you create the files, navigate to each application and environment pair's directory and run the following
177+
command to initialize Terraform:
178+
179+
.. code-block::
180+
181+
terraform init
182+
183+
Run the following command to view the Terraform plan:
184+
185+
.. code-block::
186+
187+
terraform plan
188+
189+
Run the following command to create one project and one deployment for the application and environment pair. The command uses the files and the |service-terraform| to
190+
create the projects and clusters:
191+
192+
.. code-block::
193+
194+
terraform apply
195+
196+
When prompted, type ``yes`` and press :kbd:`Enter` to apply
197+
the configuration.
198+
199+
.. tab:: Staging and Prod Environments
200+
:tabid: stagingprod
201+
202+
For your staging and production environments, create the
203+
following files for each application and environment
204+
pair. Place the files for each application and environment
205+
pair in their own directory. Change the IDs and names to use your values:
206+
207+
main.tf
208+
```````
209+
210+
.. include:: /includes/examples/tf-example-main-stagingprod.rst
211+
212+
variables.tf
213+
````````````
214+
215+
.. include:: /includes/examples/tf-example-variables.rst
216+
217+
terraform.tfvars
218+
````````````````
219+
220+
.. include:: /includes/examples/tf-example-tfvars-stagingprod.rst
221+
222+
provider.tf
223+
```````````
224+
225+
.. include:: /includes/examples/tf-example-provider.rst
226+
227+
After you create the files, navigate to each application and environment pair's directory and run the following
228+
command to initialize Terraform:
229+
230+
.. code-block::
231+
232+
terraform init
233+
234+
Run the following command to view the Terraform plan:
235+
236+
.. code-block::
237+
238+
terraform plan
239+
240+
Run the following command to create one project and one deployment for the application and environment pair. The command uses the files and the |service-terraform| to
241+
create the projects and clusters:
242+
243+
.. code-block::
244+
245+
terraform apply
246+
247+
When prompted, type ``yes`` and press :kbd:`Enter` to apply
248+
the configuration.
249+
250+
For more configuration options and info about this example,
251+
see |service-terraform| and the `MongoDB Terraform Blog Post
252+
<https://www.mongodb.com/developer/products/atlas/deploy-mongodb-atlas-terraform-aws/>`__.
125253

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.. code-block::
2+
:copyable: true
3+
4+
# Create a Project
5+
resource "mongodbatlas_project" "atlas-project" {
6+
org_id = var.atlas_org_id
7+
name = var.atlas_project_name
8+
}
9+
10+
# Create an Atlas Advanced Cluster
11+
resource "mongodbatlas_advanced_cluster" "atlas-cluster" {
12+
project_id = mongodbatlas_project.atlas-project.id
13+
name = "ClusterPortalDev"
14+
cluster_type = "REPLICASET"
15+
mongo_db_major_version = var.mongodb_version
16+
replication_specs {
17+
region_configs {
18+
electable_specs {
19+
instance_size = var.cluster_instance_size_name
20+
node_count = 3
21+
}
22+
analytics_specs {
23+
instance_size = var.cluster_instance_size_name
24+
node_count = 1
25+
}
26+
priority = 7
27+
provider_name = var.cloud_provider
28+
region_name = var.atlas_region
29+
}
30+
}
31+
}
32+
33+
# Outputs to Display
34+
output "atlas_cluster_connection_string" { value = mongodbatlas_advanced_cluster.atlas-cluster.connection_strings.0.standard_srv }
35+
output "project_name" { value = mongodbatlas_project.atlas-project.name }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.. code-block::
2+
:copyable: true
3+
4+
# Create a Project
5+
resource "mongodbatlas_project" "atlas-project" {
6+
org_id = var.atlas_org_id
7+
name = var.atlas_project_name
8+
}
9+
10+
# Create an Atlas Advanced Cluster
11+
resource "mongodbatlas_advanced_cluster" "atlas-cluster" {
12+
project_id = mongodbatlas_project.atlas-project.id
13+
name = "ClusterPortalProd"
14+
cluster_type = "REPLICASET"
15+
mongo_db_major_version = var.mongodb_version
16+
replication_specs {
17+
region_configs {
18+
electable_specs {
19+
instance_size = var.cluster_instance_size_name
20+
node_count = 3
21+
}
22+
analytics_specs {
23+
instance_size = var.cluster_instance_size_name
24+
node_count = 1
25+
}
26+
priority = 7
27+
provider_name = var.cloud_provider
28+
region_name = var.atlas_region
29+
}
30+
}
31+
}
32+
33+
# Outputs to Display
34+
output "atlas_cluster_connection_string" { value = mongodbatlas_advanced_cluster.atlas-cluster.connection_strings.0.standard_srv }
35+
output "project_name" { value = mongodbatlas_project.atlas-project.name }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. code-block::
2+
:copyable: true
3+
4+
# Define the MongoDB Atlas Provider
5+
terraform {
6+
required_providers {
7+
mongodbatlas = {
8+
source = "mongodb/mongodbatlas"
9+
}
10+
}
11+
required_version = ">= 0.13"
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. code-block::
2+
:copyable: true
3+
4+
atlas_org_id = "32b6e34b3d91647abb20e7b8"
5+
atlas_project_name = "Customer Portal - Dev"
6+
environment = "dev"
7+
cluster_instance_size_name = "M30"
8+
cloud_provider = "AWS"
9+
atlas_region = "US_WEST_2"
10+
mongodb_version = "8.0"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. code-block::
2+
:copyable: true
3+
4+
atlas_org_id = "32b6e34b3d91647abb20e7b8"
5+
atlas_project_name = "Customer Portal - Prod"
6+
environment = "prod"
7+
cluster_instance_size_name = "M30"
8+
cloud_provider = "AWS"
9+
atlas_region = "US_WEST_2"
10+
mongodb_version = "8.0"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.. code-block::
2+
:copyable: true
3+
4+
# Atlas Organization ID
5+
variable "atlas_org_id" {
6+
type = string
7+
description = "Atlas Organization ID"
8+
}
9+
# Atlas Project Name
10+
variable "atlas_project_name" {
11+
type = string
12+
description = "Atlas Project Name"
13+
}
14+
15+
# Atlas Project Environment
16+
variable "environment" {
17+
type = string
18+
description = "The environment to be built"
19+
}
20+
21+
# Cluster Instance Size Name
22+
variable "cluster_instance_size_name" {
23+
type = string
24+
description = "Cluster instance size name"
25+
}
26+
27+
# Cloud Provider to Host Atlas Cluster
28+
variable "cloud_provider" {
29+
type = string
30+
description = "AWS or GCP or Azure"
31+
}
32+
33+
# Atlas Region
34+
variable "atlas_region" {
35+
type = string
36+
description = "Atlas region where resources will be created"
37+
}
38+
39+
# MongoDB Version
40+
variable "mongodb_version" {
41+
type = string
42+
description = "MongoDB Version"
43+
}

source/includes/shared-settings-clusters-devtest.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
- Cluster tier set to ``M30`` for a medium-sized application. Use the
33
:ref:`cluster size guide <arch-center-cluster-size-guide>` to learn
44
the recommended cluster tier for your application size.
5+
6+
Our examples use |aws|, |azure|, and {+gcp+}
7+
interchangeably. You can use any of these three cloud providers, but
8+
you must change the region name to match the cloud provider. To learn about the cloud providers and their regions, see
9+
:atlas:`Cloud Providers </reference/cloud-providers/>`.
510

611

source/includes/shared-settings-clusters-stagingprod.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
:ref:`cluster size guide <arch-center-cluster-size-guide>` to learn
33
the recommended cluster tier for your application size.
44

5+
Our examples use |aws|, |azure|, and {+gcp+}
6+
interchangeably. You can use any of these three cloud providers, but
7+
you must change the region name to match the cloud provider. To learn about the cloud providers and their regions, see
8+
:atlas:`Cloud Providers </reference/cloud-providers/>`.
9+

0 commit comments

Comments
 (0)