Azure Resource Lock:
In Azure, Adding Resource Lock helps in protecting the resources against accidental deletion and modification. This also protects the environment against uncontrolled provisioning of new resources by blocking the creation of new resources.
What types of resource locks are available in Azure?
- Read-Only Lock: this will be selected only for read only actions.
- CanNotDelete Lock: this will allow authorized users to read and modify the resource except deleting the resource.
Where can we apply locks in Azure?
In azure, we can apply locks at three different levels:
- Subscription Level
- Resource Group Level
- Resource Level
When ever we add a recourse lock at a parent level scope, all resources within that scope inherit the same lock.
Example 1: If you add or apply lock at Subscription Level then all resources within that scope inherit the same lock. (Includes all Resource Group and Resources Types)
Example 2: If you add or apply lock at Resource Group Level then all resources within the resource group scope level will inherit the same lock. (Includes all Resources Types)
Q) Who can add locks on Azure Resources?
Azure Resource Lock Operations:
Required powershell module Az.Resources to perform the actions.
Add Lock on Azure Resource Group:
New-AzResourceLock -LockLevel CanNotDelete -LockName "CanNotDeleteLock" -ResourceGroupName "_add_rg_name_" -Force
Remove Lock on Azure Resource Group:
Remove-AzResourceLock -LockName CanNotDelete -ResourceGroupName "_add_rg_name_"
Remove Lock on Azure Resource Group at Force:
Remove-AzResourceLock -LockName CanNotDelete -ResourceGroupName "_add_rg_name_" -Force
Add Lock on Azure Resources:
New-AzResourceLock ` -LockLevel CanNotDelete ` –LockName CanNotDeleteLock ` -ResourceGroupName "_add_rg_name_" ` -ResourceName "_add_resource_name_" ` -ResourceType "Microsoft.Compute/virtualMachines"
Automate Adding Resource Group Locks in Azure via PowerShell:
PowerShell Script to Add Resource Group Locks.
$Subscription="" $ResourceGroupName = "" Set-AzContext -SubscriptionName "$Subscription" New-AzResourceLock -LockLevel CanNotDelete -LockName "CanNotDeleteLock" -ResourceGroupName "$ResourceGroupName" -Force
PowerShell Script to Add Locks for Selected List of Resource Groups.
$Subscription="" Set-AzContext -SubscriptionName "$Subscription" $RGs=Get-AzResourceGroup $RGList = @("RG01","RG02","RG09","Test-RG01") foreach ($RG in $RGs){ $ResourceGroupName = $RG.ResourceGroupName ## Write-Output ("ResourceGroup : $ResourceGroupName") if($RGList -contains $ResourceGroupName){ Write-Output ("ResourceGroup : $ResourceGroupName") New-AzResourceLock -LockLevel CanNotDelete -LockName "CanNotDeleteLock" -ResourceGroupName "$ResourceGroupName" -Force } }
PowerShell Script to Add Locks to All Resource Groups within a Subscription.
$Subscription="" Set-AzContext -SubscriptionName "$Subscription" $RGs=Get-AzResourceGroup foreach ($RG in $RGs){ $ResourceGroupName = $RG.ResourceGroupName Write-Output ("ResourceGroup : $ResourceGroupName") New-AzResourceLock -LockLevel CanNotDelete -LockName "CanNotDeleteLock" -ResourceGroupName "$ResourceGroupName" -Force }
Microsoft reference document resources:
👉 Get-AzResourceLock
👉 New-AzResourceLock
👉 Set-AzResourceLock
👉 Remove-AzResourceLock