diff --git a/examples/MongoDB Atlas-GCP VPC Peering/.gitignore b/examples/MongoDB Atlas-GCP VPC Peering/.gitignore new file mode 100644 index 0000000000..8ee362a9e4 --- /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 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..e4318beed8 --- /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 GCP 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 GCP 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 GCP 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..55d00978d0 --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf @@ -0,0 +1,38 @@ +# 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" + //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 + 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..7043c976df --- /dev/null +++ b/examples/MongoDB Atlas-GCP VPC Peering/cluster.tf @@ -0,0 +1,45 @@ +//This cluster is in GCP cloud-provider with VPC peering enabled + +resource "mongodbatlas_cluster" "cluster" { + 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 + ] + } +} +//The connection strings available for the GCP MognoDB Atlas cluster +output "connection_string" { + value = mongodbatlas_cluster.cluster.connection_strings +} 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..b18bf78e29 --- /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" +} 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..a0c90a2276 --- /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" +} 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" +}