Quickstart - Create an Azure Cosmos DB database and container using Terraform


Azure Cosmos DB is Microsoft’s fast NoSQL database with open APIs for any scale. You can use Azure Cosmos DB to quickly create and query key/value databases, document databases, and graph databases. Without a credit card or an Azure subscription, you can set up a free Try Azure Cosmos DB account. This quickstart focuses on the process of deployments via Terraform to create an Azure Cosmos database and a container within that database. You can later store data in this container.


An Azure subscription or free Azure Cosmos DB trial account

Terraform should be installed on your local computer. Installation instructions can be found here.

Review the Terraform File

The Terraform files used in this quickstart can be found on the terraform samples repository. Create the below three files: providers.tf, main.tf and variables.tf. Variables can be set in command line or alternatively with a terraforms.tfvars file.

Provider Terraform File

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=3.0.0"

provider "azurerm" {
  features {}

Main Terraform File

resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = var.location

resource "azurerm_cosmosdb_account" "example" {
  name                      = var.cosmosdb_account_name
  location                  = var.cosmosdb_account_location
  resource_group_name       = azurerm_resource_group.example.name
  offer_type                = "Standard"
  kind                      = "GlobalDocumentDB"
  enable_automatic_failover = false
  geo_location {
    location          = var.location
    failover_priority = 0
  consistency_policy {
    consistency_level       = "BoundedStaleness"
    max_interval_in_seconds = 300
    max_staleness_prefix    = 100000
  depends_on = [

resource "azurerm_cosmosdb_sql_database" "example" {
  name                = var.cosmosdb_sqldb_name
  resource_group_name = azurerm_resource_group.example.name
  account_name        = azurerm_cosmosdb_account.example.name
  throughput          = var.throughput

resource "azurerm_cosmosdb_sql_container" "example" {
  name                  = var.sql_container_name
  resource_group_name   = azurerm_resource_group.example.name
  account_name          = azurerm_cosmosdb_account.example.name
  database_name         = azurerm_cosmosdb_sql_database.example.name
  partition_key_path    = "/definition/id"
  partition_key_version = 1
  throughput            = var.throughput

  indexing_policy {
    indexing_mode = "consistent"

    included_path {
      path = "/*"

    included_path {
      path = "/included/?"

    excluded_path {
      path = "/excluded/?"

  unique_key {
    paths = ["/definition/idlong", "/definition/idshort"]

Variables Terraform File

variable "resource_group_name" {
  type        = string
  description = "Resource group name"

variable "location" {
  type        = string
  description = "Resource group location"

variable "cosmosdb_account_name" {
  type        = string
  description = "Cosmos db account name"

variable "cosmosdb_account_location" {
  type        = string
  description = "Cosmos db account location"

variable "cosmosdb_sqldb_name" {
  type        = string
  description = "value"

variable "sql_container_name" {
  type        = string
  description = "SQL API container name."

variable "throughput" {
  type        = number
  description = "Cosmos db database throughput"
  validation {
    condition     = var.throughput >= 400 && var.throughput <= 1000000
    error_message = "Cosmos db manual throughput should be equal to or greater than 400 and less than or equal to 1000000."
  validation {
    condition     = var.throughput % 100 == 0
    error_message = "Cosmos db throughput should be in increments of 100."

Three Cosmos DB resources are defined in the main terraform file.

Deploy via terraform

  1. Save the terraform files as main.tf, variables.tf and providers.tf to your local computer.
  2. Sign in to your terminal via Azure CLI or PowerShell
  3. Deploy via Terraform commands
    • terraform init
    • terraform plan
    • terraform apply

Validate the deployment

Use the Azure portal, Azure CLI, or Azure PowerShell to list the deployed resources in the resource group.

az resource list --resource-group "your resource group name"

Clean up resources

If you plan to continue working with subsequent quickstarts and tutorials, you might want to leave these resources in place. When no longer needed, use the Azure portal, Azure CLI, or Azure PowerShell to delete the resource group and its resources.

az group delete --name "your resource group name"

Next steps

In this quickstart, you created an Azure Cosmos account, a database and a container via terraform and validated the deployment. To learn more about Azure Cosmos DB and Terraform, continue on to the articles below.