Bylaw

Gallery › Network › Azure Community Policy

Deny VM creation in subnets that contain specified suffix

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy will deny the creation of VMs in subnets that contain a certain suffix.

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

Azure Policy JSONazurepolicy.json · 69 lines
{
  "name": "8786b26d-f008-4e0a-a1d5-bf962c6f8bff",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deny VM creation in subnets that contain specified suffix",
    "description": "This policy will deny the creation of VMs in subnets that contain a certain suffix.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "disallowedSubnet": {
        "type": "string",
        "metadata": {
          "displayname": "Deny VMs in subnets based on this name",
          "description": "VMs will not be allowed to provision into subnets that contain this word."
        }
      },
      "resourceGroupSuffix": {
        "type": "String",
        "metadata": {
          "displayName": "Resource Group Suffix",
          "description": "NICs deployed into Resource Groups containing this suffix are exempt from the Policy."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/networkInterfaces"
          },
          {
            "count": {
              "field": "Microsoft.Network/networkInterfaces/ipconfigurations[*]",
              "where": {
                "value": "[substring(last(split(string(field('Microsoft.Network/networkInterfaces/ipconfigurations[*].subnet.id')), 'subnets/')), 0, sub(length(last(split(string(field('Microsoft.Network/networkInterfaces/ipconfigurations[*].subnet.id')), 'subnets/'))), 2))]",
                "like": "[concat('*', parameters('disallowedSubnet'))]"
              }
            },
            "greater": 0
          },
          {
            "value": "[resourceGroup().name]",
            "notLike": "[concat('*', parameters('resourceGroupSuffix'))]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw8786b26d-f008-4e0a-a1d5-bf962c6f8bff.bylaw.ps1 · 43 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '8786b26d-f008-4e0a-a1d5-bf962c6f8bff' {

    Properties @{
        DisplayName = 'Deny VM creation in subnets that contain specified suffix'
        Description = 'This policy will deny the creation of VMs in subnets that contain a certain suffix.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter DisallowedSubnet @{
        Type        = 'string'
        DisplayName = 'Deny VMs in subnets based on this name'
        Description = 'VMs will not be allowed to provision into subnets that contain this word.'
    }
    Parameter ResourceGroupSuffix @{
        Type        = 'String'
        DisplayName = 'Resource Group Suffix'
        Description = 'NICs deployed into Resource Groups containing this suffix are exempt from the Policy.'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/networkInterfaces' {

        When {
            Count 'ipconfigurations[*]' -Where {
                Arm '[substring(last(split(string(field(''Microsoft.Network/networkInterfaces/ipconfigurations[*].subnet.id'')), ''subnets/'')), 0, sub(length(last(split(string(field(''Microsoft.Network/networkInterfaces/ipconfigurations[*].subnet.id'')), ''subnets/''))), 2))]' | Must -BeLike (Arm '[concat(''*'', parameters(''disallowedSubnet''))]')
            } | Must -BeGreaterThan 0
        }

        Require 'Requirement 1' {
            Arm '[resourceGroup().name]' | Must -BeLike (Arm '[concat(''*'', parameters(''resourceGroupSuffix''))]')
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/deny-vm-creation-in-subnets-that-contain-specified-suffix/azurepolicy.json in Azure/Community-Policy, under the MIT licence.