If you have read any of my blogs before, or know me only a little bit, you know I am a huge fan of ARM templates for Azure Resource Manager. However, every now and then I run into some piece of infrastructure that I would like to set up for my application only to find out that it is not supported by ARM templates. Examples are Cosmos DB databases and collections. Having to createIfNotExists() these was always a pain to code and also mixes the responsibility of resource allocation up with business logic. But no more as part of all the #MSBuild news, the following came in!
As of right now, you can specify the creation of an CosmosDB database and collection, using ARM templates. To create a CosmosDB database for use with the SQL API, you can now use the following template:
{ "type": "Microsoft.DocumentDB/databaseAccounts/apis/databases", "name": "accountName/sql/databaseName", "apiVersion": "2016-03-31", "properties": { "resource": { "id": "databaseName" }, "options": { "throughput": 400 } } }
After setting up a database, it is time to add a few containers. In this case I already provisioned throughput at the database level, so I can add as many containers as I need without additional cost. But, let’s start with just one:
{ "type": "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers", "name": "accountName/sql/databasename/containername", "apiVersion": "2016-03-31", "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts/apis/databases/accountName/sql/databaseName')]" ], "properties": { "resource":{ "id": "containerName", "partitionKey": { "paths": [ "/PartitionKey" ], "kind": "Hash" }, "indexingPolicy": { "indexingMode": "consistent", "includedPaths": [{ "path": "/*", "indexes": [ { "kind": "Range", "dataType": "number", "precision": -1 }, { "kind": "Hash", "dataType": "string", "precision": -1 } ] } ] } } } }
I cannot just create the container and specify the, now mandatory, PartitionKey but also specify custom indexing policies. Putting this together with the template that I already had for creating a CosmosDB account, I can now automatically create all the dependencies for my application using the following ARM template:
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "discriminator": { "type": "string", "minLength": 1 } }, "variables": { "accountName": "[concat(parameters('discriminator'), '-adc-demo')]", "databaseName": "myDatabase", "usersContainerName": "users", "customersContainerName": "customers" }, "resources": [ { "type": "Microsoft.DocumentDB/databaseAccounts", "name": "[variables('accountName')]", "apiVersion": "2016-03-31", "location": "[resourceGroup().location]", "kind": "GlobalDocumentDB", "properties": { "databaseAccountOfferType": "Standard", "consistencyPolicy": { "defaultConsistencyLevel": "Session", "maxIntervalInSeconds": 5, "maxStalenessPrefix": 100 }, "name": "[variables('accountName')]" } }, { "type": "Microsoft.DocumentDB/databaseAccounts/apis/databases", "name": "[concat(variables('accountName'), '/sql/', variables('databaseName'))]", "apiVersion": "2016-03-31", "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts/', variables('accountName'))]" ], "properties": { "resource": { "id": "[variables('databaseName')]" }, "options": { "throughput": "400" } } }, { "type": "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers", "name": "[concat(variables('accountName'), '/sql/', variables('databasename'), '/', variables('usersContainerName'))]", "apiVersion": "2016-03-31", "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts/apis/databases', variables('accountName'), 'sql', variables('databaseName'))]" ], "properties": { "resource": { "id": "[variables('usersContainerName')]", "partitionKey": { "paths": [ "/CustomerId" ], "kind": "Hash" }, "indexingPolicy": { "indexingMode": "consistent", "includedPaths": [ { "path": "/*", "indexes": [ { "kind": "Range", "dataType": "number", "precision": -1 }, { "kind": "Hash", "dataType": "string", "precision": -1 } ] } ] } } } }, { "type": "Microsoft.DocumentDb/databaseAccounts/apis/databases/containers", "name": "[concat(variables('accountName'), '/sql/', variables('databasename'), '/', variables('customersContainerName'))]", "apiVersion": "2016-03-31", "dependsOn": [ "[resourceId('Microsoft.DocumentDB/databaseAccounts/apis/databases', variables('accountName'), 'sql', variables('databaseName'))]" ], "properties": { "resource": { "id": "[variables('customersContainerName')]", "partitionKey": { "paths": [ "/City" ], "kind": "Hash" }, "indexingPolicy": { "indexingMode": "consistent", "includedPaths": [ { "path": "/*", "indexes": [ { "kind": "Range", "dataType": "number", "precision": -1 }, { "kind": "Hash", "dataType": "string", "precision": -1 } ] } ] } } } } ] }
I hope you enjoy CosmosDB database and collection support just as much as I do, happy coding!
Have you tried adding a CompositeIndex to the collection within ARN templates?
I am sorry for my late reply. Your question was stuck in a spam list, sorry.
I had not given composite indexes a try before, but I just did and it seems to work flawless. Just add a compositeIndexes property to the indexingPolicy and you are good.
I tried the following scenarios and they all worked: creating a container, updating a container to include a composite index and updating a container to no longer have a composite index.