Quickstart: Configure network security group flow logs using an Azure Resource Manager (ARM) template
In this quickstart, you learn how to enable network security group (NSG) flow logs using an Azure Resource Manager (ARM) template and Azure PowerShell.
A resource manager template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. The template uses declarative syntax. In declarative syntax, you describe your intended deployment without writing the sequence of programming commands to create the deployment.
We start with an overview of the properties of the NSG flow log object. We provide sample templates. Then, we use a local Azure PowerShell instance to deploy the template.
If your environment meets the prerequisites and you're familiar with using ARM templates, select the Deploy to Azure button. The template opens in the Azure portal.
Prerequisites
If you don't have an Azure subscription, create a free account before you begin.
Review the template
The template that we use in this quickstart is from Azure Quickstart Templates.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.9.1.41621",
"templateHash": "14580725600461536175"
}
},
"parameters": {
"networkWatcherName": {
"type": "string",
"defaultValue": "[format('NetworkWatcher_{0}', parameters('location'))]",
"metadata": {
"description": "Name of the Network Watcher attached to your subscription. Format: NetworkWatcher_<region_name>"
}
},
"flowLogName": {
"type": "string",
"defaultValue": "FlowLog1",
"metadata": {
"description": "Name of your Flow log resource"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Region where you resources are located"
}
},
"existingNSG": {
"type": "string",
"metadata": {
"description": "Resource ID of the target NSG"
}
},
"retentionDays": {
"type": "int",
"defaultValue": 0,
"maxValue": 365,
"minValue": 0,
"metadata": {
"description": "Retention period in days. Default is zero which stands for permanent retention. Can be any Integer from 0 to 365"
}
},
"flowLogsVersion": {
"type": "int",
"defaultValue": 2,
"allowedValues": [
1,
2
],
"metadata": {
"description": "FlowLogs Version. Correct values are 1 or 2 (default)"
}
},
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_ZRS"
],
"metadata": {
"description": "Storage Account type"
}
}
},
"variables": {
"storageAccountName": "[format('flowlogs{0}', uniqueString(resourceGroup().id))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-09-01",
"name": "[variables('storageAccountName')]",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"kind": "StorageV2",
"properties": {}
},
{
"type": "Microsoft.Network/networkWatchers",
"apiVersion": "2022-01-01",
"name": "[parameters('networkWatcherName')]",
"location": "[parameters('location')]",
"properties": {}
},
{
"type": "Microsoft.Network/networkWatchers/flowLogs",
"apiVersion": "2022-01-01",
"name": "[format('{0}/{1}', parameters('networkWatcherName'), parameters('flowLogName'))]",
"location": "[parameters('location')]",
"properties": {
"targetResourceId": "[parameters('existingNSG')]",
"storageId": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
"enabled": true,
"retentionPolicy": {
"days": "[parameters('retentionDays')]",
"enabled": true
},
"format": {
"type": "JSON",
"version": "[parameters('flowLogsVersion')]"
}
},
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
]
}
]
}
These resources are defined in the template:
- Microsoft.Storage/storageAccounts
- Microsoft.Network networkWatchers
- Microsoft.Network networkWatchers/flowLogs
The highlighted code in the preceding sample shows an NSG flow logs resource definition.
Deploy the template
This tutorial assumes that you have an existing resource group and an NSG that you can enable flow logging on.
You can save any of the example templates that are shown in this article locally as azuredeploy.json. Update the property values so they point to valid resources in your subscription.
To deploy the template, run the following command in Azure PowerShell:
$context = Get-AzSubscription -SubscriptionId <subscription Id>
Set-AzContext $context
New-AzResourceGroupDeployment -Name EnableFlowLog -ResourceGroupName NetworkWatcherRG `
-TemplateFile "C:\MyTemplates\azuredeploy.json"
Note
These commands deploy a resource to the example NetworkWatcherRG resource group, and not to the resource group that contains the NSG.
Validate the deployment
You have two options to see whether your deployment succeeded:
- Your PowerShell console shows
ProvisioningState
asSucceeded
. - Go to the NSG flow logs portal page to confirm your changes.
If there were issues with the deployment, see Troubleshoot common Azure deployment errors with Azure Resource Manager.
Clean up resources
You can delete Azure resources by using complete deployment mode. To delete a flow logs resource, specify a deployment in complete mode without including the resource you want to delete. Read more about complete deployment mode.
You also can disable an NSG flow log in the Azure portal:
- Sign in to the Azure portal.
- Select All services. In the Filter box, enter network watcher. In the search results, select Network Watcher.
- Under Logs, select NSG flow logs.
- In the list of NSGs, select the NSG for which you want to disable flow logs.
- Under Flow logs settings, select Off.
- Select Save.
Next steps
In this quickstart, you learned how to enable NSG flow logs by using an ARM template. Next, learn how to visualize your NSG flow data by using one of these options:
Feedback
Submit and view feedback for