Create a resource lock for Azure Cosmos DB MongoDB API database and collection using Azure PowerShell
APPLIES TO: MongoDB
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 MongoDB SDK, Mongoshell, any tools 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.
Sample script
# References:
# Az.CosmosDB | https://learn.microsoft.com/powershell/module/az.cosmosdb
# Az.Resources | https://learn.microsoft.com/powershell/module/az.resources
# --------------------------------------------------
# Purpose
#
# --------------------------------------------------
# Variables
# ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup"
$accountName = "myaccount"
$databaseName = "myDatabase"
$collectionName = "myCollection"
# *****
$apiVersion = "2020-04-01" # Cosmos DB RP API version
$lockLevel = "CanNotDelete" # CanNotDelete or ReadOnly
$resourceTypeAccount = "Microsoft.DocumentDB/databaseAccounts"
$resourceTypeDatabase = "$resourceTypeAccount/mongodbDatabases"
$resourceTypeCollection = "$resourceTypeDatabase/collections"
$resourceNameDatabase = "$accountName/$databaseName"
$lockNameDatabase = "$accountName-$databaseName-Lock"
$resourceNameCollection = "$accountName/$databaseName/$collectionName"
$lockNameCollection = "$accountName-$databaseName-$collectionName-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 "Create a $lockLevel lock on resource $resourceNameCollection"
New-AzResourceLock `
-ApiVersion $apiVersion `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceTypeCollection `
-ResourceName $resourceNameCollection `
-LockName $lockNameCollection `
-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 "Delete lock on resource $resourceNameCollection"
Remove-AzResourceLock `
-ApiVersion $apiVersion `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceTypeCollection `
-ResourceName $resourceNameCollection `
-LockName $lockNameCollection `
-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.