Bylaw

Gallery › Network › Azure Community Policy

VMs in Resource Groups containing suffix must have NICs in Resource Groups with same suffix

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This Policy will require that VMs deployed into Resource Groups with a certain suffix also have their NICs deployed into Resource Groups with that same suffix.

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

Azure Policy JSONazurepolicy.json · 88 lines
{
  "name": "e5eae955-3615-45b5-979a-075c292fc35a",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "VMs in Resource Groups containing suffix must have NICs in Resource Groups with same suffix",
    "description": "This Policy will require that VMs deployed into Resource Groups with a certain suffix also have their NICs deployed into Resource Groups with that same suffix.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "All",
    "parameters": {
      "resourceGroupSuffix": {
        "type": "String",
        "metadata": {
          "displayName": "Resource Group Suffix",
          "description": "VMs deployed into Resource Groups with this suffix will require that their NICs be in Resource Groups also containging this suffix."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachines"
              },
              {
                "value": "[resourceGroup().name]",
                "notLike": "[concat('*', parameters('resourceGroupSuffix'))]"
              },
              {
                "count": {
                  "field": "Microsoft.Compute/virtualMachines/networkProfile.networkInterfaces[*]",
                  "where": {
                    "value": "[split(string(field('Microsoft.Compute/virtualMachines/networkProfile.networkInterfaces[*].id')), '/')[4]]",
                    "like": "[concat('*', parameters('resourceGroupSuffix'))]"
                  }
                },
                "greater": 0
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachines"
              },
              {
                "value": "[resourceGroup().name]",
                "like": "[concat('*', parameters('resourceGroupSuffix'))]"
              },
              {
                "count": {
                  "field": "Microsoft.Compute/virtualMachines/networkProfile.networkInterfaces[*]",
                  "where": {
                    "value": "[split(string(field('Microsoft.Compute/virtualMachines/networkProfile.networkInterfaces[*].id')), '/')[4]]",
                    "notLike": "[concat('*', parameters('resourceGroupSuffix'))]"
                  }
                },
                "greater": 0
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawe5eae955-3615-45b5-979a-075c292fc35a.bylaw.ps1 · 48 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'e5eae955-3615-45b5-979a-075c292fc35a' {

    Properties @{
        DisplayName = 'VMs in Resource Groups containing suffix must have NICs in Resource Groups with same suffix'
        Description = 'This Policy will require that VMs deployed into Resource Groups with a certain suffix also have their NICs deployed into Resource Groups with that same suffix.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter ResourceGroupSuffix @{
        Type        = 'String'
        DisplayName = 'Resource Group Suffix'
        Description = 'VMs deployed into Resource Groups with this suffix will require that their NICs be in Resource Groups also containging this suffix.'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'type, networkProfile.networkInterfaces and networkProfile.networkInterfaces.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/virtualMachines' -IfPresent
                Arm '[resourceGroup().name]' | Must -BeLike (Arm '[concat(''*'', parameters(''resourceGroupSuffix''))]')
                Count 'Microsoft.Compute/virtualMachines/networkProfile.networkInterfaces[*]' -Where {
                    Arm '[split(string(field(''Microsoft.Compute/virtualMachines/networkProfile.networkInterfaces[*].id'')), ''/'')[4]]' | Must -BeLike (Arm '[concat(''*'', parameters(''resourceGroupSuffix''))]')
                } | Must -Be 0
            }
        }

        Require 'type, networkProfile.networkInterfaces and networkProfile.networkInterfaces.id are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/virtualMachines' -IfPresent
                Arm '[resourceGroup().name]' | Must -Not -BeLike (Arm '[concat(''*'', parameters(''resourceGroupSuffix''))]')
                Count 'Microsoft.Compute/virtualMachines/networkProfile.networkInterfaces[*]' -Where {
                    Arm '[split(string(field(''Microsoft.Compute/virtualMachines/networkProfile.networkInterfaces[*].id'')), ''/'')[4]]' | Must -Not -BeLike (Arm '[concat(''*'', parameters(''resourceGroupSuffix''))]')
                } | Must -Be 0
            }
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Network/vms-in-resource-groups-containing-suffix-must-have-nics-in-resource-groups-with-same-suffix/azurepolicy.json in Azure/Community-Policy, under the MIT licence.