Create an Azure Cosmos DB for NoSQL account, database, and container with autoscale
APPLIES TO: NoSQL
The script in this article creates an Azure Cosmos DB for NoSQL account, database, and container with autoscale.
Prerequisites
-
If you don't have an Azure trail subscription, create a trial subscription before you begin.
This script requires Azure CLI version 2.0.73 or later.
Note
Before you can use Azure CLI in Microsoft Azure operated by 21Vianet, please run
az cloud set -n AzureChinaCloud
first to change the cloud environment. If you want to switch back to Azure Public Cloud, runaz cloud set -n AzureCloud
again.You can use az account set to sign in with a different subscription, replacing
<subscriptionId>
with your Azure subscription ID.subscription="<subscriptionId>" # add subscription here az account set -s $subscription # ...or use 'az login'
If you prefer, you can install Azure CLI to run the script locally. Run az version to find the Azure CLI version and dependent libraries that are installed, and run az upgrade if you need to upgrade. If prompted, install Azure CLI extensions. If you're running Windows or macOS, consider running Azure CLI in a Docker container.
If you're using a local installation, sign in to Azure by running az login and following the prompts. For other sign-in options, see Sign in with the Azure CLI.
Sample script
Run the following script to create an Azure resource group, an Azure Cosmos DB for NoSQL account and database, and a container with autoscale. The resources might take a while to create.
# Create a SQL API database and container with autoscale
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="chinaeast2"
resourceGroup="msdocs-cosmosdb-rg-$randomIdentifier"
tag="autoscale-sql-cosmosdb"
account="msdocs-account-cosmos-$randomIdentifier" #needs to be lower case
database="msdocs-db-sql-cosmos"
container="container1"
partitionKey="/partitionKey"
maxThroughput=1000 #minimum = 1000
# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag
# Create a Cosmos account for SQL API
echo "Creating $account"
az cosmosdb create --name $account --resource-group $resourceGroup --default-consistency-level Eventual --locations regionName="$location" failoverPriority=0 isZoneRedundant=False
# Create a SQL API database
echo "Creating $database"
az cosmosdb sql database create --account-name $account --resource-group $resourceGroup --name $database
# Create a SQL API container with autoscale
echo "Creating $container with $maxThroughput"
az cosmosdb sql container create --account-name $account --resource-group $resourceGroup --database-name $database --name $container --partition-key-path $partitionKey --max-throughput $maxThroughput
# </FullScript>
# echo "Deleting all resources"
# az group delete --name $resourceGroup -y
This script uses the following commands:
- az group create creates a resource group to store all resources.
- az cosmosdb create creates an Azure Cosmos DB account for API for NoSQL.
- az cosmosdb sql database create creates an Azure Cosmos DB for NoSQL database.
- az cosmosdb sql container create with
--max-throughput 1000
creates an Azure Cosmos DB for NoSQL container with autoscale capability.
Clean up resources
If you no longer need the resources you created, use the az group delete command to delete the resource group and all resources it contains. These resources include the Azure Cosmos DB account, database, and container. The resources might take a while to delete.
az group delete --name $resourceGroup