Skip to content

Commit 3c3d72c

Browse files
authored
INTMDB-424: Add AWS and Azure serverless private link examples (#1043)
* Add AWS and Azure serverless private link examples * Remove unused variable
1 parent e12584c commit 3c3d72c

File tree

11 files changed

+433
-0
lines changed

11 files changed

+433
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 Serverless.
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 = data.mongodbatlas_serverless_instance.cluster_atlas.connection_strings_standard_srv
79+
}
80+
```
81+
**Outputs:**
82+
```
83+
atlasclusterstring = "mongodb+srv://cluster-atlas.za3fb.mongodb.net"
84+
85+
```
86+
87+
To fetch a private connection string, use the output of terraform as below after second apply:
88+
89+
```
90+
output "plstring" {
91+
value = mongodbatlas_serverless_instance.cluster_atlas.connection_strings_private_endpoint_srv[0]
92+
}
93+
```
94+
**Output:**
95+
```
96+
plstring = mongodb+srv://cluster-atlas-pe-0.za3fb.mongodb.net
97+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
resource "mongodbatlas_serverless_instance" "cluster_atlas" {
2+
project_id = var.atlasprojectid
3+
name = "cluster-atlas"
4+
provider_settings_backing_provider_name = "AWS"
5+
provider_settings_provider_name = "SERVERLESS"
6+
provider_settings_region_name = "US_EAST_1"
7+
continuous_backup_enabled = true
8+
}
9+
10+
data "mongodbatlas_serverless_instance" "cluster_atlas" {
11+
project_id = var.atlasprojectid
12+
name = mongodbatlas_serverless_instance.cluster_atlas.name
13+
depends_on = [mongodbatlas_privatelink_endpoint_service_serverless.atlaseplink]
14+
}
15+
16+
17+
output "atlasclusterstring" {
18+
value = data.mongodbatlas_serverless_instance.cluster_atlas.connection_strings_standard_srv
19+
}
20+
21+
/* Note Value not available until second apply*/
22+
/*
23+
output "plstring" {
24+
value = mongodbatlas_serverless_instance.cluster_atlas.connection_strings_private_endpoint_srv[0]
25+
}
26+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "mongodbatlas_privatelink_endpoint_serverless" "atlaspl" {
2+
project_id = var.atlasprojectid
3+
provider_name = "AWS"
4+
instance_name = mongodbatlas_serverless_instance.cluster_atlas.name
5+
}
6+
7+
resource "aws_vpc_endpoint" "ptfe_service" {
8+
vpc_id = aws_vpc.primary.id
9+
service_name = mongodbatlas_privatelink_endpoint_serverless.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_privatelink_endpoint_service_serverless" "atlaseplink" {
16+
project_id = mongodbatlas_privatelink_endpoint_serverless.atlaspl.project_id
17+
instance_name = mongodbatlas_serverless_instance.cluster_atlas.name
18+
endpoint_id = mongodbatlas_privatelink_endpoint_serverless.atlaspl.endpoint_id
19+
cloud_provider_endpoint_id = aws_vpc_endpoint.ptfe_service.id
20+
provider_name = "AWS"
21+
comment = "test"
22+
23+
}
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 "aws_region" {
17+
default = "us-east-1"
18+
description = "AWS Region"
19+
}
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 = "mongodb/mongodbatlas"
8+
}
9+
}
10+
required_version = ">= 0.13"
11+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Example - Microsoft Azure and MongoDB Atlas Private Endpoint Serverless
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+
* 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": "[email protected]",
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+
An serverless cluster in the project will be linked via the `cluster_name` variable.
55+
If included, the azure connection string to the cluster will be output.
56+
57+
**3\. Review the Terraform plan.**
58+
59+
Execute the below command and ensure you are happy with the plan.
60+
61+
``` bash
62+
$ terraform plan
63+
```
64+
This project currently does the below deployments:
65+
66+
- MongoDB Atlas Azure Private Endpoint
67+
- Azure Resource Group, VNET, Subnet, Private Endpoint
68+
- Azure-MongoDB Private Link
69+
70+
**4\. Execute the Terraform apply.**
71+
72+
Now execute the plan to provision the Azure resources.
73+
74+
``` bash
75+
$ terraform apply
76+
```
77+
78+
**5\. Destroy the resources.**
79+
80+
Once you are finished your testing, ensure you destroy the resources to avoid unnecessary Azure and Atlas charges.
81+
82+
``` bash
83+
$ terraform destroy
84+
```

0 commit comments

Comments
 (0)