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.
Configure Terraform: If you haven't already done so, configure Terraform using one of the following options:
Implement the Terraform code
Create a directory in which to test the sample Terraform code and make it the current directory.
Create a file named
providers.tf
and insert the following code:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
Create a file named
main.tf
and insert the following code:resource "azurerm_resource_group" "rg" { name = "${random_pet.rg_name.id}-rg" location = var.resource_group_location } resource "azurerm_cosmosdb_account" "vote_cosmos_db" { name = "${random_pet.rg_name.id}-${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 } } resource "random_integer" "ri" { min = 10000 max = 99999 } resource "random_pet" "rg_name" { prefix = var.prefix }
Create a file named
aci.tf
and insert the following code:resource "azurerm_container_group" "main" { name = "${random_pet.rg_name.id}-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" } } }
Create a file named
variables.tf
and insert the following code:variable "resource_group_location" { default = "eastus" description = "Location of the resource group." } variable "prefix" { type = string default = "cosmos-db-aci" description = "Prefix of the resource name" }
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.main.fqdn }
Initialize Terraform
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
terraform init -upgrade
Key points:
- The
-upgrade
parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.
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.
Apply a Terraform execution plan
Run terraform apply to apply the execution plan to your cloud infrastructure.
terraform apply main.tfplan
Key points:
- The example
terraform apply
command assumes you previously ranterraform plan -out main.tfplan
. - If you specified a different filename for the
-out
parameter, use that same filename in the call toterraform apply
. - If you didn't use the
-out
parameter, callterraform apply
without any parameters.
Verify the results
Get the resource group name.
echo "$(terraform output resource_group_name)"
Get the Azure Cosmos DB account name.
echo "$(terraform output cosmosdb_account_name)"
Run az cosmosdb sql database list/
az cosmosdb sql database list \ --resource-group <resource_group_name> \ --account-name <cosmosdb_account_name>
Test application
Get the Azure Cosmos DB account name.
echo "$(terraform output dns)"
Browse to the URL indicated in the previous step. You should see results similar to the following output:
Clean up resources
When you no longer need the resources created via Terraform, do the following steps:
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.
- The
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
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for