DFdom@fradley
← ~/writing
$ cat resource-locks-prevent-deletion.md

Stop someone deleting prod: resource locks in one line

Run az group delete against the wrong resource group and production is gone. A resource lock stops that, and it's a single command.

Lock a resource group so nobody can delete anything in it:

az lock create --name no-delete --lock-type CanNotDelete --resource-group prod-rg

CanNotDelete still lets people read and change resources; it refuses deletion until someone removes the lock. To freeze something completely, with no changes at all, use ReadOnly instead.

A lock applies to everything inside its scope (the level you put it on: a subscription, a resource group or a single resource), so one on the resource group protects every resource in it, including ones created later.

Management group Subscription Resource group: prod-rg Lock applied here: CanNotDelete App Service inherited: CanNotDelete SQL server inherited: CanNotDelete Storage account inherited: CanNotDelete
A lock applies to everything inside its scope, including resources created later.

Here's the same lock in Terraform, so it lives in a repo:

data "azurerm_resource_group" "prod" {
  name = "prod-rg"
}

resource "azurerm_management_lock" "no_delete" {
  name       = "no-delete"
  scope      = data.azurerm_resource_group.prod.id
  lock_level = "CanNotDelete"
  notes      = "Protects production from accidental deletion."
}

Removing the lock is a deliberate second step, which is the point. At scale you can apply locks automatically across a management-group hierarchy.

The full, runnable version is in the blog-examples repo.

discuss on linkedin → more writing