Create Azure Cosmos Databases programmatically using ARM templates and PowerShell

I have recently deployed tens of Azure resources programmatically. While most of the Azure resources and settings you can define in the ARM templates in JSON format, there are scenarios where you need to benefit from the power of PowerShell and the Azure CLI.

In this article I will demonstrate how to deploy an Azure Cosmos DB Account using an ARM template and then create the databases and collections using PowerShell.

To get the Azure templates for Visual Studio, install the Azure development SDK:

Create in Visual Studio a new project from type Azure Resource Group:

Edit the file azuredeploy.json like this:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"environmentName": {
"type": "string",
"defaultValue": "ranari"
}
},
"variables": {},
"resources": [
{
"apiVersion": "2015-04-08",
"name": "[concat(parameters('environmentName'),'mongodb')]",
"type": "Microsoft.DocumentDB/databaseAccounts",
"kind": "MongoDB",
"location": "[resourceGroup().location]",
"scale": null,
"properties": {
"ipRangeFilter": "",
"enableAutomaticFailover": false,
"enableMultipleWriteLocations": false,
"isVirtualNetworkFilterEnabled": false,
"virtualNetworkRules": [],
"databaseAccountOfferType": "Standard",
"consistencyPolicy": {
"defaultConsistencyLevel": "Session",
"maxIntervalInSeconds": 5,
"maxStalenessPrefix": 100
},
"locations": [
{
"locationName": "[resourceGroup().location]",
"failoverPriority": 0
}
],
"capabilities": []
},
"dependsOn": [],
"tags": {}
}
],
"outputs": {}
}

Deploy the solution by following these steps:

Browse the Azure site and observe that the Azure Cosmos DB Account has been created:

At this point the Azure Cosmos DB account is empty. To add a database and collection, run following PowerShell script:

#####################################################

#Parameters & variables

param(
[Parameter(Mandatory=$true)][String]$environmentName,
[Parameter(Mandatory=$false)][String]$subscriptionId = "e9c6149c-f5f6-44c7-aa8b-b8d2fa7f6140"
)

$resourceGroup = $environmentName.ToLower() + "-databases"
$mongoDBAccount = $environmentName.ToLower() + "mongodb"
$mongoDBdatabase = $environmentName.ToLower() + "-cars"

$mongoDBdatabaseCollections=@("VW",
"BMW",
"Mercedes",
"Audi",
"Skoda",
"Renault",
"Ford")

#####################################################

#Connect to Azure
Connect-AzAccount
az account set --subscription $subscriptionId

#####################################################
#Create databases and collections

az cosmosdb database create --resource-group-name $resourceGroup --db-name $mongoDBdatabase --name $mongoDBAccount

foreach($collection in $mongoDBdatabaseCollections)
{
az cosmosdb collection create --resource-group-name $resourceGroup --name $mongoDBAccount --db-name $mongoDBdatabase --collection-name $collection --throughput 400
}
#####################################################

Now check in Azure that the database and collection were successfully created:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s