|
| 1 | +# Terraform Template - MongoDB Atlas with Azure Network Peering |
| 2 | + |
| 3 | +Costa Rica |
| 4 | + |
| 5 | +[](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> |
0 commit comments