Quickstart: Deploy an Azure Cosmos DB to Azure Container Instances

Article tested with the following Terraform and Terraform provider versions:

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

This article shows how to use Terraform to deploy an Azure Cosmos DB to Azure Container Instances.

In this article, you learn how to:

  • Create an Azure Cosmos DB instance
  • Create an Azure Container Instance
  • Create an app that works across these two resources

Note

The example code in this article is located in the Microsoft Terraform GitHub repo.

Prerequisites

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.

Implement the Terraform code

  1. Create a directory in which to test the sample Terraform code and make it the current directory.

  2. Create a file named providers.tf and insert the following code:

    terraform {
      required_version = ">=0.12"
    
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    
  3. Create a file named main.tf and insert the following code:

    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    resource "random_integer" "ri" {
      min = 10000
      max = 99999
    }
    
    resource "azurerm_cosmosdb_account" "vote_cosmos_db" {
      name                = "tfex-cosmos-db-${random_integer.ri.result}"
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
      offer_type          = "Standard"
      kind                = "GlobalDocumentDB"
    
      consistency_policy {
        consistency_level       = "BoundedStaleness"
        max_interval_in_seconds = 10
        max_staleness_prefix    = 200
      }
    
      geo_location {
        location          = azurerm_resource_group.rg.location
        failover_priority = 0
      }
    }
    
  4. Create a file named aci.tf and insert the following code:

    resource "azurerm_container_group" "vote_aci" {
      name                = "vote-aci"
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
      ip_address_type     = "public"
      dns_name_label      = "vote-aci-${random_integer.ri.result}"
      os_type             = "linux"
    
      container {
        name   = "vote-aci"
        image  = "mcr.microsoft.com/azuredocs/azure-vote-front:cosmosdb"
        cpu    = "0.5"
        memory = "1.5"
        ports {
          port     = 80
          protocol = "TCP"
        }
    
        secure_environment_variables = {
          "COSMOS_DB_ENDPOINT"  = azurerm_cosmosdb_account.vote_cosmos_db.endpoint
          "COSMOS_DB_MASTERKEY" = azurerm_cosmosdb_account.vote_cosmos_db.primary_key
          "TITLE"               = "Azure Voting App"
          "VOTE1VALUE"          = "Cats"
          "VOTE2VALUE"          = "Dogs"
        }
      }
    }
    
  5. Create a file named variables.tf and insert the following code:

    variable "resource_group_location" {
      default     = "eastus"
      description = "Location of the resource group."
    }
    
    variable "resource_group_name_prefix" {
      default     = "rg"
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
    }
    
  6. Create a file named outputs.tf and insert the following code:

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "cosmosdb_account_name" {
      value = azurerm_cosmosdb_account.vote_cosmos_db.name
    }
    
    output "dns" {
      value = azurerm_container_group.vote_aci.fqdn
    }
    

Initialize Terraform

Run terraform init to initialize the Terraform deployment. This command downloads the Azure modules required to manage your Azure resources.

terraform init

Create a Terraform execution plan

Run terraform plan to create an execution plan.

terraform plan -out main.tfplan

Key points:

  • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
  • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
  • To read more about persisting execution plans and security, see the security warning section.

Apply a Terraform execution plan

Run terraform apply to apply the execution plan to your cloud infrastructure.

terraform apply main.tfplan

Key points:

  • The terraform apply command above assumes you previously ran terraform plan -out main.tfplan.
  • If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
  • If you didn't use the -out parameter, call terraform apply without any parameters.

Verify the results

  1. Get the resource group name.

    echo "$(terraform output resource_group_name)"
    
  2. Get the Azure Cosmos DB account name.

    echo "$(terraform output cosmosdb_account_name)"
    
  3. Run az cosmosdb sql database list/

    az cosmosdb sql database list \
      --resource-group <resource_group_name> \
      --account-name <cosmosdb_account_name>
    

Test application

  1. Get the Azure Cosmos DB account name.

    echo "$(terraform output dns)"
    
  2. Browse to the URL indicated in the previous step. You should see results similar to the following output:

    Azure vote application

Clean up resources

When you no longer need the resources created via Terraform, do the following steps:

  1. Run terraform plan and specify the destroy flag.

    terraform plan -destroy -out main.destroy.tfplan
    

    Key points:

    • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
    • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
    • To read more about persisting execution plans and security, see the security warning section.
  2. Run terraform apply to apply the execution plan.

    terraform apply main.destroy.tfplan
    

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps