Bylaw

Gallery › Network › Azure built-in policies

Deploy network watcher when virtual networks are created

Converted Policy DeployIfNotExists Mode Indexed Version 1.1.0

Ensures that each Azure region containing virtual networks has a Network Watcher. An existing Network Watcher in any resource group within the subscription satisfies the policy; otherwise, remediation deploys one to networkWatcherRG, which must already exist.

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

Azure Policy JSONNetworkWatcher_DINE.json · 68 lines
{
  "properties": {
    "displayName": "Deploy network watcher when virtual networks are created",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Ensures that each Azure region containing virtual networks has a Network Watcher. An existing Network Watcher in any resource group within the subscription satisfies the policy; otherwise, remediation deploys one to networkWatcherRG, which must already exist.",
    "metadata": {
      "version": "1.1.0",
      "category": "Network"
    },
    "version": "1.1.0",
    "parameters": {},
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Network/virtualNetworks"
      },
      "then": {
        "effect": "DeployIfNotExists",
        "details": {
          "type": "Microsoft.Network/networkWatchers",
          "resourceGroupName": "networkWatcherRG",
          "existenceScope": "Subscription",
          "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('networkWatcher_', parameters('location'))]",
                    "location": "[parameters('location')]"
                  }
                ]
              },
              "parameters": {
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.1.0",
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/a9b99dd8-06c5-4317-8629-9d86a3c6e7d9",
  "name": "a9b99dd8-06c5-4317-8629-9d86a3c6e7d9"
}
Bylawa9b99dd8-06c5-4317-8629-9d86a3c6e7d9.bylaw.ps1 · 23 lines
# Converted from NetworkWatcher_DINE.json by ConvertTo-Bylaw.
Policy 'a9b99dd8-06c5-4317-8629-9d86a3c6e7d9' {

    Properties @{
        DisplayName   = 'Deploy network watcher when virtual networks are created'
        Description   = 'Ensures that each Azure region containing virtual networks has a Network Watcher. An existing Network Watcher in any resource group within the subscription satisfies the policy; otherwise, remediation deploys one to networkWatcherRG, which must already exist.'
        Version       = '1.1.0'
        Category      = 'Network'
        PolicyVersion = '1.1.0'
        Mode          = 'Indexed'
    }

    Resource 'Microsoft.Network/virtualNetworks'

    DeployIfNotExists 'Microsoft.Network/networkWatchers' -Role 'Network Contributor' -ResourceGroupName 'networkWatcherRG' -ExistenceScope Subscription {
        Require 'location is (Arm ''[field(''''location'''')]'')' {
            Field location | Must -Be (Arm '[field(''location'')]')
        }
        Deployment -TemplateFile './templates/a9b99dd8-06c5-4317-8629-9d86a3c6e7d9.json' -Parameters ([ordered]@{
                location = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Network/NetworkWatcher_DINE.json in Azure/azure-policy, under the MIT licence.