Bylaw

Gallery › Backup › Azure Community Policy

Resource Lock should be enabled

Doesn't convert yet Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

With this policy: any resource that has the tag key LockLevel with the value CanNotDelete means authorized users can read and modify the resource, but they can t delete it.

ConvertTo-Bylaw can't convert this policy yet:

Azure Policy JSONazurepolicy.json · 127 lines
{
  "name": "5c91215f-8ca0-4ca3-8b56-a4a3ec336d8d",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Resource Lock should be enabled",
    "description": "With this policy: any resource that has the tag key LockLevel with the value CanNotDelete means authorized users can read and modify the resource, but they can t delete it.",
    "metadata": {
      "category": "Backup",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "tagName": {
        "type": "string",
        "metadata": {
          "displayName": "Exclusion Tag Name",
          "description": "Name of the tag to use for excluding resources from this policy. This should be used along with the Exclusion Tag Value parameter."
        },
        "defaultValue": "_MVP_Resource_Lock_should_be_enabled"
      },
      "tagValue": {
        "type": "string",
        "metadata": {
          "displayName": "Exclusion Tag Value",
          "description": "Value of the tag to use for excluding resources from this policy. This should be used along with the Exclusion Tag Name parameter."
        },
        "defaultValue": "exclude"
      },
      "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": "tags.LockLevel",
            "equals": "CanNotDelete"
          },
          {
            "value": "[length(split(field('type'),'/'))]",
            "equals": 2
          },
          {
            "not": {
              "field": "[concat('tags[',parameters('tagName'), ']')]",
              "equals": "[parameters('tagValue')]"
            }
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
          ],
          "type": "Microsoft.Authorization/locks",
          "name": "ResourceLockedByPolicy",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Authorization/locks/level",
                "In": [
                  "CanNotDelete"
                ]
              },
              {
                "field": "Microsoft.Authorization/locks/notes",
                "equals": "Locked by Azure Policy"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "resourceName": {
                    "type": "string"
                  },
                  "resourceType": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Authorization/locks",
                    "apiVersion": "2016-09-01",
                    "name": "ResourceLockedByPolicy",
                    "scope": "[concat(parameters('resourceType'),'/', parameters('resourceName'))]",
                    "properties": {
                      "level": "CanNotDelete",
                      "notes": "Locked by Azure Policy"
                    }
                  }
                ],
                "outputs": {}
              },
              "parameters": {
                "resourceName": {
                  "value": "[field('name')]"
                },
                "resourceType": {
                  "value": "[field('type')]"
                }
              }
            }
          }
        }
      }
    }
  }
}

Source: policyDefinitions/Backup/resource-lock-should-be-enabled/azurepolicy.json in Azure/Community-Policy, under the MIT licence.