Bylaw

Gallery › Network › Azure Community Policy

Deploy a user-defined route to a VNET with specific routes.

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Deploy a user-defined route to a VNET with routes from spoke to hub firewall. This policy must be assigned for each region you plan to use.

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

Azure Policy JSONazurepolicy.json · 127 lines
{
  "name": "e96c820e-7358-4829-af8d-8f996dd43c87",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy a user-defined route to a VNET with specific routes.",
    "description": "Deploy a user-defined route to a VNET with routes from spoke to hub firewall. This policy must be assigned for each region you plan to use.",
    "metadata": {
      "version": "1.0.0",
      "category": "Network"
    },
    "mode": "Indexed",
    "parameters": {
      "defaultRoute": {
        "type": "String",
        "metadata": {
          "displayName": "Default route to add into UDR",
          "description": "Policy will deploy a default route table to a vnet"
        }
      },
      "vnetRegion": {
        "type": "String",
        "metadata": {
          "displayName": "VNet Region",
          "description": "Regional VNet hub location",
          "strongType": "location"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/virtualNetworks"
          },
          {
            "field": "location",
            "equals": "[parameters('vnetRegion')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Network/routeTables",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Network/routeTables/routes[*].nextHopIpAddress",
                "equals": "[parameters('defaultRoute')]"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "udrName": {
                  "value": "[concat(field('name'),'-udr')]"
                },
                "udrLocation": {
                  "value": "[field('location')]"
                },
                "defaultRoute": {
                  "value": "[parameters('defaultRoute')]"
                }
              },
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "udrName": {
                    "type": "string"
                  },
                  "udrLocation": {
                    "type": "string"
                  },
                  "defaultRoute": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Network/routeTables",
                    "name": "[parameters('udrName')]",
                    "apiVersion": "2022-07-01",
                    "location": "[parameters('udrLocation')]",
                    "properties": {
                      "routes": [
                        {
                          "name": "AzureFirewallRoute",
                          "properties": {
                            "addressPrefix": "0.0.0.0/0",
                            "nextHopType": "VirtualAppliance",
                            "nextHopIpAddress": "[parameters('defaultRoute')]"
                          }
                        }
                      ]
                    }
                  }
                ],
                "outputs": {}
              }
            }
          }
        }
      }
    }
  }
}
Bylawe96c820e-7358-4829-af8d-8f996dd43c87.bylaw.ps1 · 50 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'e96c820e-7358-4829-af8d-8f996dd43c87' {

    Properties @{
        DisplayName = 'Deploy a user-defined route to a VNET with specific routes.'
        Description = 'Deploy a user-defined route to a VNET with routes from spoke to hub firewall. This policy must be assigned for each region you plan to use.'
        Version     = '1.0.0'
        Category    = 'Network'
        Mode        = 'Indexed'
    }

    Parameter DefaultRoute @{
        Type        = 'String'
        DisplayName = 'Default route to add into UDR'
        Description = 'Policy will deploy a default route table to a vnet'
    }
    Parameter VnetRegion @{
        Type        = 'String'
        DisplayName = 'VNet Region'
        Description = 'Regional VNet hub location'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Network/virtualNetworks' {

        Require 'location is not the VnetRegion parameter' {
            Field location | Must -Not -Be (Param VnetRegion) -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Network/routeTables' -Role 'Network Contributor' -Effect (Param Effect) {
        Require 'routes and nextHopIpAddress are as required' {
            Field routes | Must -Exist
            Each 'routes[*]' {
                Field nextHopIpAddress | Must -Be (Param DefaultRoute) -Strict
            }
        }
        Deployment -TemplateFile './templates/e96c820e-7358-4829-af8d-8f996dd43c87.json' -Parameters ([ordered]@{
                udrName      = (Arm '[concat(field(''name''),''-udr'')]')
                udrLocation  = (Arm '[field(''location'')]')
                defaultRoute = (Param DefaultRoute)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Network/deploy-a-user-defined-route-to-a-vnet-with-specific-routes/azurepolicy.json in Azure/Community-Policy, under the MIT licence.