Edit

Share via


Quickstart: Deploy an Azure DocumentDB cluster using Terraform

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

Configure environment

Set up your Azure CLI environment to manage Azure DocumentDB resources in your subscription.

  1. Start in an empty directory.

  2. Sign in to Azure CLI.

    az login
    
  3. Check your target Azure subscription.

    az account show
    

    Note

    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.

  1. Create a new main.tf file in your project directory.

  2. 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_cluster resource, see azurerm provider documentation in Terraform Registry.

Deploy the configuration

Deploy the configuration file created in the previous step using an execution plan.

  1. Initialize the Terraform deployment with Terraform CLI.

    terraform init --upgrade
    
  2. Create an execution plan, and save it to a file named main.tfplan. Provide values when prompted for the admin_username and admin_password variables.

    ARM_SUBSCRIPTION_ID=$(az account show --query id --output tsv) terraform plan --out "main.tfplan"
    

    Note

    This command sets the ARM_SUBSCRIPTION_ID environment variable temporarily. This setting is required for the azurerm provider starting with version 4.0 For more information, see subscription ID in azurerm.

  3. Apply the execution plan to deploy resources to Azure.

    ARM_SUBSCRIPTION_ID=$(az account show --query id --output tsv) terraform apply "main.tfplan"
    
  4. Wait for the deployment operation to complete before moving on.

Review deployed resources

List the Azure DocumentDB resources deployed to your resource group.

  1. Use az resource list to 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 json
    
  2. In 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.

  1. Destroy your resources managed by Terraform using the destroy command.

    ARM_SUBSCRIPTION_ID=$(az account show --query id --output tsv) terraform destroy
    

    Tip

    Alternatively, use az group delete to remove the resource group from your subscription:

    az group delete \
        --name "<resource-group-name>" \
        --yes \
        --no-wait
    

    Important

    Ensure you no longer need the resources before running this command, as it permanently deletes them.

  2. Confirm any relevant prompts to proceed with the deletion.