Adjust provisioned throughput using an Azure CLI script

The Azure CLI is a set of commands that you can use to manage various resources across Azure. Azure Cosmos DB has a rich command group that can be used to manage various facets of an Azure Cosmos DB account regardless of the selected API.

In this lab, you’ll create an Azure Cosmos DB account, database, and container using the Azure CLI. You will then make adjustments to the provisioned throughput using the Azure CLI.

Log in to the Azure CLI

Before using the Azure CLI, you must first check the version of the CLI and login using your Azure credentials.

  1. Start Visual Studio Code.

  2. Open the Terminal menu and then select New Terminal to open a new terminal instance.

  3. View the version of the Azure CLI using the following command:

     az --version
    
  4. View the most common Azure CLI command groups using the following command:

     az --help
    
  5. Install the tls/ssl certificates before you login to Azure:

     CD "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\"
     .\python.exe -m pip install pip-system-certs
    
  6. Begin the interactive login procedure for the Azure CLI using the following command:

     az login
    
  7. The Azure CLI will automatically open a web browser window or tab. within the browser instance, sign into the Azure CLI using the Microsoft credentials associated with your subscription.

  8. Close your web browser window or tab.

  9. Check if your lab provider has created a resource group for you, if so, record its name since you will need it in the next section.

     az group list --query "[].{ResourceGroupName:name}" -o table
    

    This command could return multiple Resource Group names.

  10. (Optional) If no Resource Group was created for you, choose a Resource Group name and create it. Be aware that some lab envrionments might be locked down and you will need an administrator to create the Resource Group for you.

    i. Get the your location name closet to you from this list

     az account list-locations --query "sort_by([].{YOURLOCATION:name, DisplayName:regionalDisplayName}, &YOURLOCATION)" --output table
    

    ii. Create the resource group. Be aware that some lab envrionments might be locked down and you will need an administrator to create the Resource Group for you.

     az group create --name YOURRESOURCEGROUPNAME --location YOURLOCATION
    

Create Azure Cosmos DB account using the Azure CLI

The cosmosdb command group contains basic commands to create and manage Azure Cosmos DB accounts using the CLI. Since an Azure Cosmos DB account has an addressable URI, it’s important to create a globally unique name for your new account, even if you create it via script.

  1. Return to the terminal instance already open within Visual Studio Code.

  2. View the most command Azure CLI commands related to Azure Cosmos DB using the following command:

     az cosmosdb --help
    
  3. Create a new variable named suffix with the Get-Random PowerShell cmdlet using the following command:

     $suffix=Get-Random -Maximum 1000000
    

    📝 The Get-Random cmdlet generates a random integer between 0 and 1,000,000. This is useful because our services requires a globally unique name.

  4. Create another new variable name accountName using the hard-coded string csms and variable substitution to inject the value of the $suffix variable using the following command:

     $accountName="csms$suffix"
    
  5. Create another new variable name resourceGroup using the name of the resource group you created or viewed earlier in this lab using the following command:

     $resourceGroup="<resource-group-name>"
    

    📝 For example, if your resource group is named dp420, the command will be $resourceGroup=”dp420”.

  6. Use the echo cmdlet to write the value of the $accountName and $resourceGroup variables to the terminal output using the following command:

     echo $accountName
     echo $resourceGroup
    
  7. View the options for az cosmosdb create using the following command:

     az cosmosdb create --help
    
  8. Create a new Azure Cosmos DB account using the predefined variables and the following command:

     az cosmosdb create --name $accountName --resource-group $resourceGroup
    
  9. Wait for the create command to finish execution and return before proceeding forward with this lab.

    💡 The create command can take anywhere from two to twelve minutes to complete, on average.

Create Azure Cosmos DB for NoSQL resources using the Azure CLI

The cosmosdb sql command group contains commands for managing NoSQL API-specific resources for Azure Cosmos DB. You can always use the –help flag to review the options for these command groups.

  1. Return to the terminal instance already open within Visual Studio Code.

  2. View the most command Azure CLI command groups related to Azure Cosmos DB for NoSQL using the following command:

     az cosmosdb sql --help
    
  3. View the Azure CLI commands for managing Azure Cosmos DB for NoSQL databases using the following command:

     az cosmosdb sql database --help
    
  4. Create a new Azure Cosmos DB database using the predefined variables, the database name cosmicworks, and the following command:

     az cosmosdb sql database create --name "cosmicworks" --account-name $accountName --resource-group $resourceGroup
    
  5. Wait for the create command to finish execution and return before proceeding forward with this lab.

  6. View the Azure CLI commands for managing Azure Cosmos DB for NoSQL containers using the following command:

     az cosmosdb sql container --help
    
  7. Create a new Azure Cosmos DB container using the predefined variables, the database name cosmicworks, the container name products, and the following command:

     az cosmosdb sql container create --name "products" --throughput 400 --partition-key-path "/categoryId" --database-name "cosmicworks" --account-name $accountName --resource-group $resourceGroup
    
  8. Wait for the create command to finish execution and return before proceeding forward with this lab.

  9. In a new web browser window or tab, navigate to the Azure portal (portal.azure.com).

  10. Sign into the portal using the Microsoft credentials associated with your subscription.

  11. Select Resource groups, then select the resource group you created or viewed earlier in this lab, and then select the Azure Cosmos DB account resource you created in this lab with the csms prefix.

  12. Within the Azure Cosmos DB account resource, navigate to the Data Explorer pane.

  13. In the Data Explorer, expand the cosmicworks database node, then observe the new products container node within the NoSQL API navigation tree.

  14. Select the products container node within the NoSQL API navigation tree, and then select Scale & Settings.

  15. Observe the values within the Scale tab. Specifically, observe that the Manual option is selected in the Throughput section and that the provisioned throughput is set to 400 RU/s.

  16. Close your web browser window or tab.

Adjust the throughput of an existing container using the Azure CLI

The Azure CLI can be used to migrate a container between manual and autoscale provisioning of throughput. If the container is using autoscale throughput, the CLI can be used to dynamically adjust the maximum allowed throughput value.

  1. Return to the terminal instance already open within Visual Studio Code.

  2. View the Azure CLI commands for managing Azure Cosmos DB for NoSQL container throughput using the following command:

     az cosmosdb sql container throughput --help
    
  3. Migrate the products container throughput from manual provisioning to autoscale using the following command:

     az cosmosdb sql container throughput migrate --name "products" --throughput-type autoscale --database-name "cosmicworks" --account-name $accountName --resource-group $resourceGroup
    
  4. Wait for the migrate command to finish execution and return before proceeding forward with this lab.

  5. Query the the products container to determine the minimum possible throughput value using the following command:

     az cosmosdb sql container throughput show --name "products" --query "resource.minimumThroughput" --output "tsv" --database-name "cosmicworks" --account-name $accountName --resource-group $resourceGroup
    
  6. Update the maximum autoscale throughput of the products container from the current default value of 1,000 to a new value of 5,000 using the following command:

     az cosmosdb sql container throughput update --name "products" --max-throughput 5000 --database-name "cosmicworks" --account-name $accountName --resource-group $resourceGroup
    
  7. Wait for the update command to finish execution and return before proceeding forward with this lab.

  8. Close Visual Studio Code.

  9. In a new web browser window or tab, navigate to the Azure portal (portal.azure.com).

  10. Sign into the portal using the Microsoft credentials associated with your subscription.

  11. Select Resource groups, then select the resource group you created or viewed earlier in this lab, and then select the Azure Cosmos DB account resource you created in this lab with the csms prefix.

  12. Within the Azure Cosmos DB account resource, navigate to the Data Explorer pane.

  13. In the Data Explorer, expand the cosmicworks database node, then observe the new products container node within the NoSQL API navigation tree.

  14. Select the products container node within the NoSQL API navigation tree, and then select Scale & Settings.

  15. Observe the values within the Scale tab. Specifically, observe that the Autoscale option is selected in the Throughput section and that the provisioned throughput is set to 5,000 RU/s.

  16. Close your web browser window or tab.