Bylaw

Gallery › Network › Azure Community Policy

Deploy network watcher when virtual networks are created

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This policy creates a network watcher resource in regions with virtual networks. You need to ensure existence of a resource group named networkWatcherRG, which will be used to deploy network watcher instances.

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

Azure Policy JSONazurepolicy.json · 75 lines
{
  "name": "6478a552-e407-4821-b8d1-58db079493e9",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy network watcher when virtual networks are created",
    "description": "This policy creates a network watcher resource in regions with virtual networks. You need to ensure existence of a resource group named networkWatcherRG, which will be used to deploy network watcher instances.",
    "metadata": {
      "category": "Network",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Network/virtualNetworks"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Network/networkWatchers",
          "resourceGroupName": "networkWatcherRG",
          "existenceCondition": {
            "field": "location",
            "equals": "[field('location')]"
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "location": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2016-09-01",
                    "type": "Microsoft.Network/networkWatchers",
                    "name": "[concat('networkWacher_', parameters('location'))]",
                    "location": "[parameters('location')]"
                  }
                ]
              },
              "parameters": {
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw6478a552-e407-4821-b8d1-58db079493e9.bylaw.ps1 · 29 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '6478a552-e407-4821-b8d1-58db079493e9' {

    Properties @{
        DisplayName = 'Deploy network watcher when virtual networks are created'
        Description = 'This policy creates a network watcher resource in regions with virtual networks. You need to ensure existence of a resource group named networkWatcherRG, which will be used to deploy network watcher instances.'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/virtualNetworks'

    DeployIfNotExists 'Microsoft.Network/networkWatchers' -Role 'Network Contributor' -ResourceGroupName 'networkWatcherRG' -Effect (Param Effect) {
        Require 'location is (Arm ''[field(''''location'''')]'')' {
            Field location | Must -Be (Arm '[field(''location'')]')
        }
        Deployment -TemplateFile './templates/6478a552-e407-4821-b8d1-58db079493e9.json' -Parameters ([ordered]@{
                location = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Network/deploy-network-watcher-when-virtual-networks-are-created/azurepolicy.json in Azure/Community-Policy, under the MIT licence.