Bylaw

Gallery › Network › Azure Community Policy

Deploy DNS Security Policy VNet Link by Region

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

Creates a DNS Resolver Policy Virtual Network Link for every VNet using Azure-provided DNS, linking it to the corresponding regional DNS Security Policy in a central subscription. Uses a tag-based existence check and a cross-subscription nested deployment to create the link resource.

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

Azure Policy JSONazurepolicy.json · 241 lines
{
    "name": "5f17ac77-eb44-421c-b142-0fd591d188fd",
    "type": "Microsoft.Authorization/policyDefinitions",
    "properties": {
        "displayName": "Deploy DNS Security Policy VNet Link by Region",
        "description": "Creates a DNS Resolver Policy Virtual Network Link for every VNet using Azure-provided DNS, linking it to the corresponding regional DNS Security Policy in a central subscription. Uses a tag-based existence check and a cross-subscription nested deployment to create the link resource.",
        "metadata": {
            "version": "1.0.0",
            "category": "Network"
        },
        "mode": "All",
        "parameters": {
            "effect": {
                "type": "String",
                "metadata": {
                    "displayName": "Effect",
                    "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
                },
                "allowedValues": [
                    "DeployIfNotExists",
                    "AuditIfNotExists",
                    "Disabled"
                ],
                "defaultValue": "DeployIfNotExists"
            },
            "securitySubscriptionId": {
                "type": "String",
                "metadata": {
                    "displayName": "Security Subscription ID",
                    "description": "Subscription ID where the DNS Resolver Policies are deployed."
                }
            },
            "securityResourceGroup": {
                "type": "String",
                "metadata": {
                    "displayName": "Security Resource Group",
                    "description": "Resource group containing the DNS Resolver Policies."
                }
            },
            "supportedLocations": {
                "type": "Array",
                "metadata": {
                    "displayName": "Supported Locations",
                    "description": "ARM location names where DNS Security Policies exist. VNets in other locations are ignored."
                }
            },
            "locationToPolicyMap": {
                "type": "Object",
                "metadata": {
                    "displayName": "Location to Policy Name Map",
                    "description": "Maps ARM location name (key) to the DNS Resolver Policy resource name (value). Example: {\"westeurope\":\"weu-dnsrp-blocklist\"}"
                }
            }
        },
        "policyRule": {
            "if": {
                "allOf": [
                    {
                        "field": "type",
                        "equals": "Microsoft.Network/virtualNetworks"
                    },
                    {
                        "anyOf": [
                            {
                                "field": "Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers",
                                "exists": false
                            },
                            {
                                "count": {
                                    "field": "Microsoft.Network/virtualNetworks/dhcpOptions.dnsServers[*]"
                                },
                                "equals": 0
                            }
                        ]
                    },
                    {
                        "field": "location",
                        "in": "[parameters('supportedLocations')]"
                    }
                ]
            },
            "then": {
                "effect": "[parameters('effect')]",
                "details": {
                    "type": "Microsoft.Network/virtualNetworks",
                    "name": "[field('name')]",
                    "existenceCondition": {
                        "field": "tags['dnsSecurityPolicyLinked']",
                        "exists": true
                    },
                    "roleDefinitionIds": [
                        "/providers/Microsoft.Authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7",
                        "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
                    ],
                    "deployment": {
                        "properties": {
                            "mode": "incremental",
                            "parameters": {
                                "vnetName": {
                                    "value": "[field('name')]"
                                },
                                "vnetResourceId": {
                                    "value": "[field('id')]"
                                },
                                "vnetLocation": {
                                    "value": "[field('location')]"
                                },
                                "vnetResourceGroup": {
                                    "value": "[resourceGroup().name]"
                                },
                                "locationToPolicyMap": {
                                    "value": "[parameters('locationToPolicyMap')]"
                                },
                                "securitySubscriptionId": {
                                    "value": "[parameters('securitySubscriptionId')]"
                                },
                                "securityResourceGroup": {
                                    "value": "[parameters('securityResourceGroup')]"
                                },
                                "existingTags": {
                                    "value": "[if(empty(field('tags')), createObject(), field('tags'))]"
                                }
                            },
                            "template": {
                                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                                "contentVersion": "1.0.0.0",
                                "parameters": {
                                    "vnetName": {
                                        "type": "string"
                                    },
                                    "vnetResourceId": {
                                        "type": "string"
                                    },
                                    "vnetLocation": {
                                        "type": "string"
                                    },
                                    "vnetResourceGroup": {
                                        "type": "string"
                                    },
                                    "locationToPolicyMap": {
                                        "type": "object"
                                    },
                                    "securitySubscriptionId": {
                                        "type": "string"
                                    },
                                    "securityResourceGroup": {
                                        "type": "string"
                                    },
                                    "existingTags": {
                                        "type": "object"
                                    }
                                },
                                "variables": {
                                    "cleanRg": "[toLower(replace(replace(replace(parameters('vnetResourceGroup'), '_', '-'), '.', '-'), ' ', '-'))]",
                                    "cleanName": "[toLower(replace(replace(replace(parameters('vnetName'), '_', '-'), '.', '-'), ' ', '-'))]",
                                    "linkName": "[take(concat(variables('cleanRg'), '-', variables('cleanName'), '-dnsl'), 80)]",
                                    "policyName": "[parameters('locationToPolicyMap')[parameters('vnetLocation')]]",
                                    "deployHash": "[take(uniqueString(parameters('vnetResourceId'), deployment().name), 8)]",
                                    "crossSubDeployName": "[take(concat('dnslink-', variables('deployHash')), 64)]",
                                    "mergedTags": "[union(parameters('existingTags'), createObject('dnsSecurityPolicyLinked', variables('policyName')))]"
                                },
                                "resources": [
                                    {
                                        "type": "Microsoft.Resources/deployments",
                                        "apiVersion": "2022-09-01",
                                        "name": "[variables('crossSubDeployName')]",
                                        "subscriptionId": "[parameters('securitySubscriptionId')]",
                                        "resourceGroup": "[parameters('securityResourceGroup')]",
                                        "properties": {
                                            "expressionEvaluationOptions": {
                                                "scope": "inner"
                                            },
                                            "mode": "Incremental",
                                            "template": {
                                                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                                                "contentVersion": "1.0.0.0",
                                                "parameters": {
                                                    "policyName": {
                                                        "type": "string"
                                                    },
                                                    "linkName": {
                                                        "type": "string"
                                                    },
                                                    "vnetLocation": {
                                                        "type": "string"
                                                    },
                                                    "vnetResourceId": {
                                                        "type": "string"
                                                    }
                                                },
                                                "resources": [
                                                    {
                                                        "type": "Microsoft.Network/dnsResolverPolicies/virtualNetworkLinks",
                                                        "apiVersion": "2025-10-01-preview",
                                                        "name": "[concat(parameters('policyName'), '/', parameters('linkName'))]",
                                                        "location": "[parameters('vnetLocation')]",
                                                        "properties": {
                                                            "virtualNetwork": {
                                                                "id": "[parameters('vnetResourceId')]"
                                                            }
                                                        }
                                                    }
                                                ]
                                            },
                                            "parameters": {
                                                "policyName": {
                                                    "value": "[variables('policyName')]"
                                                },
                                                "linkName": {
                                                    "value": "[variables('linkName')]"
                                                },
                                                "vnetLocation": {
                                                    "value": "[parameters('vnetLocation')]"
                                                },
                                                "vnetResourceId": {
                                                    "value": "[parameters('vnetResourceId')]"
                                                }
                                            }
                                        }
                                    },
                                    {
                                        "type": "Microsoft.Resources/tags",
                                        "apiVersion": "2024-03-01",
                                        "name": "default",
                                        "scope": "[resourceId('Microsoft.Network/virtualNetworks', parameters('vnetName'))]",
                                        "dependsOn": [
                                            "[variables('crossSubDeployName')]"
                                        ],
                                        "properties": {
                                            "tags": "[variables('mergedTags')]"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}
Bylaw5f17ac77-eb44-421c-b142-0fd591d188fd.bylaw.ps1 · 68 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '5f17ac77-eb44-421c-b142-0fd591d188fd' {

    Properties @{
        DisplayName = 'Deploy DNS Security Policy VNet Link by Region'
        Description = 'Creates a DNS Resolver Policy Virtual Network Link for every VNet using Azure-provided DNS, linking it to the corresponding regional DNS Security Policy in a central subscription. Uses a tag-based existence check and a cross-subscription nested deployment to create the link resource.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }
    Parameter SecuritySubscriptionId @{
        Type        = 'String'
        DisplayName = 'Security Subscription ID'
        Description = 'Subscription ID where the DNS Resolver Policies are deployed.'
    }
    Parameter SecurityResourceGroup @{
        Type        = 'String'
        DisplayName = 'Security Resource Group'
        Description = 'Resource group containing the DNS Resolver Policies.'
    }
    Parameter SupportedLocations @{
        Type        = 'Array'
        DisplayName = 'Supported Locations'
        Description = 'ARM location names where DNS Security Policies exist. VNets in other locations are ignored.'
    }
    Parameter LocationToPolicyMap @{
        Type        = 'Object'
        DisplayName = 'Location to Policy Name Map'
        Description = 'Maps ARM location name (key) to the DNS Resolver Policy resource name (value). Example: {"westeurope":"weu-dnsrp-blocklist"}'
    }

    Resource 'Microsoft.Network/virtualNetworks' {

        When {
            AnyOf {
                Field dhcpOptions.dnsServers | Must -Not -Exist
                Count 'dhcpOptions.dnsServers[*]' | Must -Be 0
            }
        }

        Require 'location is not one of the SupportedLocations parameter' {
            Field location | Must -Not -BeIn (Param SupportedLocations) -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Network/virtualNetworks' -Role 'Network Contributor', 'Contributor' -Name (Arm '[field(''name'')]') -Effect (Param Effect) {
        Require 'tag dnsSecurityPolicyLinked exists' {
            Tag dnsSecurityPolicyLinked | Must -Exist
        }
        Deployment -TemplateFile './templates/5f17ac77-eb44-421c-b142-0fd591d188fd.json' -Parameters ([ordered]@{
                vnetName               = (Arm '[field(''name'')]')
                vnetResourceId         = (Arm '[field(''id'')]')
                vnetLocation           = (Arm '[field(''location'')]')
                vnetResourceGroup      = (Arm '[resourceGroup().name]')
                locationToPolicyMap    = (Param LocationToPolicyMap)
                securitySubscriptionId = (Param SecuritySubscriptionId)
                securityResourceGroup  = (Param SecurityResourceGroup)
                existingTags           = (Arm '[if(empty(field(''tags'')), createObject(), field(''tags''))]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Network/deploy-dns-security-policy-vnet-link-by-region/azurepolicy.json in Azure/Community-Policy, under the MIT licence.