Bylaw

Gallery › Network › Azure Community Policy

VMs not in a specific subnet cannot be part of a backend pool

Uses Condition Policy Audit Deny Disabled Mode All Version 1.0.0

need to add description

ConvertTo-Bylaw converted this policy, and proved it compiles back to the same definition. Conditions the language can't express yet are written as raw JSON with Condition.

Azure Policy JSONazurepolicy.json · 57 lines
{
  "name": "2e3fbee4-3033-4ec0-b076-c0689636c43b",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "VMs not in a specific subnet cannot be part of a backend pool",
    "description": "need to add description",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "allowedSubnetName": {
        "type": "string",
        "metadata": {
          "displayName": "Name of subnet where VMs are allowed to be added into a backend pool."
        }
      },
      "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[*].applicationGatewayBackendAddressPools[*]"
            },
            "greater": 0
          },
          {
            "field": "Microsoft.Network/networkInterfaces/ipconfigurations[*].subnet.id",
            "notContains": "[parameters('allowedSubnetName')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw2e3fbee4-3033-4ec0-b076-c0689636c43b.bylaw.ps1 · 37 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '2e3fbee4-3033-4ec0-b076-c0689636c43b' {

    Properties @{
        DisplayName = 'VMs not in a specific subnet cannot be part of a backend pool'
        Description = 'need to add description'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter AllowedSubnetName @{
        Type        = 'string'
        DisplayName = 'Name of subnet where VMs are allowed to be added into a backend pool.'
    }
    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 {
            Condition '{ "count": { "field": "Microsoft.Network/networkInterfaces/ipConfigurations[*].applicationGatewayBackendAddressPools[*]" }, "greater": 0 }'
        }

        Require 'ipconfigurations and subnet.id are as required' {
            Count 'ipconfigurations[*]' -Where {
                Field subnet.id | Must -ContainText (Param AllowedSubnetName)
            } | Must -Not -Be 0
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/vms-not-in-a-specific-subnet-cannot-be-part-of-a-backend-pool/azurepolicy.json in Azure/Community-Policy, under the MIT licence.