Description
To prevent deletion of an Azure Container Registry, check its management locks, including those inherited from its resource group or subscription.
Potential impact
Losing the registry can delay deployments and recovery while you rebuild it and restore the images.
Remediation
If no inherited lock prevents deletion, set scope on an azurerm_management_lock to the registry ID and lock_level to CanNotDelete. Verify the effective scope and level after applying the change. Limit who can remove the lock and agree on an approval process. Manage image and tag deletion permissions and image recovery separately.
Examples
Both excerpts use the same Standard registry. Define the referenced resource group separately.
Before
resource "azurerm_container_registry" "acr" {
name = "containerRegistry1"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
sku = "Standard"
admin_enabled = false
}
After
resource "azurerm_container_registry" "acr" {
name = "containerRegistry1"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
sku = "Standard"
admin_enabled = false
}
resource "azurerm_management_lock" "acr_lock" {
name = "acr-lock"
scope = azurerm_container_registry.acr.id
lock_level = "CanNotDelete"
notes = "Protect registry from accidental deletion"
}
acr-lock applies directly to acr. The first excerpt may already inherit protection from a parent scope.