Scaling an Azure Cosmos Db instance with Azure Automation

I wrote before about the need to be able to automatically scale your CosmosDB collections in an attempt to maximize performance while keeping cost at a minimum.

In that article I presented more of a “code-first” solution that you could deploy and use outside the Azure Portal, as a stand-alone tool.

This time, I’m exploring another option, that ties into some preexisting tools that Azure offers - Azure Automation

Azure Automation

With Azure Automation you can easily schedule and execute tasks to solve a variety of use cases.
These tasks are simple scripts that you can write yourself using Python, PowerShell or even a Graphical Designer. Or, you can browse a gallery of scripts written by others that just happens to do what you need.

To make it easier for anyone to automate their CosmosDB scale with Azure Automation, I wrote and published a script to the PowerShell gallery just for that.

You can find the script by just searching for “cosmosdb” in the Azure Automation PowerShell gallery

Hoes does it work

The script is relatively simple. It’s using the AZ Resources PowerShell modules to update a property on an Azure resource, in this case, the thoughput property of a CosmosDb collection resource. You need to pass enough information to uniquely identify a CosmosDb collection: resource Group Name, account Name, database Name and collection Name

As we know, CosmosDb supports multiple API types (Gremlin, Cassandra, MongoDb, etc…) and each of them has a different way of addressing and naming their components: Gremlin works with Graphs and Collections, Cassandra works with Keyspaces and Tables as so on.

The biggest challenge here was to correctly identify the resource type and resource name for those components to be able to correctly address them in the cmdlet and send the right property to update the throughput.

I finally found the answer “hidden” withing the Azure-QuickStart-Templates so the final code looks something like this:

Step by step usage guide

So, how do you actually use this in Azure Automation?

  • create an Azure Automation account
  • install the Az.Resources module from the modules section
  • in the runbooks section, go to Browse Gallery and find the `Scale-CosmosDb` then import it
  • open the new runbook you imported, and go to the schedules section to create a new schedule to run the script.
  • each instance of the runbook schedule will get its own parameters to run the script so that’s where you can configure which collection you want to scale on this schedule.

Local

Lastly, you can also take a look at the original script and just run it locally in a PowerShell terminal.

Share Comments