Bylaw

Gallery › Network › Azure built-in policies

Virtual networks should be protected by Azure DDoS Protection

Converted Policy Audit Disabled Modify Mode Indexed Version 1.0.1

Protect your virtual networks against volumetric and protocol attacks with Azure DDoS Protection. For more information, visit https://aka.ms/ddosprotectiondocs.

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

Azure Policy JSONVirtualNetworkDdosStandard_Audit.json · 85 lines
{
  "properties": {
    "displayName": "Virtual networks should be protected by Azure DDoS Protection",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Protect your virtual networks against volumetric and protocol attacks with Azure DDoS Protection. For more information, visit https://aka.ms/ddosprotectiondocs.",
    "metadata": {
      "version": "1.0.1",
      "category": "Network"
    },
    "version": "1.0.1",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "Modify",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Modify"
      },
      "ddosPlan": {
        "type": "String",
        "metadata": {
          "displayName": "DDoS Protection Plan",
          "description": "DDoS Protection Plan resource to be associated to the virtual networks",
          "strongType": "Microsoft.Network/ddosProtectionPlans"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/virtualNetworks"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Network/virtualNetworks/enableDdosProtection",
                "notEquals": true
              },
              {
                "field": "Microsoft.Network/virtualNetworks/ddosProtectionPlan",
                "equals": ""
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "conflictEffect": "audit",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7"
          ],
          "operations": [
            {
              "operation": "addOrReplace",
              "field": "Microsoft.Network/virtualNetworks/enableDdosProtection",
              "value": true
            },
            {
              "operation": "addOrReplace",
              "field": "Microsoft.Network/virtualNetworks/ddosProtectionPlan.id",
              "value": "[parameters('ddosPlan')]"
            }
          ]
        }
      }
    },
    "versions": [
      "1.0.1",
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/94de2ad3-e0c1-4caf-ad78-5d47bbc83d3d",
  "name": "94de2ad3-e0c1-4caf-ad78-5d47bbc83d3d"
}
Bylaw94de2ad3-e0c1-4caf-ad78-5d47bbc83d3d.bylaw.ps1 · 41 lines
# Converted from VirtualNetworkDdosStandard_Audit.json by ConvertTo-Bylaw.
Policy '94de2ad3-e0c1-4caf-ad78-5d47bbc83d3d' {

    Properties @{
        DisplayName   = 'Virtual networks should be protected by Azure DDoS Protection'
        Description   = 'Protect your virtual networks against volumetric and protocol attacks with Azure DDoS Protection. For more information, visit https://aka.ms/ddosprotectiondocs.'
        Version       = '1.0.1'
        Category      = 'Network'
        PolicyVersion = '1.0.1'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('Modify', 'Audit', 'Disabled')
        Default       = 'Modify'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter DdosPlan @{
        Type        = 'String'
        DisplayName = 'DDoS Protection Plan'
        Description = 'DDoS Protection Plan resource to be associated to the virtual networks'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Network/ddosProtectionPlans' }
    }

    Resource 'Microsoft.Network/virtualNetworks' {

        Require 'enableDdosProtection is true' {
            Field enableDdosProtection | Must -Be $true
        }

        Require 'ddosProtectionPlan is not' {
            Field ddosProtectionPlan | Must -Not -Be '' -IfPresent
        }
    }

    Modify -Role 'Network Contributor' -ConflictEffect Audit -Effect (Param Effect) {
        Field enableDdosProtection | AddOrReplace $true
        Field ddosProtectionPlan.id | AddOrReplace (Param DdosPlan)
    }
}

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