Bylaw

Gallery › General › Azure Community Policy

Deploy Resource Lock on RGs - tag exclusion

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.1

Automatically deploys a resource lock to resources that are not in the dev environment to prevent accidental deletion.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 94 lines
{
  "name": "466ba28e-87e0-4202-906b-d4b399d11591",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy Resource Lock on RGs - tag exclusion",
    "description": "Automatically deploys a resource lock to resources that are not in the dev environment to prevent accidental deletion.",
    "metadata": {
      "version": "1.0.1",
      "category": "General"
    },
    "mode": "All",
    "parameters": {
      "TagOfExclusion": {
        "type": "String",
        "metadata": {
          "displayName": "Tag of environment to exclude",
          "description": "If there is a need to exclude RGs from the audit based on a tag, you can do so. In this field, define which tag you want to check for it's value."
        },
        "defaultValue": "Environment"
      },
      "TagValue": {
        "type": "String",
        "metadata": {
          "displayName": "Value of the tag for exclusion",
          "description": "If you decided to configure an exclusion, you need to configure a specific value of the tag you defined. Put the tag value for exclusion in this field"
        },
        "defaultValue": "dev"
      },
      "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"
          },
          {
            "not": {
              "field": "[concat('tags[', parameters('TagOfExclusion'), ']')]",
              "equals": "[parameters('TagValue')]"
            }
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Authorization/locks",
          "evaluationDelay": "AfterProvisioning",
          "existenceCondition": {
            "field": "Microsoft.Authorization/locks/level",
            "equals": "CanNotDelete"
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "resources": [
                  {
                    "name": "Auto locked by policy",
                    "type": "Microsoft.Authorization/locks",
                    "apiVersion": "2020-05-01",
                    "properties": {
                      "level": "CanNotDelete",
                      "notes": "This lock was deployed automatically by Azure Policy to prevent the resource group and its containing resources from accidental deletion."
                    }
                  }
                ]
              }
            }
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
          ]
        }
      }
    }
  }
}
Bylaw466ba28e-87e0-4202-906b-d4b399d11591.bylaw.ps1 · 44 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '466ba28e-87e0-4202-906b-d4b399d11591' {

    Properties @{
        DisplayName = 'Deploy Resource Lock on RGs - tag exclusion'
        Description = 'Automatically deploys a resource lock to resources that are not in the dev environment to prevent accidental deletion.'
        Version     = '1.0.1'
        Category    = 'General'
        Mode        = 'All'
    }

    Parameter TagOfExclusion @{
        Default     = 'Environment'
        DisplayName = 'Tag of environment to exclude'
        Description = 'If there is a need to exclude RGs from the audit based on a tag, you can do so. In this field, define which tag you want to check for it''s value.'
        JsonName    = 'TagOfExclusion'
    }
    Parameter TagValue @{
        Default     = 'dev'
        DisplayName = 'Value of the tag for exclusion'
        Description = 'If you decided to configure an exclusion, you need to configure a specific value of the tag you defined. Put the tag value for exclusion in this field'
        JsonName    = 'TagValue'
    }
    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 'tag (Param TagOfExclusion) is the TagValue parameter' {
            Tag (Param TagOfExclusion) | Must -Be (Param TagValue)
        }
    }

    DeployIfNotExists 'Microsoft.Authorization/locks' -Role 'Owner' -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
        Require 'level is CanNotDelete' {
            Field level | Must -Be 'CanNotDelete'
        }
        Deployment -TemplateFile './templates/466ba28e-87e0-4202-906b-d4b399d11591.json'
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/General/deploy-resource-lock-on-rgs-tag-exclusion/azurepolicy.json in Azure/Community-Policy, under the MIT licence.