|
| 1 | +--- |
| 2 | +title: | |
| 3 | + Quickstart: Create a cluster with Terraform |
| 4 | +titleSuffix: Azure Cosmos DB for MongoDB vCore |
| 5 | +description: In this quickstart, create a new Azure Cosmos DB for MongoDB vCore cluster to store databases, collections, and documents by using Terraform. |
| 6 | +author: gahl-levy |
| 7 | +ms.author: gahllevy |
| 8 | +ms.service: cosmos-db |
| 9 | +ms.subservice: mongodb-vcore |
| 10 | +ms.topic: quickstart |
| 11 | +ms.date: 03/18/2024 |
| 12 | +--- |
| 13 | + |
| 14 | +# Azure Cosmos DB for MongoDB (vCore) with Terraform |
| 15 | +This document provides instructions on using Terraform to deploy Azure Cosmos DB for MongoDB vCore resources. This workaround involves directly calling the ARM API through Terraform, as the current AzureRM provider does not natively support the preview version of the ARM API for Azure Cosmos DB vCore. |
| 16 | + |
| 17 | +# Prerequisites |
| 18 | +- Intermediate to advanced knowledge of Terraform and Azure. |
| 19 | +- installed on your machine. |
| 20 | +- An Azure subscription. |
| 21 | + |
| 22 | +## Terraform Configuration |
| 23 | +Create a main.tf file and include the following configuration: |
| 24 | + |
| 25 | +```hcl |
| 26 | +terraform { |
| 27 | + required_providers { |
| 28 | + azurerm = { # <--- Note that it is azurerm |
| 29 | + source = "hashicorp/azurerm" |
| 30 | + version = "3.94.0" |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +provider "azurerm" { |
| 35 | + features {} |
| 36 | +} |
| 37 | +resource "azurerm_resource_group" "example" { |
| 38 | + name = "gemin" |
| 39 | + location = "West Europe" |
| 40 | +} |
| 41 | +resource "azurerm_resource_group_template_deployment" "terraform-arm" { |
| 42 | + name = "terraform-arm-01" |
| 43 | + resource_group_name = azurerm_resource_group.example.name |
| 44 | + deployment_mode = "Incremental" |
| 45 | + template_content = file("template.json") |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Create a template.json file and populate it with the following JSON content, making sure to replace placeholder values (CLUSTER_NAME, TEMPLATE_NAME, region, node specs, administratorLogin, administratorLoginPassword), with your specific configurations: |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "$schema": https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#, |
| 54 | + "contentVersion": "1.0.0.0", |
| 55 | + "parameters": { |
| 56 | + "CLUSTER_NAME": { // replace |
| 57 | + "defaultValue": "TEMPLATE_NAME", // replace |
| 58 | + "type": "String" |
| 59 | + } |
| 60 | + }, |
| 61 | + "variables": {}, |
| 62 | + "resources": [ |
| 63 | + { |
| 64 | + "type": "Microsoft.DocumentDB/mongoClusters", |
| 65 | + "apiVersion": "2023-11-15-preview", |
| 66 | + "name": "[parameters('CLUSTER_NAME')]", // replace |
| 67 | + "location": "westeurope", // replace if needed |
| 68 | + "properties": { |
| 69 | + "clusterStatus": "Ready", |
| 70 | + "administratorLogin": "", // replace |
| 71 | + "administratorLoginPassword" : "", // replace |
| 72 | + "serverVersion": "6.0", |
| 73 | + "nodeGroupSpecs": [ |
| 74 | + { |
| 75 | + "kind": "Shard", |
| 76 | + "sku": "M40", // replace if needed |
| 77 | + "diskSizeGB": 128, |
| 78 | + "enableHa": false, // replace if needed |
| 79 | + "nodeCount": 1 |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | + }, |
| 84 | + { |
| 85 | + "type": "Microsoft.DocumentDB/mongoClusters/firewallRules", |
| 86 | + "apiVersion": "2023-11-15-preview", |
| 87 | + "name": "[concat(parameters('CLUSTER_NAME'), '/allowAll')]", // replace |
| 88 | + "dependsOn": [ |
| 89 | + "[resourceId('Microsoft.DocumentDB/mongoClusters', parameters('CLUSTER_NAME'))]" // replace |
| 90 | + ], |
| 91 | + "properties": { |
| 92 | + "startIpAddress": "0.0.0.0", |
| 93 | + "endIpAddress": "255.255.255.255" |
| 94 | + } |
| 95 | + }, |
| 96 | + { |
| 97 | + "type": "Microsoft.DocumentDB/mongoClusters/firewallRules", |
| 98 | + "apiVersion": "2023-11-15-preview", |
| 99 | + "name": "[concat(parameters('CLUSTER_NAME'), '/AllowAllAzureServicesAndResourcesWithinAzureIps_2023-12-6_17-3-22')]", // replace |
| 100 | + "dependsOn": [ |
| 101 | + "[resourceId('Microsoft.DocumentDB/mongoClusters', parameters('CLUSTER_NAME'))]" // replace |
| 102 | + ], |
| 103 | + "properties": { |
| 104 | + "startIpAddress": "0.0.0.0", |
| 105 | + "endIpAddress": "0.0.0.0" |
| 106 | + } |
| 107 | + }, |
| 108 | + { |
| 109 | + "type": "Microsoft.DocumentDB/mongoClusters/firewallRules", |
| 110 | + "apiVersion": "2023-11-15-preview", |
| 111 | + "name": "[concat(parameters('CLUSTER_NAME'), '/allowAzure')]", // replace |
| 112 | + "dependsOn": [ |
| 113 | + "[resourceId('Microsoft.DocumentDB/mongoClusters', parameters('CLUSTER_NAME'))]" // replace |
| 114 | + ], |
| 115 | + "properties": { |
| 116 | + "startIpAddress": "0.0.0.0", |
| 117 | + "endIpAddress": "0.0.0.0" |
| 118 | + } |
| 119 | + } |
| 120 | + ] |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Deployment |
| 125 | +Execute the following commands to initialize your Terraform workspace, create an execution plan, and apply the plan to deploy your resources: |
| 126 | + |
| 127 | +```bash |
| 128 | +terraform init -upgrade |
| 129 | +terraform plan -out main.tfplan |
| 130 | +terraform apply "main.tfplan" |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +## Next steps |
| 135 | + |
| 136 | +> [!div class="nextstepaction"] |
| 137 | +> [Migration options for Azure Cosmos DB for MongoDB vCore](migration-options.md) |
0 commit comments