Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB Atlas - GCP VPC Peering example #490

Merged
merged 5 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/MongoDB Atlas-GCP VPC Peering/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.terraform
.terraform.lock.hcl
terraform.tfstate
terraform.tfstate.backup
terraform.tfvars
service-account.json
58 changes: 58 additions & 0 deletions examples/MongoDB Atlas-GCP VPC Peering/Readme.md
Original file line number Diff line number Diff line change
@@ -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
```
38 changes: 38 additions & 0 deletions examples/MongoDB Atlas-GCP VPC Peering/atlas-network.tf
Original file line number Diff line number Diff line change
@@ -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" {
nikhil-mongo marked this conversation as resolved.
Show resolved Hide resolved
project_id = var.project_id
cidr_block = var.gcp_cidr
comment = "cidr block for GCP VPC Whitelist"
}
45 changes: 45 additions & 0 deletions examples/MongoDB Atlas-GCP VPC Peering/cluster.tf
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions examples/MongoDB Atlas-GCP VPC Peering/provider.tf
Original file line number Diff line number Diff line change
@@ -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"
}
25 changes: 25 additions & 0 deletions examples/MongoDB Atlas-GCP VPC Peering/variables.tf
Original file line number Diff line number Diff line change
@@ -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"
}
11 changes: 11 additions & 0 deletions examples/MongoDB Atlas-GCP VPC Peering/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
}
google = {
source = "hashicorp/google"
}
}
required_version = ">= 0.15"
}