Bylaw

Gallery › Network › Azure built-in policies

Configure virtual networks to enforce workspace, storage account and retention interval for Flow logs and Traffic Analytics

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.1.2

If a virtual network already has traffic analytics enabled, then, this policy will overwrite its existing settings with the ones provided during policy creation. Traffic analytics is a cloud-based solution that provides visibility into user and application activity in cloud networks.

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

Azure Policy JSONVirtualNetwork_FlowLog_TrafficAnalytics_Update.json · 333 lines
{
  "properties": {
    "displayName": "Configure virtual networks to enforce workspace, storage account and retention interval for Flow logs and Traffic Analytics",
    "description": "If a virtual network already has traffic analytics enabled, then, this policy will overwrite its existing settings with the ones provided during policy creation. Traffic analytics is a cloud-based solution that provides visibility into user and application activity in cloud networks.",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "metadata": {
      "version": "1.1.2",
      "category": "Network"
    },
    "version": "1.1.2",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "vnetRegion": {
        "type": "string",
        "metadata": {
          "displayName": "Virtual Networks Region",
          "description": "Configures for virtual networks in the selected region only.",
          "strongType": "location"
        }
      },
      "storageId": {
        "type": "string",
        "metadata": {
          "displayName": "Storage Account",
          "description": "The resource id of storage account where flowlogs will be sent to. It will be used for deployment purposes only. Make sure this storage account is located in the same region as the Vnet.",
          "assignPermissions": true,
          "strongType": "Microsoft.Storage/storageAccounts"
        }
      },
      "timeInterval": {
        "type": "string",
        "metadata": {
          "displayName": "Traffic analytics processing interval in minutes",
          "description": "Traffic analytics processes blobs at the selected frequency."
        },
        "defaultValue": "60",
        "allowedValues": [
          "10",
          "60"
        ]
      },
      "workspaceResourceId": {
        "type": "string",
        "metadata": {
          "displayName": "Workspace Resource ID",
          "description": "Log Analytics workspace resource id",
          "assignPermissions": true,
          "strongType": "Microsoft.OperationalInsights/workspaces"
        }
      },
      "workspaceRegion": {
        "type": "string",
        "metadata": {
          "displayName": "Workspace Region",
          "description": "Log Analytics workspace region",
          "strongType": "location"
        }
      },
      "workspaceId": {
        "type": "string",
        "defaultValue": "00000000-0000-0000-0000-000000000000",
        "metadata": {
          "displayName": "Log Analytics Workspace",
          "description": "Log Analytics workspace GUID - deprecated",
          "deprecated": true
        }
      },
      "networkWatcherRG": {
        "type": "string",
        "defaultValue": "NetworkWatcherRG",
        "metadata": {
          "displayName": "Network Watchers RG",
          "description": "The name of the resource group where the flowLog resources will be created. This will be used only if a deployment is required. This is the resource group where the Network Watchers are located.",
          "strongType": "existingResourceGroups"
        }
      },
      "networkWatcherName": {
        "type": "string",
        "metadata": {
          "displayName": "Network Watcher",
          "description": "The resource id of the network watcher under which the flowLog resources will be created. Make sure it belongs to the same region as the Vnet.",
          "strongType": "Microsoft.Network/networkWatchers"
        }
      },
      "retentionDays": {
        "type": "string",
        "metadata": {
          "displayName": "Number of days to retain flowlogs",
          "description": "The number of days for which flowlog data will be retained in storage account. If you want to retain data forever and do not want to apply any retention policy, set retention (days) to 0."
        },
        "defaultValue": "30"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/virtualNetworks"
          },
          {
            "field": "location",
            "equals": "[parameters('vnetRegion')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Network/networkWatchers/flowlogs",
          "resourceGroupName": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs'))), parameters('networkWatcherRG'), split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[4])]",
          "name": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs[*].id'))), 'null/null', concat(split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[8], '/', split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[10]))]",
          "existenceCondition": {
            "anyof": [
              {
                "field": "Microsoft.Network/networkWatchers/flowLogs/enabled",
                "equals": "false"
              },
              {
                "allof": [
                  {
                    "field": "Microsoft.Network/networkWatchers/flowLogs/enabled",
                    "equals": "true"
                  },
                  {
                    "field": "Microsoft.Network/networkWatchers/flowLogs/storageId",
                    "equals": "[parameters('storageId')]"
                  },
                  {
                    "field": "Microsoft.Network/networkWatchers/flowLogs/flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.enabled",
                    "equals": "true"
                  },
                  {
                    "field": "Microsoft.Network/networkWatchers/flowLogs/flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.trafficAnalyticsInterval",
                    "in": [
                      "10",
                      "60"
                    ]
                  },
                  {
                    "field": "Microsoft.Network/networkWatchers/flowLogs/flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.workspaceResourceId",
                    "equals": "[parameters('workspaceResourceId')]"
                  },
                  {
                    "anyof": [
                      {
                        "allof": [
                          {
                            "value": "[parameters('retentionDays')]",
                            "equals": "0"
                          },
                          {
                            "anyof": [
                              {
                                "field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.days",
                                "equals": "0"
                              },
                              {
                                "field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.enabled",
                                "equals": "false"
                              }
                            ]
                          }
                        ]
                      },
                      {
                        "allof": [
                          {
                            "value": "[parameters('retentionDays')]",
                            "notequals": "0"
                          },
                          {
                            "field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.days",
                            "notequals": "0"
                          },
                          {
                            "field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.enabled",
                            "equals": "true"
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "storageId": {
                    "type": "string"
                  },
                  "timeInterval": {
                    "type": "string"
                  },
                  "workspaceRegion": {
                    "type": "string"
                  },
                  "workspaceResourceId": {
                    "type": "string"
                  },
                  "networkWatcherRG": {
                    "type": "string"
                  },
                  "networkWatcherName": {
                    "type": "string"
                  },
                  "flowlogName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "targetResource": {
                    "type": "string"
                  },
                  "retentionDays": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "type": "Microsoft.Resources/deployments",
                    "name": "[concat('flowlogDeployment-', uniqueString(parameters('flowlogName')))]",
                    "apiVersion": "2022-09-01",
                    "resourceGroup": "[parameters('networkWatcherRG')]",
                    "properties": {
                      "mode": "incremental",
                      "parameters": {},
                      "template": {
                        "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {},
                        "resources": [
                          {
                            "type": "Microsoft.Network/networkWatchers/flowLogs",
                            "name": "[concat(parameters('networkWatcherName'), '/', parameters('flowlogName'))]",
                            "apiVersion": "2022-09-01",
                            "location": "[parameters('location')]",
                            "properties": {
                              "targetResourceId": "[parameters('targetResource')]",
                              "storageId": "[parameters('storageId')]",
                              "enabled": "true",
                              "flowAnalyticsConfiguration": {
                                "networkWatcherFlowAnalyticsConfiguration": {
                                  "enabled": true,
                                  "workspaceRegion": "[parameters('workspaceRegion')]",
                                  "workspaceResourceId": "[parameters('workspaceResourceId')]",
                                  "trafficAnalyticsInterval": "[parameters('timeInterval')]"
                                }
                              },
                              "retentionPolicy": {
                                "days": "[parameters('retentionDays')]",
                                "enabled": "true"
                              },
                              "format": {
                                "type": "JSON",
                                "version": 2
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                ]
              },
              "parameters": {
                "storageId": {
                  "value": "[parameters('storageId')]"
                },
                "timeInterval": {
                  "value": "[parameters('timeInterval')]"
                },
                "workspaceRegion": {
                  "value": "[parameters('workspaceRegion')]"
                },
                "workspaceResourceId": {
                  "value": "[parameters('workspaceResourceId')]"
                },
                "networkWatcherRG": {
                  "value": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs'))), parameters('networkWatcherRG'), split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[4])]"
                },
                "networkWatcherName": {
                  "value": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs'))), last(split(parameters('networkWatcherName'), '/')), split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[8])]"
                },
                "flowlogName": {
                  "value": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs'))), concat(take(concat(field('name'), '-', resourceGroup().name), 72), '-', 'flowlog'), split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[10])]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "targetResource": {
                  "value": "[concat(resourceGroup().id, '/providers/Microsoft.Network/virtualNetworks/', field('name'))]"
                },
                "retentionDays": {
                  "value": "[parameters('retentionDays')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.1.2",
      "1.1.1",
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/052c180e-287d-44c3-86ef-01aeae2d9774",
  "name": "052c180e-287d-44c3-86ef-01aeae2d9774"
}
Bylaw052c180e-287d-44c3-86ef-01aeae2d9774.bylaw.ps1 · 125 lines
# Converted from VirtualNetwork_FlowLog_TrafficAnalytics_Update.json by ConvertTo-Bylaw.
Policy '052c180e-287d-44c3-86ef-01aeae2d9774' {

    Properties @{
        DisplayName   = 'Configure virtual networks to enforce workspace, storage account and retention interval for Flow logs and Traffic Analytics'
        Description   = 'If a virtual network already has traffic analytics enabled, then, this policy will overwrite its existing settings with the ones provided during policy creation. Traffic analytics is a cloud-based solution that provides visibility into user and application activity in cloud networks.'
        Version       = '1.1.2'
        Category      = 'Network'
        PolicyVersion = '1.1.2'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter VnetRegion @{
        Type        = 'string'
        DisplayName = 'Virtual Networks Region'
        Description = 'Configures for virtual networks in the selected region only.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter StorageId @{
        Type        = 'string'
        DisplayName = 'Storage Account'
        Description = 'The resource id of storage account where flowlogs will be sent to. It will be used for deployment purposes only. Make sure this storage account is located in the same region as the Vnet.'
        Metadata    = [ordered]@{ assignPermissions = $true; strongType = 'Microsoft.Storage/storageAccounts' }
    }
    Parameter TimeInterval @{
        Type          = 'string'
        AllowedValues = @('10', '60')
        Default       = '60'
        DisplayName   = 'Traffic analytics processing interval in minutes'
        Description   = 'Traffic analytics processes blobs at the selected frequency.'
    }
    Parameter WorkspaceResourceId @{
        Type        = 'string'
        DisplayName = 'Workspace Resource ID'
        Description = 'Log Analytics workspace resource id'
        Metadata    = [ordered]@{ assignPermissions = $true; strongType = 'Microsoft.OperationalInsights/workspaces' }
    }
    Parameter WorkspaceRegion @{
        Type        = 'string'
        DisplayName = 'Workspace Region'
        Description = 'Log Analytics workspace region'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter WorkspaceId @{
        Type        = 'string'
        Default     = '00000000-0000-0000-0000-000000000000'
        DisplayName = 'Log Analytics Workspace'
        Description = 'Log Analytics workspace GUID - deprecated'
        Metadata    = [ordered]@{ deprecated = $true }
    }
    Parameter NetworkWatcherRG @{
        Type        = 'string'
        Default     = 'NetworkWatcherRG'
        DisplayName = 'Network Watchers RG'
        Description = 'The name of the resource group where the flowLog resources will be created. This will be used only if a deployment is required. This is the resource group where the Network Watchers are located.'
        Metadata    = [ordered]@{ strongType = 'existingResourceGroups' }
    }
    Parameter NetworkWatcherName @{
        Type        = 'string'
        DisplayName = 'Network Watcher'
        Description = 'The resource id of the network watcher under which the flowLog resources will be created. Make sure it belongs to the same region as the Vnet.'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Network/networkWatchers' }
    }
    Parameter RetentionDays @{
        Type        = 'string'
        Default     = '30'
        DisplayName = 'Number of days to retain flowlogs'
        Description = 'The number of days for which flowlog data will be retained in storage account. If you want to retain data forever and do not want to apply any retention policy, set retention (days) to 0.'
    }

    Resource 'Microsoft.Network/virtualNetworks' {

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

    DeployIfNotExists 'Microsoft.Network/networkWatchers/flowlogs' -Role 'Contributor' -Name (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id''))), ''null/null'', concat(split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[8], ''/'', split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[10]))]') -ResourceGroupName (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs''))), parameters(''networkWatcherRG''), split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[4])]') -Effect (Param Effect) {
        Require 'enabled, storageId, flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.enabled and 4 more are as required' {
            AnyOf {
                Field enabled | Must -Be 'false'
                AllOf {
                    Field enabled | Must -Be 'true'
                    Field storageId | Must -Be (Param StorageId)
                    Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.enabled | Must -Be 'true'
                    Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.trafficAnalyticsInterval | Must -BeIn @('10', '60')
                    Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.workspaceResourceId | Must -Be (Param WorkspaceResourceId)
                    AnyOf {
                        AllOf {
                            Param RetentionDays | Must -Be '0'
                            AnyOf {
                                Field retentionPolicy.days | Must -Be '0'
                                Field retentionPolicy.enabled | Must -Be 'false'
                            }
                        }
                        AllOf {
                            Param RetentionDays | Must -Not -Be '0'
                            Field retentionPolicy.days | Must -Not -Be '0' -IfPresent
                            Field retentionPolicy.enabled | Must -Be 'true'
                        }
                    }
                }
            }
        }
        Deployment -TemplateFile './templates/052c180e-287d-44c3-86ef-01aeae2d9774.json' -Parameters ([ordered]@{
                storageId           = (Param StorageId)
                timeInterval        = (Param TimeInterval)
                workspaceRegion     = (Param WorkspaceRegion)
                workspaceResourceId = (Param WorkspaceResourceId)
                networkWatcherRG    = (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs''))), parameters(''networkWatcherRG''), split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[4])]')
                networkWatcherName  = (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs''))), last(split(parameters(''networkWatcherName''), ''/'')), split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[8])]')
                flowlogName         = (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs''))), concat(take(concat(field(''name''), ''-'', resourceGroup().name), 72), ''-'', ''flowlog''), split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[10])]')
                location            = (Arm '[field(''location'')]')
                targetResource      = (Arm '[concat(resourceGroup().id, ''/providers/Microsoft.Network/virtualNetworks/'', field(''name''))]')
                retentionDays       = (Param RetentionDays)
            })
    }
}

Its deployment templates are written beside it:

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