Bylaw

Gallery › Network › Azure built-in policies

Create Regional NetworkWatcher in NetworkWatcherRG for VNet Flowlogs

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This policy creates a Network Watcher in the specified region to enable Flowlogs for Virtual Networks.

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

Azure Policy JSONVirtualNetwork_Flowlog_TrafficAnalytics_NetworkWatcher_DINE.json · 114 lines
{
  "properties": {
    "displayName": "Create Regional NetworkWatcher in NetworkWatcherRG for VNet Flowlogs",
    "description": "This policy creates a Network Watcher in the specified region to enable Flowlogs for Virtual Networks.",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "location": {
        "type": "String",
        "metadata": {
          "displayName": "Network Watcher Location",
          "description": "Location where the Network Watcher exists or will be created in. It should match the location of the Virtual Network where flow logs are to be enabled.",
          "strongType": "location"
        }
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Network/networkWatchers"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Network/networkWatchers",
          "name": "[toLower(concat('networkWatcher_', parameters('location')))]",
          "existenceCondition": {
            "allof": [
              {
                "field": "id",
                "equals": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/NetworkWatcherRG/providers/Microsoft.Network/networkWatchers/networkWatcher_', parameters('location'))]"
              },
              {
                "field": "location",
                "equals": "[parameters('location')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ],
          "deployment": {
            "properties": {
              "mode": "Incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "location": {
                    "type": "string"
                  }
                },
                "variables": {
                  "networkWatcherName": "[concat('networkWatcher_', parameters('location'))]"
                },
                "resources": [
                  {
                    "type": "Microsoft.Resources/deployments",
                    "apiVersion": "2021-04-01",
                    "name": "deployNetworkWatcherForFlowlog",
                    "properties": {
                      "mode": "Incremental",
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "resources": [
                          {
                            "type": "Microsoft.Network/networkWatchers",
                            "apiVersion": "2022-07-01",
                            "name": "[variables('networkWatcherName')]",
                            "location": "[parameters('location')]",
                            "properties": {}
                          }
                        ]
                      }
                    },
                    "resourceGroup": "NetworkWatcherRG"
                  }
                ]
              },
              "parameters": {
                "location": {
                  "value": "[parameters('location')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/6f4374d1-455b-4f14-83d9-3bf5133c5bb5",
  "name": "6f4374d1-455b-4f14-83d9-3bf5133c5bb5"
}
Bylaw6f4374d1-455b-4f14-83d9-3bf5133c5bb5.bylaw.ps1 · 37 lines
# Converted from VirtualNetwork_Flowlog_TrafficAnalytics_NetworkWatcher_DINE.json by ConvertTo-Bylaw.
Policy '6f4374d1-455b-4f14-83d9-3bf5133c5bb5' {

    Properties @{
        DisplayName   = 'Create Regional NetworkWatcher in NetworkWatcherRG for VNet Flowlogs'
        Description   = 'This policy creates a Network Watcher in the specified region to enable Flowlogs for Virtual Networks.'
        Version       = '1.0.0'
        Category      = 'Network'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter Location @{
        Type        = 'String'
        DisplayName = 'Network Watcher Location'
        Description = 'Location where the Network Watcher exists or will be created in. It should match the location of the Virtual Network where flow logs are to be enabled.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }

    Resource 'Microsoft.Network/networkWatchers'

    DeployIfNotExists 'Microsoft.Network/networkWatchers' -Role 'Network Contributor' -Name (Arm '[toLower(concat(''networkWatcher_'', parameters(''location'')))]') -Effect (Param Effect) {
        Require 'id and location are as required' {
            Field id | Must -Be (Arm '[concat(''/subscriptions/'', subscription().subscriptionId, ''/resourceGroups/NetworkWatcherRG/providers/Microsoft.Network/networkWatchers/networkWatcher_'', parameters(''location''))]')
            Field location | Must -Be (Param Location)
        }
        Deployment -TemplateFile './templates/6f4374d1-455b-4f14-83d9-3bf5133c5bb5.json' -Parameters ([ordered]@{
                location = (Param Location)
            })
    }
}

Its deployment templates are written beside it:

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