Create resource lock for an Azure Cosmos DB for NoSQL database and container using Azure CLI

APPLIES TO: NoSQL

The script in this article demonstrates performing resource lock operations for a SQL database and container.

Important

To create resource locks, you must have membership in the owner role in the subscription.

Resource locks do not work for changes made by users connecting using any Azure Cosmos DB SDK, any tools that connect via account keys, or the Azure Portal unless the Azure Cosmos DB account is first locked with the disableKeyBasedMetadataWriteAccess property enabled. To learn more about how to enable this property see, Preventing changes from SDKs.

If you don't have an Azure subscription, create an Azure free account before you begin.

Prerequisites

  • 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 subscriptionId with your Azure subscription ID.

If you don't have an Azure subscription, create an Azure free account before you begin.

subscription="subscriptionId" # Set Azure subscription ID here

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

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

Run the script

# Resource lock operations for a SQL database and container

# Subscription owner permissions required for this script

# Run this script after running
# "https://docs.microsoft.com/azure/cosmos-db/scripts/cli/sql/create#sample-script"

# Variable block
# Use values from prerequisite script or from your environment
# resourceGroup="your resource group name"
# account="your account name"
# database="your database name"
# container="your container name"

lockType="CanNotDelete" # CanNotDelete or ReadOnly
databaseParent="databaseAccounts/$account"
containerParent="databaseAccounts/$account/sqlDatabases/$database"
databaseLock="$database-Lock"
containerLock="$container-Lock"

# Create a delete lock on database
echo "Creating $lockType lock on $database"
az lock create --name $databaseLock --resource-group $resourceGroup --resource-type Microsoft.DocumentDB/sqlDatabases --lock-type $lockType --parent $databaseParent --resource $database

# Create a delete lock on container
echo "Creating $lockType lock on $container"
az lock create --name $containerLock --resource-group $resourceGroup --resource-type Microsoft.DocumentDB/containers --lock-type $lockType --parent $containerParent --resource $container

# List all locks on a Cosmos account
echo "Listing locks on $account"
az lock list --resource-group $resourceGroup --resource-name $account --namespace Microsoft.DocumentDB --resource-type databaseAccounts

# Delete lock on database
echo "Deleting $databaseLock on $database"
lockid=$(az lock show --name $databaseLock     --resource-group $resourceGroup     --resource-type Microsoft.DocumentDB/sqlDatabases     --resource $database     --parent $databaseParent     --output tsv --query id)
az lock delete --ids $lockid

# Delete lock on container
echo "Deleting $containerLock on $container"
lockid=$(az lock show --name $containerLock     --resource-group $resourceGroup     --resource-type Microsoft.DocumentDB/containers     --resource-name $container     --parent $containerParent     --output tsv --query id)
az lock delete --ids $lockid

Clean up resources

Use the following command to remove the resource group and all resources associated with it using the az group delete command - unless you have an ongoing need for these resources. Some of these resources may take a while to create, as well as to delete.

az group delete --name $resourceGroup

Sample reference

This script uses the following commands. Each command in the table links to command specific documentation.

Command Notes
az lock create Creates a lock.
az lock list List lock information.
az lock show Show properties of a lock.
az lock delete Deletes a lock.

Next steps