Implementing Resource Locks
This lesson explains the lock levels, how they are inherited, and what side effects to expect.
Learning outcomes
After completing this lesson, you will be able to:
- Compare CanNotDelete and ReadOnly locks.
- Explain inheritance and precedence.
- Explain why locks do not protect data.
- Identify operations that a ReadOnly lock can break.
What a lock does
A resource lock prevents accidental change to an Azure resource, a resource group or a subscription. A lock overrides role-based access control: even an Owner cannot delete a locked resource until the lock is removed. This is what makes a lock different from a role. A role grants or withholds a right from a person, whereas a lock protects the resource from everyone, including the people who legitimately hold rights.
There are two levels, as the following comparison shows.
Inheritance and precedence
A lock applies to the scope at which it is created and everything beneath it. A lock on a resource group protects all of its resources, including new ones. When several locks apply, the most restrictive one takes effect, so a ReadOnly lock on a resource group overrides a CanNotDelete lock on a resource within it.
Creating or deleting a lock needs permission for Microsoft.Authorization/locks/*. Among built-in roles, only Owner and User Access Administrator hold it. This allows a separation of duties: a team of Contributors can work on resources, and they cannot remove locks. An Owner can remove a lock, so locks prevent mistakes rather than deliberate action by an Owner. To resist a malicious administrator, use deny settings on a deployment stack or a separate tenant control.
Lock precedence
Control plane only
Locks apply to the control plane, that is, to operations sent through management.azure.com. They do not affect the data plane. A ReadOnly lock on a storage account stops changes to the account's settings, but it does not stop anyone from uploading or deleting blobs through the blob endpoint. A CanNotDelete lock on an Azure SQL database stops the database from being dropped through Azure Resource Manager, but it does not stop a user from deleting rows, or from running a T-SQL DROP DATABASE statement.
A scenario that asks you to protect data from deletion should therefore lead to soft delete, immutability or backup, not to a lock.
What a lock cannot do
Side effects
Locks can break operations that look harmless. The cause is usually that an operation which seems to read actually sends a POST request, which a ReadOnly lock blocks. Examples include the following.
- Listing storage account keys is a POST, so a ReadOnly lock on the account stops the portal from showing keys and can break tools that rely on them.
- Azure App Service with a ReadOnly lock cannot start or stop, and it may not show certain settings.
- Virtual machines with a ReadOnly lock cannot be started or restarted, because those are write operations.
- Resource group deletion is blocked by a lock on any resource in the group.
- Azure Backup needs to delete old restore point collections, so a lock on the collection's resource group stops clean-up.
Test a lock in a non-production environment first, and prefer CanNotDelete unless a frozen configuration is truly needed.
Locks with side effects
Creating a lock
The following commands create a CanNotDelete lock on a resource group, and list the locks that apply to it.
az lock create --name no-delete --lock-type CanNotDelete --resource-group RG
az lock list --resource-group RG -o tableLocks can also be deployed by policy or through templates, which keeps them consistent across environments. Include them in a deployment so that protection does not depend on someone remembering to add it.
Protecting a production network
Operators must be able to change rules on a production virtual network and its NSGs, but neither the network nor the NSGs may be deleted by accident. A previous mistake removed a resource group during a clean-up. Operators hold Network Contributor. What should you configure?
Apply a CanNotDelete lock on the resource group that contains the network resources. It allows the rule changes that operators need, and it blocks deletion of the group and of everything in it. Operators cannot remove the lock, as Network Contributor does not include the locks permission.
A ReadOnly lock would stop the rule changes that the scenario needs. A custom role without delete rights would stop operators, but not someone with a broader role, such as the person who performed the clean-up. The Azure Policy DenyAction effect can block delete requests, but it would need an assignment for each resource type, whereas one lock covers the whole group.
Can a CanNotDelete lock on a storage account stop someone from deleting a blob?
Show answer
No. Locks act on the control plane, and deleting a blob is a data plane operation.
Exam Tips
Remember for the exam
- A lock overrides RBAC for everyone until it is removed.
- Locks act on the control plane only.
- The most restrictive lock in the hierarchy applies.
- ReadOnly can break POST-based reads such as listing storage keys.