Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this quickstart, you deploy a new Azure DocumentDB cluster using Terraform. This quickstart provides step-by-step instructions to help you get started quickly. This cluster contains all your MongoDB resources: databases, collections, and documents. It provides a unique endpoint for tools and software development kits (SDKs) to connect to Azure DocumentDB and perform operations.
Prerequisites
An Azure subscription
- If you don't have an Azure subscription, create a free account
- Terraform 1.2.0 or later.
Use the Bash environment in Azure Cloud Shell. For more information, see Get started with Azure Cloud Shell.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Authenticate to Azure using Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use and manage extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
Configure environment
Set up your Azure CLI environment to manage Azure DocumentDB resources in your subscription.
Start in an empty directory.
Sign in to Azure CLI.
az loginCheck your target Azure subscription.
az account showNote
If you aren't connected to the subscription you expected, use this command to change your subscription:
az account set --subscription "<subscription-name>"For more information, see manage Azure subscriptions with the Azure CLI.
Prepare the Terraform configuration
Create and configure a Terraform file to define the resources required for deploying an Azure DocumentDB cluster.
Create a new main.tf file in your project directory.
Add this configuration to the file's content.
variable "admin_username" { type = string description = "Username for default administrator account" } variable "admin_password" { type = string description = "Password for default administrator account" sensitive = true } terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 4.0" } } } provider "azurerm" { features { } } resource "azurerm_resource_group" "resource_group" { name = "example-resource-group" location = "West US" } resource "azurerm_mongo_cluster" "cluster" { name = "example-mongo-cluster" resource_group_name = azurerm_resource_group.resource_group.name location = azurerm_resource_group.resource_group.location administrator_username = var.admin_username administrator_password = var.admin_password shard_count = "1" compute_tier = "M10" high_availability_mode = "Disabled" storage_size_in_gb = "32" version = "8.0" }Tip
For more information on options using the
azurerm_mongo_clusterresource, seeazurermprovider documentation in Terraform Registry.
Deploy the configuration
Deploy the configuration file created in the previous step using an execution plan.
Initialize the Terraform deployment with Terraform CLI.
terraform init --upgradeCreate an execution plan, and save it to a file named main.tfplan. Provide values when prompted for the
admin_usernameandadmin_passwordvariables.ARM_SUBSCRIPTION_ID=$(az account show --query id --output tsv) terraform plan --out "main.tfplan"Note
This command sets the
ARM_SUBSCRIPTION_IDenvironment variable temporarily. This setting is required for theazurermprovider starting with version 4.0 For more information, see subscription ID inazurerm.Apply the execution plan to deploy resources to Azure.
ARM_SUBSCRIPTION_ID=$(az account show --query id --output tsv) terraform apply "main.tfplan"Wait for the deployment operation to complete before moving on.
Review deployed resources
List the Azure DocumentDB resources deployed to your resource group.
Use
az resource listto get a list of resources in your resource group.az resource list \ --resource-group "<resource-group-name>" \ --namespace "Microsoft.DocumentDB" \ --resource-type "mongoClusters" \ --query "[].name" \ --output jsonIn the example output, look for resources that have a type of
Microsoft.DocumentDB/mongoClusters. Here's an example of the type of output to expect:[ "msdocs-documentdb-example-cluster" ]
Clean up resources
Remove all the resources defined in your Terraform configuration.
Destroy your resources managed by Terraform using the
destroycommand.ARM_SUBSCRIPTION_ID=$(az account show --query id --output tsv) terraform destroyTip
Alternatively, use
az group deleteto remove the resource group from your subscription:az group delete \ --name "<resource-group-name>" \ --yes \ --no-waitImportant
Ensure you no longer need the resources before running this command, as it permanently deletes them.
Confirm any relevant prompts to proceed with the deletion.