Create a resource lock for Azure Cosmos DB Table API table using Azure PowerShell
APPLIES TO: Table
Note
We recommend that you use the Azure Az PowerShell module to interact with Azure. See Install Azure PowerShell to get started. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
This sample requires Azure PowerShell Az 5.4.0 or later. Run Get-Module -ListAvailable Az
to see which versions are installed.
If you need to install, see Install Azure PowerShell module.
Run Connect-AzAccount -Environment AzureChinaCloud to sign in to Azure.
Important
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.
Sample script
# References:
# Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# Az.Resources | https://docs.microsoft.com/powershell/module/az.resources
# --------------------------------------------------
# Purpose
#
# --------------------------------------------------
# Variables
# ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup"
$accountName = "myaccount"
$databaseName = "myDatabase"
# *****
$apiVersion = "2020-04-01" # Cosmos DB RP API version
$lockLevel = "CanNotDelete" # CanNotDelete or ReadOnly
$resourceTypeAccount = "Microsoft.DocumentDB/databaseAccounts"
$resourceTypeDatabase = "$resourceTypeAccount/tables"
$resourceNameDatabase = "$accountName/$databaseName"
$lockNameDatabase = "$accountName-$databaseName-Lock"
# --------------------------------------------------
Write-Host "Create a $lockLevel lock on resource $resourceNameDatabase"
New-AzResourceLock `
-ApiVersion $apiVersion `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceTypeDatabase `
-ResourceName $resourceNameDatabase `
-LockName $lockNameDatabase `
-LockLevel $lockLevel `
-Force
Write-Host "List all locks on Cosmos DB account $accountName to confirm lock creation"
Get-AzResourceLock `
-ApiVersion $apiVersion `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceTypeAccount `
-ResourceName $accountName
Write-Host "Delete lock on resource $resourceNameDatabase"
Remove-AzResourceLock `
-ApiVersion $apiVersion `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceTypeDatabase `
-ResourceName $resourceNameDatabase `
-LockName $lockNameDatabase `
-Force
Write-Host "List all locks on Cosmos DB account $accountName to confirm lock removal"
Get-AzResourceLock `
-ApiVersion $apiVersion `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceTypeAccount `
-ResourceName $accountName
Clean up deployment
After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.
Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"
Script explanation
This script uses the following commands. Each command in the table links to command specific documentation.
Command | Notes |
---|---|
Azure Resource | |
New-AzResourceLock | Creates a resource lock. |
Get-AzResourceLock | Gets a resource lock, or lists resource locks. |
Remove-AzResourceLock | Removes a resource lock. |
Next steps
For more information on Azure PowerShell, see Azure PowerShell documentation.