Gallery › Regulatory Compliance › Azure Community Policy
Deploy CanNotDelete Resource Lock on Resource Groups
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0
Creates a resource lock at the resource group level for preventing resource deletion.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 85 lines
{
"name": "0bdc4336-fd4c-4798-b524-a2ecbeed9d0c",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Deploy CanNotDelete Resource Lock on Resource Groups",
"description": "Creates a resource lock at the resource group level for preventing resource deletion.",
"metadata": {
"category": "Regulatory Compliance",
"version": "1.0.0"
},
"mode": "All",
"parameters": {
"rgNames": {
"type": "Array",
"metadata": {
"displayName": "Resource Groups names",
"description": "The list of RG that should have cannot delete resource lock, all resources will inherit the lock"
},
"defaultValue": [
""
]
},
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
},
"allowedValues": [
"DeployIfNotExists",
"AuditIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
},
{
"field": "name",
"in": "[parameters('rgNames')]"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Authorization/locks",
"existenceCondition": {
"field": "Microsoft.Authorization/locks/level",
"equals": "CanNotDelete"
},
"roleDefinitionIds": [
"/providers/Microsoft.Authorization/roleDefinitions/28bf596f-4eb7-45ce-b5bc-6cf482fec137"
],
"deployment": {
"properties": {
"mode": "incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Authorization/locks",
"apiVersion": "2020-05-01",
"name": "DenyDelete",
"properties": {
"level": "CanNotDelete",
"notes": "Prevents deletion of resource group."
}
}
]
}
}
}
}
}
}
}
}Bylaw0bdc4336-fd4c-4798-b524-a2ecbeed9d0c.bylaw.ps1 · 38 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '0bdc4336-fd4c-4798-b524-a2ecbeed9d0c' {
Properties @{
DisplayName = 'Deploy CanNotDelete Resource Lock on Resource Groups'
Description = 'Creates a resource lock at the resource group level for preventing resource deletion.'
Category = 'Regulatory Compliance'
Version = '1.0.0'
Mode = 'All'
}
Parameter RgNames @{
Type = 'Array'
Default = @('')
DisplayName = 'Resource Groups names'
Description = 'The list of RG that should have cannot delete resource lock, all resources will inherit the lock'
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
}
Resource 'Microsoft.Resources/subscriptions/resourceGroups' {
Require 'name is not one of the RgNames parameter' {
Field name | Must -Not -BeIn (Param RgNames) -IfPresent
}
}
DeployIfNotExists 'Microsoft.Authorization/locks' -Role 'Locks Contributor' -Effect (Param Effect) {
Require 'level is CanNotDelete' {
Field level | Must -Be 'CanNotDelete'
}
Deployment -TemplateFile './templates/0bdc4336-fd4c-4798-b524-a2ecbeed9d0c.json'
}
}Its deployment templates are written beside it:
templates/0bdc4336-fd4c-4798-b524-a2ecbeed9d0c.json
Source: policyDefinitions/Regulatory Compliance/deploy-cannotdelete-resource-lock-on-resource-groups/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw