Azure CosmosDB (DocumentDB) Mongo Collection

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

azurerm_cosmosdb_mongo_collection (Terraform)

The Mongo Collection in CosmosDB (DocumentDB) can be configured in Terraform with the resource name azurerm_cosmosdb_mongo_collection. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

azure.tf#L72
resource "azurerm_cosmosdb_mongo_collection" "ngc" {
  name                = "Sessions"
  resource_group_name = azurerm_resource_group.ngc.name
  account_name        = azurerm_cosmosdb_account.ngc.name
  database_name       = azurerm_cosmosdb_mongo_database.ngc_sessions.name

cosmosdb-mongodb-collection.tf#L4
resource "azurerm_cosmosdb_mongo_collection" "mongo_collection_static" {
  name = var.collection_name
  resource_group_name = var.resource_group_name
  account_name = var.account_name
  database_name = var.db_name
  default_ttl_seconds = var.default_ttl_seconds
cosmosdb_mongo_collection_test.tf#L122
resource "azurerm_cosmosdb_mongo_collection" "non-usage_autoscale" {
  name                = "tfex-cosmos-mongo-db"
  resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
  account_name        = azurerm_cosmosdb_account.example.name
  database_name       = azurerm_cosmosdb_mongo_database.non-usage_autoscale.name

cosmosdb_mongo_collection_test.tf#L122
resource "azurerm_cosmosdb_mongo_collection" "non-usage_autoscale" {
  name                = "tfex-cosmos-mongo-db"
  resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
  account_name        = azurerm_cosmosdb_account.example.name
  database_name       = azurerm_cosmosdb_mongo_database.non-usage_autoscale.name

main.tf#L60
resource "azurerm_cosmosdb_mongo_collection" "questions" {
  name                = "questions"
  resource_group_name = azurerm_resource_group.rg.name
  account_name        = azurerm_cosmosdb_account.default.name
  database_name       = azurerm_cosmosdb_mongo_database.default.name

mongo_collection.tf#L2
resource "azurerm_cosmosdb_mongo_collection" "collection" {
  name                = var.settings.name
  resource_group_name = var.resource_group_name
  account_name        = var.cosmosdb_account_name
  database_name       = var.database_name
  shard_key           = var.settings.shard_key
cosmos-db-mongo-collection.tf#L1
resource "azurerm_cosmosdb_mongo_collection" "event_collection" {
  name                = "events"
  resource_group_name = azurerm_resource_group.rg.name
  account_name        = azurerm_cosmosdb_account.db_account.name
  database_name       = azurerm_cosmosdb_mongo_database.mongo_db.name

mongo_collection.tf#L2
resource "azurerm_cosmosdb_mongo_collection" "collection" {
  name                = var.settings.name
  resource_group_name = var.resource_group_name
  account_name        = var.cosmosdb_account_name
  database_name       = var.database_name
  shard_key           = var.settings.shard_key
mongo_collection.tf#L2
resource "azurerm_cosmosdb_mongo_collection" "collection" {
  name                = var.settings.name
  resource_group_name = var.resource_group_name
  account_name        = var.cosmosdb_account_name
  database_name       = var.database_name
  shard_key           = var.settings.shard_key
main.tf#L24
resource "azurerm_cosmosdb_mongo_collection" "mongodb" {
  count = length(var.collections)

  name = var.collections[count.index].name
  default_ttl_seconds = var.collections[count.index].default_ttl_seconds

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 Mongo Collection within a Cosmos DB Account.

Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections (Azure Resource Manager)

The databaseAccounts/mongodbDatabases/collections in Microsoft.DocumentDB can be configured in Azure Resource Manager with the resource name Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections. 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 collection 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.

          • id required - string

            Name of the Cosmos DB MongoDB collection

          • indexes optional array
              • key optional
                  • keys optional - array

                    List of keys for each MongoDB collection in the Azure Cosmos DB service

              • options optional
                  • expireAfterSeconds optional - integer

                    Expire after seconds

                  • unique optional - boolean

                    Is unique or not

          • shardKey optional - string

            The shard key and partition kind pair, only support "Hash" partition kind

  • 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) Mongo Collection?

Azure CosmosDB (DocumentDB) Mongo Collection 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) Mongo Collection?

For Terraform, the cloud-native-conference/Infrastructure, marvin-heiden/sendungsverfolgung and gilyas/infracost source code examples are useful. See the Terraform Example section for further details.