Bylaw

Gallery › Network › Azure Landing Zones Library

Deploy a route table with specific user defined routes

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Deploys a route table with specific user defined routes when one does not exist. The route table deployed by the policy must be manually associated to subnet(s)

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

Azure Policy JSONDeploy-Custom-Route-Table.alz_policy_definition.json · 211 lines
{
  "name": "Deploy-Custom-Route-Table",
  "properties": {
    "description": "Deploys a route table with specific user defined routes when one does not exist. The route table deployed by the policy must be manually associated to subnet(s)",
    "displayName": "Deploy a route table with specific user defined routes",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Network",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "disableBgpPropagation": {
        "defaultValue": false,
        "metadata": {
          "description": "Disable BGP Propagation",
          "displayName": "DisableBgpPropagation"
        },
        "type": "Boolean"
      },
      "effect": {
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists",
        "metadata": {
          "description": "Enable or disable the execution of the policy",
          "displayName": "Effect"
        },
        "type": "String"
      },
      "requiredRoutes": {
        "metadata": {
          "description": "Routes that must exist in compliant route tables deployed by this policy",
          "displayName": "requiredRoutes"
        },
        "type": "Array"
      },
      "routeTableName": {
        "metadata": {
          "description": "Name of the route table automatically deployed by this policy",
          "displayName": "routeTableName"
        },
        "type": "String"
      },
      "vnetRegion": {
        "metadata": {
          "description": "Only VNets in this region will be evaluated against this policy",
          "displayName": "vnetRegion"
        },
        "type": "String"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "Microsoft.Network/virtualNetworks",
            "field": "type"
          },
          {
            "equals": "[parameters('vnetRegion')]",
            "field": "location"
          }
        ]
      },
      "then": {
        "details": {
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "disableBgpPropagation": {
                  "value": "[parameters('disableBgpPropagation')]"
                },
                "requiredRoutes": {
                  "value": "[parameters('requiredRoutes')]"
                },
                "routeTableName": {
                  "value": "[parameters('routeTableName')]"
                },
                "vnetRegion": {
                  "value": "[parameters('vnetRegion')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "disableBgpPropagation": {
                    "type": "bool"
                  },
                  "requiredRoutes": {
                    "type": "array"
                  },
                  "routeTableName": {
                    "type": "string"
                  },
                  "vnetRegion": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2021-04-01",
                    "name": "routeTableDepl",
                    "properties": {
                      "mode": "Incremental",
                      "parameters": {
                        "disableBgpPropagation": {
                          "value": "[parameters('disableBgpPropagation')]"
                        },
                        "requiredRoutes": {
                          "value": "[parameters('requiredRoutes')]"
                        },
                        "routeTableName": {
                          "value": "[parameters('routeTableName')]"
                        },
                        "vnetRegion": {
                          "value": "[parameters('vnetRegion')]"
                        }
                      },
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                          "disableBgpPropagation": {
                            "type": "bool"
                          },
                          "requiredRoutes": {
                            "type": "array"
                          },
                          "routeTableName": {
                            "type": "string"
                          },
                          "vnetRegion": {
                            "type": "string"
                          }
                        },
                        "resources": [
                          {
                            "apiVersion": "2021-02-01",
                            "location": "[[parameters('vnetRegion')]",
                            "name": "[[parameters('routeTableName')]",
                            "properties": {
                              "copy": "[variables('copyLoop')]",
                              "disableBgpRoutePropagation": "[[parameters('disableBgpPropagation')]"
                            },
                            "type": "Microsoft.Network/routeTables"
                          }
                        ]
                      }
                    },
                    "type": "Microsoft.Resources/deployments"
                  }
                ],
                "variables": {
                  "copyLoop": [
                    {
                      "count": "[[length(parameters('requiredRoutes'))]",
                      "input": {
                        "name": "[[concat('route-',copyIndex('routes'))]",
                        "properties": {
                          "addressPrefix": "[[split(parameters('requiredRoutes')[copyIndex('routes')], ';')[0]]",
                          "nextHopIpAddress": "[[if(equals(toLower(split(parameters('requiredRoutes')[copyIndex('routes')], ';')[1]),'virtualappliance'),split(parameters('requiredRoutes')[copyIndex('routes')], ';')[2], null())]",
                          "nextHopType": "[[split(parameters('requiredRoutes')[copyIndex('routes')], ';')[1]]"
                        }
                      },
                      "name": "routes"
                    }
                  ]
                }
              }
            }
          },
          "existenceCondition": {
            "allOf": [
              {
                "equals": "[parameters('routeTableName')]",
                "field": "name"
              },
              {
                "count": {
                  "field": "Microsoft.Network/routeTables/routes[*]",
                  "where": {
                    "in": "[parameters('requiredRoutes')]",
                    "value": "[concat(current('Microsoft.Network/routeTables/routes[*].addressPrefix'), ';', current('Microsoft.Network/routeTables/routes[*].nextHopType'), if(equals(toLower(current('Microsoft.Network/routeTables/routes[*].nextHopType')),'virtualappliance'), concat(';', current('Microsoft.Network/routeTables/routes[*].nextHopIpAddress')), ''))]"
                  }
                },
                "equals": "[length(parameters('requiredRoutes'))]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/subscriptions/e867a45d-e513-44ac-931e-4741cef80b24/providers/Microsoft.Authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ],
          "type": "Microsoft.Network/routeTables"
        },
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeploy-Custom-Route-Table.bylaw.ps1 · 62 lines
# Converted from Deploy-Custom-Route-Table.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deploy-Custom-Route-Table' {

    Properties @{
        DisplayName = 'Deploy a route table with specific user defined routes'
        Description = 'Deploys a route table with specific user defined routes when one does not exist. The route table deployed by the policy must be manually associated to subnet(s)'
        Category    = 'Network'
        Version     = '1.0.0'
        Mode        = 'Indexed'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter DisableBgpPropagation @{
        Type        = 'Boolean'
        Default     = $false
        DisplayName = 'DisableBgpPropagation'
        Description = 'Disable BGP Propagation'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter RequiredRoutes @{
        Type        = 'Array'
        DisplayName = 'requiredRoutes'
        Description = 'Routes that must exist in compliant route tables deployed by this policy'
    }
    Parameter RouteTableName @{
        Type        = 'String'
        DisplayName = 'routeTableName'
        Description = 'Name of the route table automatically deployed by this policy'
    }
    Parameter VnetRegion @{
        Type        = 'String'
        DisplayName = 'vnetRegion'
        Description = 'Only VNets in this region will be evaluated against this 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 'name and routes are as required' {
            Field name | Must -Be (Param RouteTableName)
            Count 'routes[*]' -Where {
                Arm '[concat(current(''Microsoft.Network/routeTables/routes[*].addressPrefix''), '';'', current(''Microsoft.Network/routeTables/routes[*].nextHopType''), if(equals(toLower(current(''Microsoft.Network/routeTables/routes[*].nextHopType'')),''virtualappliance''), concat('';'', current(''Microsoft.Network/routeTables/routes[*].nextHopIpAddress'')), ''''))]' | Must -BeIn (Param RequiredRoutes)
            } | Must -Be (Param RequiredRoutes -Length)
        }
        Deployment -TemplateFile './templates/Deploy-Custom-Route-Table.json' -Parameters ([ordered]@{
                disableBgpPropagation = (Param DisableBgpPropagation)
                requiredRoutes        = (Param RequiredRoutes)
                routeTableName        = (Param RouteTableName)
                vnetRegion            = (Param VnetRegion)
            })
    }
}

Its deployment templates are written beside it:

Source: policy_definitions/Deploy-Custom-Route-Table.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.