Skip to content

Commit e899f0f

Browse files
authored
Merge pull request #21 from MicrosoftCloudEssentials-LearningHub/mongo-atlas
Mongo atlas -> Set Up a Network Peering Connection
2 parents c69448a + 6e45872 commit e899f0f

File tree

10 files changed

+449
-2
lines changed

10 files changed

+449
-2
lines changed

1_storage-databases/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Costa Rica
55
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
66
[brown9804](https://github.com/brown9804)
77

8-
Last updated: 2025-03-24
8+
Last updated: 2025-03-27
99

1010
------------------------------------------
1111

@@ -22,6 +22,7 @@ Last updated: 2025-03-24
2222
- [PostgreSQL Server](./1_storage-databases/postgresql)
2323
- [Azure SQL Database](./1_storage-databases/sql-database)
2424
- [Azure Storage Account](./1_storage-databases/storage-account)
25+
- [MongoDB Atlas with Azure Network Peering](./1_storage-databases/mongo-atlas)
2526

2627
<div align="center">
2728
<h3 style="color: #4CAF50;">Total Visitors</h3>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Terraform Template - MongoDB Atlas with Azure Network Peering
2+
3+
Costa Rica
4+
5+
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
6+
[brown9804](https://github.com/brown9804)
7+
8+
Last updated: 2025-03-27
9+
10+
----------
11+
12+
> This template contains Terraform configurations to create and manage MongoDB Atlas with Azure network peering. Below is a description of the files and the variables used in this template.
13+
14+
<details>
15+
<summary><b>List of References </b> (Click to expand)</summary>
16+
17+
- [AzureRM - MongoDB Atlas Provider](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs)
18+
- [MongoDB - Set Up a Network Peering Connection](https://www.mongodb.com/docs/atlas/security-vpc-peering/)
19+
- [MongoDB - Manage Identity Providers](https://www.mongodb.com/docs/atlas/security/manage-federated-auth/)
20+
- [Resource: mongodbatlas_network_peering](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/network_peering)
21+
- [MongoDB Atlas - Limits](https://www.mongodb.com/docs/atlas/reference/limitations/#std-label-limits)
22+
- [Resource: azuread_service_principal](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/service_principal)
23+
- [Resource: azuread_application](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application.html)
24+
- [Resource: azuread_service_principal_password](https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/service_principal_password)
25+
- [Deploying MongoDB Atlas With Terraform with Azure](https://www.mongodb.com/developer/products/atlas/mongodb-atlas-deployment-with-terraform/?msockid=38ec3806873362243e122ce086486339)
26+
27+
</details>
28+
29+
30+
> [!NOTE]
31+
> The MongoDB Atlas network peering depends on the Azure resources. Terraform ensures that these resources are created before the peering by using the `depends_on` argument in the configuration.
32+
33+
<p align="center">
34+
<img width="150" alt="image" src="https://github.com/user-attachments/assets/14f80abf-1cc2-4286-bb07-05dd24a0121d">
35+
</p>
36+
37+
<p align="center">
38+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/2963fec6-0ec5-47b6-b203-a18b15a1d9a6">
39+
</p>
40+
41+
<p align="center">
42+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/be7ee535-f2c8-4c33-9fa2-a75025366097">
43+
</p>
44+
45+
<p align="center">
46+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/62f3c882-2ca5-4abf-9de0-fa83857ec03d">
47+
</p>
48+
49+
50+
<p align="center">
51+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/de1d3b05-1596-4be0-8a35-73d472b17478">
52+
</p>
53+
54+
> [!TIP]
55+
> For this demostration, I used promotional codes that I found for MongoDB Atlas that will give you $100 FREE MONGODB credits: `GETATLAS `
56+
57+
## Prerequisites
58+
59+
- An `Azure subscription is required`. All other resources, including instructions for creating a Resource Group, are provided in this workshop.
60+
- `Contributor role assigned or any custom role that allows`: access to manage all resources, and the ability to deploy resources within subscription.
61+
- If you choose to use the Terraform approach, please ensure that:
62+
- [Terraform is installed on your local machine](https://developer.hashicorp.com/terraform/tutorials/azure-get-started/install-cli#install-terraform).
63+
- [Install the Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) to work with both Terraform and Azure commands.
64+
- A MongoDB Atlas account. If you don't have one yet, just sign up for a [free MongoDB Atlas account here](https://www.mongodb.com/cloud/atlas/register).
65+
66+
https://github.com/user-attachments/assets/1507a6a7-b74b-4ec6-8118-433ef52e6d8f
67+
68+
## Permissions required
69+
70+
> Please read [terraform documentation on azurerm specifics](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/network_peering). To create the peering request with an Azure VNET, you need to grant Atlas the following permissions on the virtual network: <br/>
71+
> - Microsoft.Network/virtualNetworks/virtualNetworkPeerings/read <br/>
72+
> - Microsoft.Network/virtualNetworks/virtualNetworkPeerings/write <br/>
73+
> - Microsoft.Network/virtualNetworks/virtualNetworkPeerings/delete <br/>
74+
> - Microsoft.Network/virtualNetworks/peer/action <br/>
75+
> For more information, see [Set Up a Network Peering Connection](https://docs.atlas.mongodb.com/security-vpc-peering/) and [Mongo DB network peering](https://docs.atlas.mongodb.com/reference/api/vpc-create-peering-connection/)
76+
77+
> For example:
78+
79+
https://github.com/user-attachments/assets/2f71a62f-1d8c-41f4-9403-efb84028056c
80+
81+
## File Descriptions
82+
83+
- **main.tf**: Contains the main configuration for creating the MongoDB Atlas network peering and the Azure resources that it depends on.
84+
- **variables.tf**: Defines the input variables used in the Terraform configuration.
85+
- **provider.tf**: Configures the providers to interact with MongoDB Atlas and Azure resources.
86+
- **terraform.tfvars**: Provides default values for the variables defined in `variables.tf`.
87+
- **outputs.tf**: Defines the outputs of the Terraform configuration, such as the peering connection details.
88+
89+
## Variables
90+
91+
Below is a list of variables used in this template, their expected values, types, and examples:
92+
93+
| Variable Name | Description | Type | Example Value |
94+
|-----------------------------|--------------------------------------------------|---------|---------------------------|
95+
| `azure_subscription_id` | The Azure subscription ID. | string | `"your-subscription-id"` |
96+
| `resource_group_name` | The name of the Azure resource group. | string | `"your-resource-group"` |
97+
| `vnet_name` | The name of the Azure virtual network. | string | `"your-vnet-name"` |
98+
| `azure_vnet_cidr_block` | The CIDR block for the Azure virtual network. | string | `"10.1.0.0/16"` |
99+
| `atlas_cidr_block` | The CIDR block for the MongoDB Atlas network container. | string | `"192.168.1.0/24"` |
100+
| `location` | The location/region where the resources will be created. | string | `"West US"` |
101+
| `mongodbatlas_org_id` | The organization ID for MongoDB Atlas. | string | `"your-org-id"` |
102+
| `azure_directory_id` | The Azure directory ID. | string | `"your-directory-id"` |
103+
| `mongodbatlas_public_key` | The public key for MongoDB Atlas. | string | `"your-public-key"` |
104+
| `mongodbatlas_private_key` | The private key for MongoDB Atlas. | string | `"your-private-key"` |
105+
106+
## How to create MongoDB Atlas public/private key
107+
108+
1. Go to `Access Manager`
109+
2. Click on `Add new`
110+
111+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/a96f3604-2e9c-4a72-8984-747577f2114d">
112+
113+
3. Select API Key, click on create new, and store the values securely
114+
115+
<img width="350" alt="image" src="https://github.com/user-attachments/assets/4e774329-6af5-4e76-a6f8-13d25c30be08">
116+
117+
https://github.com/user-attachments/assets/7c42f0c5-37cf-4d1b-9f48-ccf95d6c5f4d
118+
119+
## How it works
120+
121+
> Overall, to set up network peering between Azure and MongoDB Atlas using Terraform, start by creating a MongoDB Atlas account, project, network container, and cluster. Ensure you have an Azure virtual network (VNet) configured and [grant necessary permissions to the Azure service principal to enable it to peer with MongoDB Atlas](#permissions-required). Deploy the VNet peering module to establish the connection between the Azure VNet and MongoDB Atlas, making sure the CIDR blocks do not overlap to avoid conflicts. You can use this template, and run the stardard terraform commands to deploy the infrastrcture, click [here if you need more information about it](https://github.com/MicrosoftCloudEssentials-LearningHub/AzureTerraformTemplates-v0.0.0/tree/mongo-atlas?tab=readme-ov-file#how-to-execute-it) <br/>
122+
123+
https://github.com/user-attachments/assets/95f0c5cd-b1b9-422d-a32d-4cfdb98a5a1e
124+
125+
<div align="center">
126+
<h3 style="color: #4CAF50;">Total Visitors</h3>
127+
<img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
128+
</div>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Deployment known errors - Overview
2+
3+
Costa Rica
4+
5+
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
6+
[brown9804](https://github.com/brown9804)
7+
8+
Last updated: 2025-03-27
9+
10+
----------
11+
12+
> Here is a brief overview of error messages and their solutions.
13+
14+
## Content
15+
16+
- [401 - Unauthorized](#unauthorized)
17+
- [500 - Internal Server Error](#internal-server-error)
18+
19+
## 401 Unauthorized
20+
21+
<p align="center">
22+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/7c42ef92-6f1e-4dac-a105-f0b5ed4dee4c" />
23+
</p>
24+
25+
```
26+
│ Error: error creating project: Terraform Project
27+
28+
│ with mongodbatlas_project.example,
29+
│ on main.tf line 42, in resource "mongodbatlas_project" "example":
30+
│ 42: resource "mongodbatlas_project" "example" {
31+
32+
│ https://cloud.mongodb.com/api/atlas/v2/groups POST: HTTP 401 Unauthorized (Error code: "") Detail: You are not
33+
│ authorized for this resource. Reason: Unauthorized. Params: [], BadRequestDetail:
34+
```
35+
36+
> [!TIP]
37+
> Ensure you have added the user, group, or identity to the Atlas organization. This issue can also be resolved by leveraging public and private keys to connect. Check [this for more information about it](
38+
39+
## 500 Internal Server Error
40+
41+
<p align="center">
42+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/482a289c-e912-40e2-b13a-4167a763cdfe" />
43+
</p>
44+
45+
46+
```
47+
48+
│ Error: error creating MongoDB Network Peering Connection: https://cloud.mongodb.com/api/atlas/v2/groups/677a5/peers POST: HTTP 500 Internal Server Error (Error code: "UNEXPECTED_ERROR") Detail: Unexpected error. Reason: Internal Server Error. Params: [], BadRequestDetail:
49+
50+
│ with mongodbatlas_network_peering.azure_mongodb,
51+
│ on main.tf line 71, in resource "mongodbatlas_network_peering" "azure_mongodb":
52+
│ 71: resource "mongodbatlas_network_peering" "azure_mongodb" {
53+
54+
```
55+
56+
> [!TIP]
57+
> Please ensure you have configured the Terraform module correctly. You can use [this template as a guide](/1_storage-databases/mongo-atlas/main.tf).
58+
> Also, make sure you have created the service principal with the correct permissions. [Validate the connection before continuing, as shown in the video](https://github.com/MicrosoftCloudEssentials-LearningHub/AzureTerraformTemplates-v0.0.0/tree/main/1_storage-databases/mongo-atlas#permissions-required).
59+
60+
<div align="center">
61+
<h3 style="color: #4CAF50;">Total Visitors</h3>
62+
<img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
63+
</div>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Main configuration file for defining MongoDB Atlas network peering and Azure resources
2+
3+
# Notes:
4+
# To assign the necessary permissions to the user running Terraform, please review
5+
# how to create a service principal (if you don't have one) and assign a custom role to it.
6+
# - Use the `azurerm_resource_group` resource to create a resource group in Azure.
7+
# This is a container that holds related resources for an Azure solution.
8+
# - Use the `azurerm_virtual_network` resource to create a virtual network in Azure.
9+
# This network will be used for the peering connection with MongoDB Atlas.
10+
# - Use the `mongodbatlas_project` resource to create a project in MongoDB Atlas.
11+
# A project is a logical grouping of clusters, users, and other resources.
12+
# - Use the `mongodbatlas_network_container` resource to define the CIDR block
13+
# and provider details for the peering connection. This container holds the
14+
# network settings for the peering connection.
15+
# - Use the `mongodbatlas_network_peering` resource to create
16+
# the peering connection request with Azure VNET. This resource
17+
# establishes the network peering between MongoDB Atlas and Azure.
18+
# - The `mongodbatlas_advanced_cluster` resource is used to create a MongoDB Atlas cluster once the peering connection is established.
19+
# This resource defines the cluster configuration, including replication specs
20+
# and backup settings. Note that VNET peering is only supported for dedicated clusters (M10 and above).
21+
22+
#----------------------------------------------------------------
23+
# Create the Azure Resource Group
24+
#----------------------------------------------------------------
25+
resource "azurerm_resource_group" "example" {
26+
name = var.resource_group_name
27+
location = var.location
28+
}
29+
30+
#----------------------------------------------------------------
31+
# Create the Azure Virtual Network
32+
#----------------------------------------------------------------
33+
resource "azurerm_virtual_network" "example" {
34+
name = var.vnet_name
35+
address_space = [var.azure_vnet_cidr_block] # Ensure this CIDR block does not overlap with existing peering connections
36+
location = var.location
37+
resource_group_name = azurerm_resource_group.example.name
38+
}
39+
40+
#----------------------------------------------------------------
41+
# Create the MongoDB Atlas Project
42+
#----------------------------------------------------------------
43+
resource "mongodbatlas_project" "example" {
44+
name = "Terraform Project" # Change for your project name
45+
org_id = var.mongodbatlas_org_id
46+
}
47+
48+
#----------------------------------------------------------------
49+
# Create the MongoDB Atlas Network Container
50+
# Container example provided but not always required,
51+
# see network_container documentation for details.
52+
#----------------------------------------------------------------
53+
resource "mongodbatlas_network_container" "example" {
54+
project_id = mongodbatlas_project.example.id
55+
atlas_cidr_block = var.atlas_cidr_block
56+
provider_name = "AZURE"
57+
region = "US_EAST_2"
58+
}
59+
60+
#----------------------------------------------------------------
61+
# Create the MongoDB Atlas Cluster
62+
#----------------------------------------------------------------
63+
resource "mongodbatlas_advanced_cluster" "test" {
64+
project_id = mongodbatlas_project.example.id
65+
name = "terraform-deployment-test" # Choose cluster name
66+
cluster_type = "REPLICASET"
67+
backup_enabled = true
68+
replication_specs {
69+
region_configs {
70+
priority = 7
71+
provider_name = "AZURE"
72+
region_name = "US_EAST_2"
73+
electable_specs {
74+
instance_size = "M10" # Depending on the tier, clusters have certain configuration limits
75+
node_count = 3
76+
}
77+
}
78+
}
79+
80+
depends_on = [mongodbatlas_network_container.example]
81+
}
82+
83+
#----------------------------------------------------------------
84+
# Introduce a wait time before creating the MongoDB Atlas Network Peering
85+
#----------------------------------------------------------------
86+
resource "time_sleep" "wait_5_minutes" {
87+
depends_on = [
88+
mongodbatlas_advanced_cluster.test
89+
]
90+
91+
create_duration = "5m"
92+
}
93+
94+
#----------------------------------------------------------------
95+
# Create the MongoDB Atlas Network Peering
96+
#----------------------------------------------------------------
97+
resource "mongodbatlas_network_peering" "azure_mongodb" {
98+
project_id = mongodbatlas_project.example.id
99+
container_id = mongodbatlas_network_container.example.id
100+
provider_name = "AZURE"
101+
azure_directory_id = var.azure_directory_id
102+
azure_subscription_id = var.azure_subscription_id
103+
resource_group_name = var.resource_group_name
104+
vnet_name = var.vnet_name
105+
106+
depends_on = [
107+
azurerm_resource_group.example,
108+
azurerm_virtual_network.example,
109+
time_sleep.wait_5_minutes
110+
]
111+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
output "resource_group_name" {
2+
description = "The name of the resource group created in Azure."
3+
value = azurerm_resource_group.example.name
4+
}
5+
6+
output "vnet_name" {
7+
description = "The name of the virtual network created in Azure."
8+
value = azurerm_virtual_network.example.name
9+
}
10+
11+
output "mongodbatlas_project_id" {
12+
description = "The ID of the MongoDB Atlas project."
13+
value = mongodbatlas_project.example.id
14+
}
15+
16+
output "mongodbatlas_network_container_id" {
17+
description = "The ID of the MongoDB Atlas network container."
18+
value = mongodbatlas_network_container.example.id
19+
}
20+
21+
output "mongodbatlas_network_peering_id" {
22+
description = "The ID of the MongoDB Atlas network peering."
23+
value = mongodbatlas_network_peering.azure_mongodb.id
24+
}
25+
26+
output "mongodbatlas_cluster_id" {
27+
description = "The ID of the MongoDB Atlas cluster."
28+
value = mongodbatlas_advanced_cluster.test.id
29+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Name": "AtlasPeering",
3+
"IsCustom": true,
4+
"Description": "Grants MongoDB access to manage peering connections on network",
5+
"Actions": [
6+
"Microsoft.Network/virtualNetworks/virtualNetworkPeerings/read",
7+
"Microsoft.Network/virtualNetworks/virtualNetworkPeerings/write",
8+
"Microsoft.Network/virtualNetworks/virtualNetworkPeerings/delete",
9+
"Microsoft.Network/virtualNetworks/peer/action"
10+
],
11+
"AssignableScopes": [
12+
"/subscriptions/{your-subscription-id}"
13+
]
14+
}

0 commit comments

Comments
 (0)