From 20048529b5d45fc31afa991ef0d0ca170a21235b Mon Sep 17 00:00:00 2001 From: Nikhil Singh Date: Thu, 1 Jul 2021 12:19:27 +0530 Subject: [PATCH 1/5] MongoDB Atlas - GCP VPC Peering example --- .../MongoDB Atlas-GCP VPC Peering/.gitignore | 6 ++ .../MongoDB Atlas-GCP VPC Peering/Readme.md | 58 +++++++++++++++++++ .../atlas-network.tf | 33 +++++++++++ .../MongoDB Atlas-GCP VPC Peering/cluster.tf | 40 +++++++++++++ .../MongoDB Atlas-GCP VPC Peering/provider.tf | 10 ++++ .../variables.tf | 25 ++++++++ .../MongoDB Atlas-GCP VPC Peering/versions.tf | 11 ++++ 7 files changed, 183 insertions(+) create mode 100644 examples/MongoDB Atlas-GCP VPC Peering/.gitignore create mode 100644 examples/MongoDB Atlas-GCP VPC Peering/Readme.md create mode 100644 examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf create mode 100644 examples/MongoDB Atlas-GCP VPC Peering/cluster.tf create mode 100644 examples/MongoDB Atlas-GCP VPC Peering/provider.tf create mode 100644 examples/MongoDB Atlas-GCP VPC Peering/variables.tf create mode 100644 examples/MongoDB Atlas-GCP VPC Peering/versions.tf diff --git a/examples/MongoDB Atlas-GCP VPC Peering/.gitignore b/examples/MongoDB Atlas-GCP VPC Peering/.gitignore new file mode 100644 index 0000000000..ebbf7658b4 --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/.gitignore @@ -0,0 +1,6 @@ +.terraform +.terraform.lock.hcl +terraform.tfstate +terraform.tfstate.backup +terraform.tfvars +service-account.json \ No newline at end of file diff --git a/examples/MongoDB Atlas-GCP VPC Peering/Readme.md b/examples/MongoDB Atlas-GCP VPC Peering/Readme.md new file mode 100644 index 0000000000..25fe59ad5f --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/Readme.md @@ -0,0 +1,58 @@ +# Example - GCP and MongoDB Atlas VPC Peering + +This project aims to provide an example of using Azure and MongoDB Atlas together. + + +## Dependencies + +* Terraform v0.15 +* GCP Account +* A MongoDB Atlas account + +``` +Terraform v0.15.3 +on darwin_amd64 ++ provider registry.terraform.io/hashicorp/google v3.74.0 ++ provider registry.terraform.io/mongodb/mongodbatlas v0.9.1 +``` + +## Usage + +**1\. Ensure your GCP credentials are set up.** + +1. Fetch the Json key from GCP for your project following GCP [documentation](https://cloud.google.com/iam/docs/creating-managing-service-account-keys). +2. Copy the `json` file to the root of the terrform configuration as `service-account.json`. + + +**2\. TFVARS** + +Now create **terraform.tfvars** file with all the variable values and make sure **not to commit it**. + +**3\. Review the Terraform plan.** + +Execute the below command and ensure you are happy with the plan. + +``` bash +$ terraform plan +``` +This project currently does the below deployments: + +- MongoDB Atlas Azure cluster - M10 +- MongoDB Atlas Network Container +- MongoDB Atlas and GCP VPC peering, Routes Entry and IP Access Whitelisting + +**4\. Execute the Terraform apply.** + +Now execute the plan to provision the AWS resources. + +``` bash +$ terraform apply +``` + +**5\. Destroy the resources.** + +Once you are finished your testing, ensure you destroy the resources to avoid unnecessary Azure and Atlas charges. + +``` bash +$ terraform destroy +``` diff --git a/examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf b/examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf new file mode 100644 index 0000000000..3cb2dabdcb --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf @@ -0,0 +1,33 @@ +# Container example provided but not always required, +# see network_container documentation for details. +resource "mongodbatlas_network_container" "test" { + project_id = var.project_id + atlas_cidr_block = "10.8.0.0/18" + provider_name = "GCP" +} + +# Create the peering connection request +resource "mongodbatlas_network_peering" "test" { + project_id = var.project_id + container_id = mongodbatlas_network_container.test.container_id + provider_name = "GCP" + gcp_project_id = var.gcpprojectid + network_name = "default" +} + +# the following assumes a GCP provider is configured +data "google_compute_network" "default" { + name = "default" +} + +# Create the GCP peer +resource "google_compute_network_peering" "peering" { + name = "peering-gcp-terraform-test" + network = data.google_compute_network.default.self_link + peer_network = "https://www.googleapis.com/compute/v1/projects/${mongodbatlas_network_peering.test.atlas_gcp_project_id}/global/networks/${mongodbatlas_network_peering.test.atlas_vpc_name}" +} +resource "mongodbatlas_project_ip_access_list" "test" { + project_id = var.project_id + cidr_block = var.gcp_cidr + comment = "cidr block for GCP VPC Whitelist" +} diff --git a/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf new file mode 100644 index 0000000000..d9145979db --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf @@ -0,0 +1,40 @@ + +resource "mongodbatlas_cluster" "cluster-test-single" { + project_id = var.project_id + name = "cluster-test" + cluster_type = "REPLICASET" + replication_specs { + num_shards = 1 + regions_config { + region_name = var.atlas_region + electable_nodes = 3 + priority = 7 + read_only_nodes = 0 + } + } + labels { + key = "environment" + value = "prod" + } + provider_backup_enabled = true + auto_scaling_disk_gb_enabled = true + mongo_db_major_version = "4.4" + auto_scaling_compute_enabled = true + auto_scaling_compute_scale_down_enabled = true + + + //Provider Settings "block" + provider_name = "GCP" + provider_instance_size_name = "M10" + provider_auto_scaling_compute_max_instance_size = "M20" + provider_auto_scaling_compute_min_instance_size = "M10" + disk_size_gb = 40 + advanced_configuration { + minimum_enabled_tls_protocol = "TLS1_2" + } + lifecycle { + ignore_changes = [ + provider_instance_size_name + ] + } +} \ No newline at end of file diff --git a/examples/MongoDB Atlas-GCP VPC Peering/provider.tf b/examples/MongoDB Atlas-GCP VPC Peering/provider.tf new file mode 100644 index 0000000000..6328c70333 --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/provider.tf @@ -0,0 +1,10 @@ +provider "mongodbatlas" { + public_key = var.public_key + private_key = var.private_key +} +provider "google" { + credentials = file("service-account.json") + project = var.gcpprojectid + region = var.gcp_region + //zone="us-central-1c" +} \ No newline at end of file diff --git a/examples/MongoDB Atlas-GCP VPC Peering/variables.tf b/examples/MongoDB Atlas-GCP VPC Peering/variables.tf new file mode 100644 index 0000000000..d6459f7bc8 --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/variables.tf @@ -0,0 +1,25 @@ +variable "public_key" { + description = "Public API key to authenticate to Atlas" +} +variable "private_key" { + description = "Private API key to authenticate to Atlas" +} +variable "mongodbversion" { + description = "The Major MongoDB Version" + default = "4.2" +} +variable "project_id" { + description = "The Atlas Project Name" +} +variable "gcpprojectid" { + default = "terraform-gcp-atlas" +} +variable "gcp_cidr" { + default = "10.128.0.0/20" +} +variable "gcp_region" { + description = "The GCP Region to use for deployment" +} +variable "atlas_region" { + description = "The MongoDB Atlas region" +} \ No newline at end of file diff --git a/examples/MongoDB Atlas-GCP VPC Peering/versions.tf b/examples/MongoDB Atlas-GCP VPC Peering/versions.tf new file mode 100644 index 0000000000..70a979ce77 --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/versions.tf @@ -0,0 +1,11 @@ +terraform { + required_providers { + mongodbatlas = { + source = "mongodb/mongodbatlas" + } + google = { + source = "hashicorp/google" + } + } + required_version = ">= 0.15" +} From ad9f8727cec0da053518c968800f28b0119045d8 Mon Sep 17 00:00:00 2001 From: Nikhil Singh Date: Thu, 1 Jul 2021 12:41:00 +0530 Subject: [PATCH 2/5] add newline --- examples/MongoDB Atlas-GCP VPC Peering/.gitignore | 2 +- examples/MongoDB Atlas-GCP VPC Peering/cluster.tf | 2 +- examples/MongoDB Atlas-GCP VPC Peering/provider.tf | 2 +- examples/MongoDB Atlas-GCP VPC Peering/variables.tf | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/MongoDB Atlas-GCP VPC Peering/.gitignore b/examples/MongoDB Atlas-GCP VPC Peering/.gitignore index ebbf7658b4..8ee362a9e4 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/.gitignore +++ b/examples/MongoDB Atlas-GCP VPC Peering/.gitignore @@ -3,4 +3,4 @@ terraform.tfstate terraform.tfstate.backup terraform.tfvars -service-account.json \ No newline at end of file +service-account.json diff --git a/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf index d9145979db..1e7415ef88 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf +++ b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf @@ -37,4 +37,4 @@ resource "mongodbatlas_cluster" "cluster-test-single" { provider_instance_size_name ] } -} \ No newline at end of file +} diff --git a/examples/MongoDB Atlas-GCP VPC Peering/provider.tf b/examples/MongoDB Atlas-GCP VPC Peering/provider.tf index 6328c70333..b18bf78e29 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/provider.tf +++ b/examples/MongoDB Atlas-GCP VPC Peering/provider.tf @@ -7,4 +7,4 @@ provider "google" { project = var.gcpprojectid region = var.gcp_region //zone="us-central-1c" -} \ No newline at end of file +} diff --git a/examples/MongoDB Atlas-GCP VPC Peering/variables.tf b/examples/MongoDB Atlas-GCP VPC Peering/variables.tf index d6459f7bc8..a0c90a2276 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/variables.tf +++ b/examples/MongoDB Atlas-GCP VPC Peering/variables.tf @@ -22,4 +22,4 @@ variable "gcp_region" { } variable "atlas_region" { description = "The MongoDB Atlas region" -} \ No newline at end of file +} From 6331fe4e622ae1c666adc5328ee2c91daf264643 Mon Sep 17 00:00:00 2001 From: Nikhil Singh Date: Sat, 3 Jul 2021 06:40:11 +0530 Subject: [PATCH 3/5] comments enhancement and typo fix as per PR-490 --- examples/MongoDB Atlas-GCP VPC Peering/Readme.md | 2 +- examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf | 7 ++++++- examples/MongoDB Atlas-GCP VPC Peering/cluster.tf | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/MongoDB Atlas-GCP VPC Peering/Readme.md b/examples/MongoDB Atlas-GCP VPC Peering/Readme.md index 25fe59ad5f..49de66f574 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/Readme.md +++ b/examples/MongoDB Atlas-GCP VPC Peering/Readme.md @@ -1,6 +1,6 @@ # Example - GCP and MongoDB Atlas VPC Peering -This project aims to provide an example of using Azure and MongoDB Atlas together. +This project aims to provide an example of using GCP and MongoDB Atlas together. ## Dependencies diff --git a/examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf b/examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf index 3cb2dabdcb..55d00978d0 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf +++ b/examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf @@ -23,9 +23,14 @@ data "google_compute_network" "default" { # Create the GCP peer resource "google_compute_network_peering" "peering" { name = "peering-gcp-terraform-test" - network = data.google_compute_network.default.self_link + //The URI of the GCP VPC. self_link which is found by enabling the [Compute Engine API](https://console.cloud.google.com/apis/api/compute.googleapis.com) + network = data.google_compute_network.default.self_link + //The URI of the Atlas VPC peer_network = "https://www.googleapis.com/compute/v1/projects/${mongodbatlas_network_peering.test.atlas_gcp_project_id}/global/networks/${mongodbatlas_network_peering.test.atlas_vpc_name}" } + +#Create IP Access List for connection from GCP +//You will need to add the private IP ranges of the subnets in which your application is hosted to the IP access list in order to connect to your Atlas cluster. GCP networks generated in auto-mode use a CIDR range of 10.128.0.0/9 resource "mongodbatlas_project_ip_access_list" "test" { project_id = var.project_id cidr_block = var.gcp_cidr diff --git a/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf index 1e7415ef88..17817aeacf 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf +++ b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf @@ -1,3 +1,4 @@ +//This cluster is in GCP cloud-provider with VPC peering enabled resource "mongodbatlas_cluster" "cluster-test-single" { project_id = var.project_id @@ -38,3 +39,7 @@ resource "mongodbatlas_cluster" "cluster-test-single" { ] } } +//The connection strings available for the GCP MognoDB Atlas cluster +output "connection_string" { + value = mongodbatlas_cluster.cluster-atlas.connection_strings +} From 943c0aa45941a9adc2d086c42a26fe3c20750c40 Mon Sep 17 00:00:00 2001 From: Nikhil Singh Date: Sat, 3 Jul 2021 11:39:28 +0530 Subject: [PATCH 4/5] fix typo --- examples/MongoDB Atlas-GCP VPC Peering/cluster.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf index 17817aeacf..7043c976df 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf +++ b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf @@ -1,6 +1,6 @@ //This cluster is in GCP cloud-provider with VPC peering enabled -resource "mongodbatlas_cluster" "cluster-test-single" { +resource "mongodbatlas_cluster" "cluster" { project_id = var.project_id name = "cluster-test" cluster_type = "REPLICASET" @@ -41,5 +41,5 @@ resource "mongodbatlas_cluster" "cluster-test-single" { } //The connection strings available for the GCP MognoDB Atlas cluster output "connection_string" { - value = mongodbatlas_cluster.cluster-atlas.connection_strings + value = mongodbatlas_cluster.cluster.connection_strings } From 93a58d4c9b92c80db6aeb18f20d80fd9eb98976b Mon Sep 17 00:00:00 2001 From: Nikhil Singh Date: Wed, 7 Jul 2021 11:57:42 +0530 Subject: [PATCH 5/5] fix typo --- examples/MongoDB Atlas-GCP VPC Peering/Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/MongoDB Atlas-GCP VPC Peering/Readme.md b/examples/MongoDB Atlas-GCP VPC Peering/Readme.md index 49de66f574..e4318beed8 100644 --- a/examples/MongoDB Atlas-GCP VPC Peering/Readme.md +++ b/examples/MongoDB Atlas-GCP VPC Peering/Readme.md @@ -37,7 +37,7 @@ $ terraform plan ``` This project currently does the below deployments: -- MongoDB Atlas Azure cluster - M10 +- MongoDB Atlas GCP cluster - M10 - MongoDB Atlas Network Container - MongoDB Atlas and GCP VPC peering, Routes Entry and IP Access Whitelisting @@ -51,7 +51,7 @@ $ terraform apply **5\. Destroy the resources.** -Once you are finished your testing, ensure you destroy the resources to avoid unnecessary Azure and Atlas charges. +Once you are finished your testing, ensure you destroy the resources to avoid unnecessary GCP and Atlas charges. ``` bash $ terraform destroy