Lesson 3 of 3 Azure Governance and Compliance

Protecting Resources with Resource Locks

This lesson explains the purpose of resource locks, the CanNotDelete and ReadOnly lock types, inheritance, and how locked resources are changed.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the purpose of resource locks.
  • Distinguish the CanNotDelete and ReadOnly lock types.
  • Explain how locks are inherited and removed.

Why locks are needed

Azure RBAC grants permissions, and an administrator with Owner or Contributor rights can delete a production database or virtual network with a single click or command. A resource lock prevents resources from being accidentally deleted or changed, even by users who have permission to do so. Locks add a deliberate extra step before a critical resource can be removed or altered.

Lock types

There are two types of resource lock. The following comparison shows what each allows.

CanNotDelete (shown as Delete)
Read the resourceAllowed
Modify the resourceAllowed
Delete the resourceBlocked
Choose when Protect against deletion while allowing changes
ReadOnly
Read the resourceAllowed
Modify the resourceBlocked
Delete the resourceBlocked
Choose when Freeze the resource's configuration completely

A ReadOnly lock is similar to restricting all authorized users to the permissions that the Reader role grants. Because it blocks all changes, it can have side effects: for example, a ReadOnly lock on a storage account prevents listing its access keys, which some tools need, and a ReadOnly lock on an App Service prevents some portal features from working. CanNotDelete is the more common choice.

Scenario questions

Choose the lock

Exam skills 3.2.3

Choose the lock type for each requirement.

Log in to try this activity Interactive activities are free with an account.

Scope, inheritance and who can manage locks

Locks can be applied to a subscription, a resource group or an individual resource, but not to a management group, and they are inherited: a lock on a resource group applies to every resource in it, including resources added later. Locks apply to everyone, including subscription Owners, because they act on the resource rather than on a person.

Locks apply to control plane (management) operations sent to Azure Resource Manager, not to the data inside a resource: a CanNotDelete lock on a storage account stops the account from being deleted, but users with data access can still delete blobs inside it. Creating or deleting a lock requires permission to the Microsoft.Authorization/locks actions, which the Owner and User Access Administrator built-in roles have.

True or false

Where locks apply

Exam skills 3.2.3

Check where locks can be applied and who they affect.

Log in to try this activity Interactive activities are free with an account.

Changing or deleting a locked resource

To modify or delete a locked resource, the lock must first be removed. The following steps show the process.

  1. Confirm the change is intended

    The lock exists to prompt this check; confirm with the resource owner.

  2. Remove the lock

    A user with Owner or User Access Administrator rights deletes the lock from the resource, resource group or subscription where it is defined.

  3. Perform the action

    Delete or modify the resource as planned.

  4. Reapply the lock if needed

    If the resource remains, add the lock again to restore protection.

If a lock is inherited from a resource group or subscription, it must be removed at the level where it was created; it cannot be removed from the child resource.

Troubleshooting scenario

The lock that will not go

Exam skills 3.2.3

Resolve problems when changing a locked resource.

Log in to try this activity Interactive activities are free with an account.
Worked example

Protecting production

An administrator accidentally deleted a production virtual network last month. The company wants to prevent every resource in the production resource group from being deleted, including resources added in future, while still allowing administrators to change their configuration. What should be configured?

Apply a CanNotDelete (Delete) lock to the production resource group. The lock is inherited by every current and future resource in the group, blocks deletion even by Owners, and still allows configuration changes. A ReadOnly lock would block the configuration changes that administrators need, and removing the administrators' Contributor or Owner role would stop them from doing their jobs.

Check yourself

Does a CanNotDelete lock on a storage account stop users from deleting blobs inside it?

Show answer

No. Locks apply to management operations on the resource, not to its data.

Exam Tips

Remember for the exam

  • CanNotDelete (Delete in the portal) allows changes but blocks deletion; ReadOnly blocks changes and deletion.
  • Locks apply to everyone, including Owners; remove the lock before deleting or changing the resource.
  • Locks are inherited from subscriptions and resource groups and apply to management operations, not data.
  • Owner and User Access Administrator can create and delete locks.