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 trail subscription, create a trial subscription before you begin.

Prerequisites

You can use the local Azure CLI.

  • This article requires version 2.9.1 or later of the Azure CLI.

Sample script

Sign in to Azure

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 trail subscription, create a trial subscription before you begin.

az cloud set -n AzureChinaCloud
az login

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

For more information, see set active subscription or log in.

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 local 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: