Find an existing Azure Cosmos DB free-tier account in a subscription using Azure CLI
APPLIES TO: NoSQL MongoDB Cassandra Gremlin Table
The script in this article demonstrates how to locate an Azure Cosmos DB free-tier account within a subscription.
Each Azure subscription can have up to one Azure Cosmos DB free-tier account. If you're trying to create a free-tier account, the option may be disabled in the Azure portal, or you get an error when attempting to create a free-tier account. If either of these issues occur, use this script to locate the name of the existing free-tier account, and the resource group it belongs to.
If you don't have an Azure subscription, create an Azure free account before you begin.
Prerequisites
Use the Bash environment in Azure Cloud Shell. For more information, see Quickstart for Bash in Azure Cloud Shell.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
- This article requires version 2.9.1 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
Sample script
Launch Azure Cloud Shell
The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.
To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.
When Cloud Shell opens, verify that Bash is selected for your environment. Subsequent sessions will use Azure CLI in a Bash environment, Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.
Sign in to Azure
Cloud Shell is automatically authenticated under the initial account signed-in with. Use the following script to sign in using a different subscription, replacing <Subscription ID>
with your Azure Subscription ID. If you don't have an Azure subscription, create an Azure free account before you begin.
subscription="<subscriptionId>" # add subscription here
az account set -s $subscription # ...or use 'az login'
For more information, see set active subscription or log in interactively
Run the script
# Azure Cosmos DB offers one free-tier account per subscription
# This script will find if you have a free-tier account and output
# the name of the Cosmos DB account and its resource group
# These can remain commented out if running in Azure Cloud Shell
#az login
#az account set -s {your subscription id}
isFound=0
# Iterate through all the resource groups in the subscription
for rg in $(az group list --query "[].name" --output tsv)
do
echo "Checking resource group: $rg"
# Return the Cosmos DB account in the resource group marked as free tier
ft=$(az cosmosdb list -g $rg --query "[?enableFreeTier].name" --output tsv)
if [ ${#ft} -gt 0 ]; then
echo "$ft is a free tier account in resource group: $rg"
isFound=1
break
fi
done
if [ $isFound -eq 0 ]; then
echo "No Free Tier accounts in subscription"
fi
Sample reference
This script uses the following commands. Each command in the table links to command specific documentation.
Command | Notes |
---|---|
az group list | Lists all resource groups in an Azure subscription. |
az cosmosdb list | Lists all Azure Cosmos DB account in a resource group. |
Next steps
For more information on the Azure Cosmos DB CLI, see Azure Cosmos DB CLI documentation.
For Azure CLI samples for specific APIs, see: