title: How to apply Azure resource locks to Resource Groups via Policy
body: <p>I'm trying to create an Azure policy which will deploy a resource lock with the level of 'CanNotDelete' to resource groups within a subscription.</p>
  
  <p>Currently the policy is 100% compliant but no locks have been created by the policy.</p>
  
  <p>I have the following in my JSON policy.rules file;</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>   
  {
     "if": {
        "field": "type",
        "equals": "Microsoft.Resources/resourceGroups"
     },
     "then": {
        "effect": "deployIfNotExists",
        "details": {
              "type": "Microsoft.Authorization/locks",
              "existenceCondition": {
                 "field": "Microsoft.Authorization/locks/level",
                 "equals": "CanNotDelete"
           },
           "roleDefinitionIds": [
              "/providers/Microsoft.Authorization/roleDefinitions/0000-0000-0000-0000-0000000"
  ],
        "deployment": {
           "properties": {
              "mode": "incremental",
              "template": {
                 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
                 "contentVersion": "1.0.0.0",
                 "parameters": {
                    "location": {
                       "type": "string"
                    }
                 },
                 "resources": [
                    {
                       "type": "Microsoft.Authorization/locks",
                       "apiVersion": "2017-04-01",
                       "name": "ResourceLock",
                       "properties": {
                         "level": "CanNotDelete",
                         "notes": "Prevent accidental deletion of resource groups"
                       }
                    }
                 ]
              }
           }
        }
     }
  }
  }  
    </code></pre>
  </div>
  </div>
  </p>