Bylaw

Gallery › General › Azure Landing Zones Library

Do not allow deletion of specified resource and resource type

Converted Policy DenyAction Disabled Mode All Version 1.0.0

This policy enables you to specify the resource and resource type that your organization can protect from accidentals deletion by blocking delete calls using the deny action effect.

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

Azure Policy JSONDenyAction-DeleteResources.alz_policy_definition.json · 70 lines
{
  "name": "DenyAction-DeleteResources",
  "properties": {
    "description": "This policy enables you to specify the resource and resource type that your organization can protect from accidentals deletion by blocking delete calls using the deny action effect.",
    "displayName": "Do not allow deletion of specified resource and resource type",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "General",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "allowedValues": [
          "DenyAction",
          "Disabled"
        ],
        "defaultValue": "DenyAction",
        "metadata": {
          "description": "Enable or disable the execution of the policy",
          "displayName": "Effect"
        },
        "type": "String"
      },
      "resourceName": {
        "metadata": {
          "description": "Provide the name of the resource that you want to protect from accidental deletion.",
          "displayName": "Resource Name"
        },
        "type": "String"
      },
      "resourceType": {
        "metadata": {
          "description": "Provide the resource type that you want to protect from accidental deletion.",
          "displayName": "Resource Type"
        },
        "type": "String"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "[parameters('resourceType')]",
            "field": "type"
          },
          {
            "field": "name",
            "like": "[parameters('resourceName')]"
          }
        ]
      },
      "then": {
        "details": {
          "actionNames": [
            "delete"
          ]
        },
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDenyAction-DeleteResources.bylaw.ps1 · 42 lines
# Converted from DenyAction-DeleteResources.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'DenyAction-DeleteResources' {

    Properties @{
        DisplayName = 'Do not allow deletion of specified resource and resource type'
        Description = 'This policy enables you to specify the resource and resource type that your organization can protect from accidentals deletion by blocking delete calls using the deny action effect.'
        Category    = 'General'
        Version     = '1.0.0'
        Mode        = 'All'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter Effect @{
        AllowedValues = @('DenyAction', 'Disabled')
        Default       = 'DenyAction'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter ResourceName @{
        Type        = 'String'
        DisplayName = 'Resource Name'
        Description = 'Provide the name of the resource that you want to protect from accidental deletion.'
    }
    Parameter ResourceType @{
        Type        = 'String'
        DisplayName = 'Resource Type'
        Description = 'Provide the resource type that you want to protect from accidental deletion.'
    }

    Resource * {

        When {
            Field type | Must -Be (Param ResourceType)
        }

        Require 'name is not like the ResourceName parameter' {
            Field name | Must -Not -BeLike (Param ResourceName) -IfPresent
        }
    }

    DenyAction Delete -Effect (Param Effect)
}

Source: policy_definitions/DenyAction-DeleteResources.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.