Azure CosmosDB (DocumentDB) SQL Container

This page shows how to write Terraform and Azure Resource Manager for CosmosDB (DocumentDB) SQL Container and write them securely.

azurerm_cosmosdb_sql_container (Terraform)

The SQL Container in CosmosDB (DocumentDB) can be configured in Terraform with the resource name azurerm_cosmosdb_sql_container. The following sections describe 9 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L49
resource "azurerm_cosmosdb_sql_container" "users_access_cont" {
  name                  = "Users"
  resource_group_name   = azurerm_cosmosdb_account.cdb.resource_group_name
  account_name          = azurerm_cosmosdb_account.cdb.name
  database_name         = azurerm_cosmosdb_sql_database.access_db.name
  partition_key_path    = "/Partition"
cosmosdb_sql_container_test.tf#L93
resource "azurerm_cosmosdb_sql_container" "non-usage_autoscale" {
  name                = "example-container"
  resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
  account_name        = azurerm_cosmosdb_account.example.name
  database_name       = azurerm_cosmosdb_sql_database.example.name
  partition_key_path  = "/definition/id"
consumer_azm.tf#L7
resource "azurerm_cosmosdb_sql_container" "coll_consumer_azm_1" {
  name                   = "consumer_account_list"
  resource_group_name    = var.resource_group_name
  account_name           = azurerm_cosmosdb_account.consumer_cosmosdb_account.name
  database_name          = azurerm_cosmosdb_sql_database.db-consumer-azm.name
  partition_key_path     = "/listNameIdInsKey"
consumer_asp.tf#L7
resource "azurerm_cosmosdb_sql_container" "coll_consumer_asp_1" {
  name                   = "consumer_account_list"
  resource_group_name    = var.resource_group_name
  account_name           = azurerm_cosmosdb_account.consumer_cosmosdb_account.name
  database_name          = azurerm_cosmosdb_sql_database.db-consumer-asp.name
  partition_key_path     = "/listNameIdInsKey"
cosmosdb_sql_container_test.tf#L93
resource "azurerm_cosmosdb_sql_container" "non-usage_autoscale" {
  name                = "example-container"
  resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
  account_name        = azurerm_cosmosdb_account.example.name
  database_name       = azurerm_cosmosdb_sql_database.example.name
  partition_key_path  = "/definition/id"
main.tf#L1
resource "azurerm_cosmosdb_sql_container" "container" {
  name                  = var.container_name
  resource_group_name   = var.resource_group_name
  account_name          = var.db_account_name
  database_name         = var.db_name
  partition_key_path    = var.pkey_path
main.tf#L2
resource "azurerm_cosmosdb_sql_container" "container" {


  resource_group_name = var.context.resource_group_name
  account_name        = var.service_settings.account_name
  database_name       = var.service_settings.database_name
cosmosdb-container.tf#L1
resource "azurerm_cosmosdb_sql_container" "cosmosdb_container" {

  for_each = {
    for index, attribute in var.container_attributes: index => attribute
  }

main.tf#L32
resource "azurerm_cosmosdb_sql_container" "wallet_users" {
  name                = "users"
  resource_group_name = azurerm_cosmosdb_account.wallet_account.resource_group_name
  account_name        = azurerm_cosmosdb_account.wallet_account.name
  database_name       = azurerm_cosmosdb_sql_database.wallet_database.name
  partition_key_path  = "/id"

Review your Terraform file for Azure best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages a SQL Container within a Cosmos DB Account.

Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers (Azure Resource Manager)

The databaseAccounts/sqlDatabases/containers in Microsoft.DocumentDB can be configured in Azure Resource Manager with the resource name Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

An example could not be found in GitHub.

Parameters

  • apiVersion required - string
  • location optional - string

    The location of the resource group to which the resource belongs.

  • name required - string

    Cosmos DB container name.

  • properties required
      • options optional
          • autoscaleSettings optional
              • maxThroughput optional - integer

                Represents maximum throughput, the resource can scale up to.

          • throughput optional - integer

            Request Units per second. For example, "throughput": 10000.

      • resource required
          • analyticalStorageTtl optional - integer

            Analytical TTL.

          • conflictResolutionPolicy optional
              • conflictResolutionPath optional - string

                The conflict resolution path in the case of LastWriterWins mode.

              • conflictResolutionProcedure optional - string

                The procedure to resolve conflicts in the case of custom mode.

              • mode optional - string

                Indicates the conflict resolution mode.

          • defaultTtl optional - integer

            Default time to live

          • id required - string

            Name of the Cosmos DB SQL container

          • indexingPolicy optional
              • automatic optional - boolean

                Indicates if the indexing policy is automatic

              • compositeIndexes optional - array

                List of composite path list

              • excludedPaths optional array
                  • path optional - string

                    The path for which the indexing behavior applies to. Index paths typically start with root and end with wildcard (/path/*)

              • includedPaths optional array
                  • indexes optional array
                      • dataType optional - string

                        The datatype for which the indexing behavior is applied to.

                      • kind optional - string

                        Indicates the type of index.

                      • precision optional - integer

                        The precision of the index. -1 is maximum precision.

                  • path optional - string

                    The path for which the indexing behavior applies to. Index paths typically start with root and end with wildcard (/path/*)

              • indexingMode optional - string

                Indicates the indexing mode.

              • spatialIndexes optional array
                  • path optional - string

                    The path for which the indexing behavior applies to. Index paths typically start with root and end with wildcard (/path/*)

                  • types optional - array

                    List of path's spatial type

          • partitionKey optional
              • kind optional - string

                Indicates the kind of algorithm used for partitioning. For MultiHash, multiple partition keys (upto three maximum) are supported for container create.

              • paths optional - array

                List of paths using which data within the container can be partitioned

              • version optional - integer

                Indicates the version of the partition key definition

          • uniqueKeyPolicy optional
              • uniqueKeys optional array
                  • paths optional - array

                    List of paths must be unique for each document in the Azure Cosmos DB service

  • tags optional - string

    Tags are a list of key-value pairs that describe the resource. These tags can be used in viewing and grouping this resource (across resource groups). A maximum of 15 tags can be provided for a resource. Each tag must have a key no greater than 128 characters and value no greater than 256 characters. For example, the default experience for a template type is set with "defaultExperience": "Cassandra". Current "defaultExperience" values also include "Table", "Graph", "DocumentDB", and "MongoDB".

  • type required - string

Frequently asked questions

What is Azure CosmosDB (DocumentDB) SQL Container?

Azure CosmosDB (DocumentDB) SQL Container is a resource for CosmosDB (DocumentDB) of Microsoft Azure. Settings can be wrote in Terraform.

Where can I find the example code for the Azure CosmosDB (DocumentDB) SQL Container?

For Terraform, the marsoffice/qeeps-terraform, gilyas/infracost and Lmartin43/Lmartin43 source code examples are useful. See the Terraform Example section for further details.