Skip to content

Commit 9092546

Browse files
authored
documentation fix #335 and examples added for the Azure VNET peering and AWS Private Link (#340)
* added examples for the Azure VNET peering and AWS Private Link * typo fix as mentioned in #335 * fix Readme.md with Terraform version
1 parent aa63309 commit 9092546

File tree

13 files changed

+479
-4
lines changed

13 files changed

+479
-4
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Example - AWS and Atlas PrivateLink with Terraform
2+
3+
This project aims to provide a very straight-forward example of setting up PrivateLink connection between AWS and MongoDB Atlas.
4+
5+
6+
## Dependencies
7+
8+
* Terraform v0.13
9+
* An AWS account - provider.aws: version = "~> 3.3"
10+
* A MongoDB Atlas account - provider.mongodbatlas: version = "~> 0.6"
11+
12+
## Usage
13+
14+
**1\. Ensure your AWS and MongoDB Atlas credentials are set up.**
15+
16+
This can be done using environment variables:
17+
18+
``` bash
19+
$ export AWS_SECRET_ACCESS_KEY='your secret key'
20+
$ export AWS_ACCESS_KEY_ID='your key id'
21+
```
22+
23+
```bash
24+
export MONGODB_ATLAS_PUBLIC_KEY="xxxx"
25+
export MONGODB_ATLAS_PRIVATE_KEY="xxxx"
26+
```
27+
28+
... or the `~/.aws/credentials` file.
29+
30+
```
31+
$ cat ~/.aws/credentials
32+
[default]
33+
aws_access_key_id = your key id
34+
aws_secret_access_key = your secret key
35+
36+
```
37+
... or follow as in the `variables.tf` file and create **terraform.tfvars** file with all the variable values and make sure **not to commit it**.
38+
39+
**2\. Review the Terraform plan.**
40+
41+
Execute the below command and ensure you are happy with the plan.
42+
43+
``` bash
44+
$ terraform plan
45+
```
46+
This project currently does the below deployments:
47+
48+
- MongoDB cluster - M10
49+
- AWS Custom VPC, Internet Gateway, Route Tables, Subnets with Public and Private access
50+
- PrivateLink Connection at MongoDB Atlas
51+
- Create VPC Endpoint in AWS
52+
53+
**3\. Configure the security group as required.**
54+
55+
The security group in this configuration allows All Traffic access in Inbound and Outbound Rules.
56+
57+
**4\. Execute the Terraform apply.**
58+
59+
Now execute the plan to provision the AWS and Atlas resources.
60+
61+
``` bash
62+
$ terraform apply
63+
```
64+
65+
**5\. Destroy the resources.**
66+
67+
Once you are finished your testing, ensure you destroy the resources to avoid unnecessary charges.
68+
69+
``` bash
70+
$ terraform destroy
71+
```
72+
73+
**Important Point**
74+
75+
To fetch the connection string follow the below steps:
76+
```
77+
output "atlasclusterstring" {
78+
value = mongodbatlas_cluster.cluster-atlas.connection_strings
79+
}
80+
```
81+
**Outputs:**
82+
```
83+
atlasclusterstring = [
84+
{
85+
"aws_private_link" = {
86+
"vpce-0ebb76559e8affc96" = "mongodb://pl-0-us-east-1.za3fb.mongodb.net:1024,pl-0-us-east-1.za3fb.mongodb.net:1025,pl-0-us-east-1.za3fb.mongodb.net:1026/?ssl=true&authSource=admin&replicaSet=atlas-d177ke-shard-0"
87+
}
88+
"aws_private_link_srv" = {
89+
"vpce-0ebb76559e8affc96" = "mongodb+srv://cluster-atlas-pl-0.za3fb.mongodb.net"
90+
}
91+
"private" = ""
92+
"private_srv" = ""
93+
"standard" = "mongodb://cluster-atlas-shard-00-00.za3fb.mongodb.net:27017,cluster-atlas-shard-00-01.za3fb.mongodb.net:27017,cluster-atlas-shard-00-02.za3fb.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=atlas-d177ke-shard-0"
94+
"standard_srv" = "mongodb+srv://cluster-atlas.za3fb.mongodb.net"
95+
},
96+
]
97+
```
98+
99+
To fetch a particular connection string, use the **lookup()** function of terraform as below:
100+
101+
```
102+
output "plstring" {
103+
value = lookup(mongodbatlas_cluster.cluster-atlas.connection_strings[0].aws_private_link_srv, aws_vpc_endpoint.ptfe_service.id)
104+
}
105+
```
106+
**Output:**
107+
```
108+
plstring = mongodb+srv://cluster-atlas-pl-0.za3fb.mongodb.net
109+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
resource "mongodbatlas_cluster" "cluster-atlas" {
2+
project_id = var.atlasprojectid
3+
name = "cluster-atlas"
4+
num_shards = 1
5+
replication_factor = 3
6+
provider_backup_enabled = true
7+
auto_scaling_disk_gb_enabled = true
8+
mongo_db_major_version = "4.2"
9+
10+
//Provider settings
11+
provider_name = "AWS"
12+
disk_size_gb = 10
13+
provider_disk_iops = 100
14+
provider_volume_type = "STANDARD"
15+
provider_encrypt_ebs_volume = true
16+
provider_instance_size_name = "M10"
17+
provider_region_name = var.atlas_region
18+
}
19+
output "atlasclusterstring" {
20+
value = mongodbatlas_cluster.cluster-atlas.connection_strings
21+
}
22+
output "plstring" {
23+
value = lookup(mongodbatlas_cluster.cluster-atlas.connection_strings[0].aws_private_link_srv, aws_vpc_endpoint.ptfe_service.id)
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "mongodbatlas_private_endpoint" "atlaspl" {
2+
project_id = var.atlasprojectid
3+
provider_name = "AWS"
4+
region = var.aws_region
5+
}
6+
7+
resource "aws_vpc_endpoint" "ptfe_service" {
8+
vpc_id = aws_vpc.primary.id
9+
service_name = mongodbatlas_private_endpoint.atlaspl.endpoint_service_name
10+
vpc_endpoint_type = "Interface"
11+
subnet_ids = [aws_subnet.primary-az1.id, aws_subnet.primary-az2.id]
12+
security_group_ids = [aws_security_group.primary_default.id]
13+
}
14+
15+
resource "mongodbatlas_private_endpoint_interface_link" "atlaseplink" {
16+
project_id = mongodbatlas_private_endpoint.atlaspl.project_id
17+
private_link_id = mongodbatlas_private_endpoint.atlaspl.private_link_id
18+
interface_endpoint_id = aws_vpc_endpoint.ptfe_service.id
19+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//Create Primary VPC
2+
resource "aws_vpc" "primary" {
3+
cidr_block = "10.0.0.0/16"
4+
enable_dns_hostnames = true
5+
enable_dns_support = true
6+
}
7+
8+
//Create IGW
9+
resource "aws_internet_gateway" "primary" {
10+
vpc_id = aws_vpc.primary.id
11+
}
12+
13+
//Route Table
14+
resource "aws_route" "primary-internet_access" {
15+
route_table_id = aws_vpc.primary.main_route_table_id
16+
destination_cidr_block = "0.0.0.0/0"
17+
gateway_id = aws_internet_gateway.primary.id
18+
}
19+
20+
//Subnet-A
21+
resource "aws_subnet" "primary-az1" {
22+
vpc_id = aws_vpc.primary.id
23+
cidr_block = "10.0.1.0/24"
24+
map_public_ip_on_launch = true
25+
availability_zone = "${var.aws_region}a"
26+
}
27+
28+
//Subnet-B
29+
resource "aws_subnet" "primary-az2" {
30+
vpc_id = aws_vpc.primary.id
31+
cidr_block = "10.0.2.0/24"
32+
map_public_ip_on_launch = false
33+
availability_zone = "${var.aws_region}b"
34+
}
35+
36+
/*Security-Group
37+
Ingress - Port 80 -- limited to instance
38+
Port 22 -- Open to ssh without limitations
39+
Egress - Open to All*/
40+
41+
resource "aws_security_group" "primary_default" {
42+
name_prefix = "default-"
43+
description = "Default security group for all instances in ${aws_vpc.primary.id}"
44+
vpc_id = aws_vpc.primary.id
45+
ingress {
46+
from_port = 0
47+
to_port = 0
48+
protocol = "tcp"
49+
cidr_blocks = [
50+
"0.0.0.0/0",
51+
]
52+
}
53+
egress {
54+
from_port = 0
55+
to_port = 0
56+
protocol = "-1"
57+
cidr_blocks = ["0.0.0.0/0"]
58+
}
59+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provider "mongodbatlas" {
2+
public_key = var.public_key
3+
private_key = var.private_key
4+
}
5+
provider "aws" {
6+
access_key = var.access_key
7+
secret_key = var.secret_key
8+
region = var.aws_region
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
variable "public_key" {
2+
description = "The public API key for MongoDB Atlas"
3+
}
4+
variable "private_key" {
5+
description = "The private API key for MongoDB Atlas"
6+
}
7+
variable "atlasprojectid" {
8+
description = "Atlas project ID"
9+
}
10+
variable "access_key" {
11+
description = "The access key for AWS Account"
12+
}
13+
variable "secret_key" {
14+
description = "The secret key for AWS Account"
15+
}
16+
variable "atlas_region" {
17+
default = "US_EAST_1"
18+
description = "Atlas Region"
19+
}
20+
variable "aws_region" {
21+
default = "us-east-1"
22+
description = "AWS Region"
23+
}
24+
variable "aws_account_id" {
25+
description = "My AWS Account ID"
26+
}
27+
variable "atlasorgid" {
28+
description = "Atlas Org ID"
29+
}
30+
variable "atlas_vpc_cidr" {
31+
description = "Atlas CIDR"
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
}
6+
mongodbatlas = {
7+
source = "terraform-providers/mongodbatlas"
8+
}
9+
}
10+
required_version = ">= 0.13"
11+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Example - Microsoft Azure and MongoDB Atlas VNET Peering
2+
3+
This project aims to provide an example of using Azure and MongoDB Atlas together.
4+
5+
6+
## Dependencies
7+
8+
* Terraform v0.13
9+
* Microsoft Azure account
10+
* A MongoDB Atlas account
11+
12+
```
13+
Terraform v0.13.0
14+
+ provider registry.terraform.io/hashicorp/azuread v1.0.0
15+
+ provider registry.terraform.io/hashicorp/azurerm v2.31.1
16+
+ provider registry.terraform.io/terraform-providers/mongodbatlas v0.6.5
17+
```
18+
19+
## Usage
20+
21+
**1\. Ensure your Azure credentials are set up.**
22+
23+
1. Install the Azure CLI by following the steps from the [official Azure documentation](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli).
24+
2. Run the command `az login` and this will take you to the default browser and perform the authentication.
25+
3. Once authenticated, it will print the user details as below:
26+
27+
```
28+
⇒ az login
29+
You have logged in. Now let us find all the subscriptions to which you have access...
30+
The following tenants don't contain accessible subscriptions. Use 'az login --allow-no-subscriptions' to have tenant level access.
31+
XXXXX
32+
[
33+
{
34+
"cloudName": "AzureCloud",
35+
"homeTenantId": "XXXXX",
36+
"id": "XXXXX",
37+
"isDefault": true,
38+
"managedByTenants": [],
39+
"name": "Pay-As-You-Go",
40+
"state": "Enabled",
41+
"tenantId": "XXXXX",
42+
"user": {
43+
"name": "person@domain.com",
44+
"type": "user"
45+
}
46+
}
47+
]
48+
```
49+
50+
**2\. TFVARS**
51+
52+
Now create **terraform.tfvars** file with all the variable values and make sure **not to commit it**.
53+
54+
**3\. Review the Terraform plan. **
55+
56+
Execute the below command and ensure you are happy with the plan.
57+
58+
``` bash
59+
$ terraform plan
60+
```
61+
This project currently does the below deployments:
62+
63+
- MongoDB Atlas Azure cluster - M10
64+
- Azure Resource Group, VNET, Service Principal, Role-Definition, Role-Association
65+
- Azure-MongoDB Atlas VNET Peering
66+
67+
**4\. Execute the Terraform apply.**
68+
69+
Now execute the plan to provision the AWS resources.
70+
71+
``` bash
72+
$ terraform apply
73+
```
74+
75+
**5\. Destroy the resources.**
76+
77+
Once you are finished your testing, ensure you destroy the resources to avoid unnecessary Azure and Atlas charges.
78+
79+
``` bash
80+
$ terraform destroy
81+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Configure the MongoDB Atlas Provider and connect via a key
2+
provider "mongodbatlas" {
3+
public_key = var.public_key
4+
private_key = var.private_key
5+
}
6+
# Create the mongodb atlas Azure cluster
7+
resource "mongodbatlas_cluster" "azure-cluster" {
8+
project_id = var.project_id
9+
name = var.name
10+
num_shards = 1
11+
12+
replication_factor = 3
13+
backup_enabled = false
14+
auto_scaling_disk_gb_enabled = true
15+
mongo_db_major_version = "4.2"
16+
17+
//Provider settings block in this case it is Azure
18+
provider_name = "AZURE"
19+
provider_disk_type_name = var.provider_disk_type_name
20+
provider_instance_size_name = var.provider_instance_size_name
21+
provider_region_name = var.provider_region_name
22+
}
23+
24+
# Create the peering connection request
25+
resource "mongodbatlas_network_peering" "test" {
26+
project_id = var.project_id
27+
container_id = mongodbatlas_cluster.azure-cluster.container_id
28+
provider_name = "AZURE"
29+
azure_directory_id = data.azurerm_client_config.current.tenant_id
30+
azure_subscription_id = data.azurerm_client_config.current.subscription_id
31+
resource_group_name = var.resource_group_name
32+
vnet_name = var.vnet_name
33+
atlas_cidr_block = var.atlas_cidr_block
34+
}

0 commit comments

Comments
 (0)