Azure Network Watcher Flow Log

This page shows how to write Terraform and Azure Resource Manager for Network Watcher Flow Log and write them securely.

azurerm_network_watcher_flow_log (Terraform)

The Watcher Flow Log in Network can be configured in Terraform with the resource name azurerm_network_watcher_flow_log. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

azurerm_nsg_log_retention.tf#L1
resource "azurerm_network_watcher_flow_log" "allowed" {
  retention_policy {
    enabled = true
    days    = 90
  }
}
nsg-flow-logs.tf#L25
resource "azurerm_network_watcher_flow_log" "flowlogsaks" {
  network_watcher_name = azurerm_network_watcher.flowlogs.name
  resource_group_name  = azurerm_resource_group.tf-security.name

  network_security_group_id = azurerm_network_security_group.aks.id
  storage_account_id        = azurerm_storage_account.tfflowlogs.id
positive.tf#L1
resource "azurerm_network_watcher_flow_log" "positive1" {
    network_watcher_name = azurerm_network_watcher.test.name
    resource_group_name  = azurerm_resource_group.test.name
    network_security_group_id = azurerm_network_security_group.test.id
    storage_account_id        = azurerm_storage_account.test.id
    enabled                   = true
positive.tf#L1
resource "azurerm_network_watcher_flow_log" "positive1" {
    network_watcher_name = azurerm_network_watcher.test.name
    resource_group_name  = azurerm_resource_group.test.name
    network_security_group_id = azurerm_network_security_group.test.id
    storage_account_id        = azurerm_storage_account.test.id
    enabled                   = true
16.flowlog.tf#L1
resource "azurerm_network_watcher_flow_log" "test" {
  network_watcher_name = "NetworkWatcher_westeurope"
  resource_group_name  = "NetworkWatcherRG"

  network_security_group_id = azurerm_network_security_group.NSG-FRONTEND[0].id
  storage_account_id        = azurerm_storage_account.aksflowlogmf01.id
negative.tf#L1
resource "azurerm_network_watcher_flow_log" "negative1" {
    network_watcher_name = azurerm_network_watcher.test.name
    resource_group_name  = azurerm_resource_group.test.name
    network_security_group_id = azurerm_network_security_group.test.id
    storage_account_id        = azurerm_storage_account.test.id
    enabled                   = true
negative.tf#L1
resource "azurerm_network_watcher_flow_log" "negative1" {
    network_watcher_name = azurerm_network_watcher.test.name
    resource_group_name  = azurerm_resource_group.test.name
    network_security_group_id = azurerm_network_security_group.test.id
    storage_account_id        = azurerm_storage_account.test.id
    enabled                   = true
main.tf#L18
resource "azurerm_network_watcher_flow_log" "flow_log" {
  network_watcher_name = local.net_watcher_name
  resource_group_name  = local.resource_group_name

  network_security_group_id = local.network_security_group_id
  storage_account_id        = local.storage_account_id
main.tf#L6
resource "azurerm_network_watcher_flow_log" "module_flow_log" {
  network_watcher_name = resourcename  # azurerm_network_watcher.test.name
  resource_group_name  = resourcegroup # azurerm_resource_group.test.name

  network_security_group_id = azurerm_network_security_group.test.id
  storage_account_id        = azurerm_storage_account.test.id
flow_logs.tf#L2
resource "azurerm_network_watcher_flow_log" "flow" {
  count = try(var.settings, {}) == {} ? 0 : 1

  network_watcher_name = try(var.network_watchers[var.settings.network_watcher_key].name, format("NetworkWatcher_%s", var.resource_location))
  resource_group_name  = try(var.network_watchers[var.settings.network_watcher_rg_key].resource_group_name, "NetworkWatcherRG")
  version              = try(var.settings.version, 2)

Review your Terraform file for Azure best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Security Best Practices for azurerm_network_watcher_flow_log

There is 1 setting in azurerm_network_watcher_flow_log that should be taken care of for security reasons. The following section explain an overview and example code.

risk-label

Ensure to enable Retention policy for flow logs and set it to enough duration

It is better to enable a retention policy for flow logs. Flow logs show us all network activity in the cloud environment and support us when we face critical incidents.

Review your Azure Network settings

You can check if the azurerm_network_watcher_flow_log setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

Explanation in Terraform Registry

Manages a Network Watcher Flow Log.

Tips: Best Practices for The Other Azure Network Resources

In addition to the azurerm_network_security_group, Azure Network has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

azurerm_network_security_group

Ensure to disable RDP port from the Internet

It is better to disable the RDP port from the Internet. RDP access should not be accepted from the Internet (*, 0.0.0.0, /0, internet, any), and consider using the Azure Bastion Service.

risk-label

azurerm_network_security_rule

Ensure to set a more restrictive CIDR range for ingress from the internet

It is better to set a more restrictive CIDR range not to use very broad subnets. If possible, segments should be divided into smaller subnets.

Review your Azure Network settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Microsoft.Network/networkWatchers (Azure Resource Manager)

The networkWatchers in Microsoft.Network can be configured in Azure Resource Manager with the resource name Microsoft.Network/networkWatchers. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

network_watcher.json#L10
        "type": "Microsoft.Network/networkWatchers"
    },
    {
        "etag": "W/\"1999291d-91bc-41e2-9705-74be3187e118\"",
        "id": "/subscriptions/510f92e0-3fcf-4b8f-8a23-095d37e6a299/resourceGroups/NetworkWatcherRG/providers/Microsoft.Network/networkWatchers/NetworkWatcher_eastus",
        "location": "eastus",
network_watcher.json#L10
        "type": "Microsoft.Network/networkWatchers"
    },
    {
        "etag": "W/\"1999291d-91bc-41e2-9705-74be3187e118\"",
        "id": "/subscriptions/510f92e0-3fcf-4b8f-8a23-095d37e6a299/resourceGroups/NetworkWatcherRG/providers/Microsoft.Network/networkWatchers/NetworkWatcher_eastus",
        "location": "eastus",
policy.json#L20
     "type": "Microsoft.Network/networkWatchers",
     "resourceGroupName": "networkWatcherRG",
     "existenceCondition": {
      "field": "location",
      "equals": "[field('location')]"
     },
Microsoft.Authorization_policyDefinitions-Deploy-Nsg-FlowLogs.parameters.json
{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "input": {
Microsoft.Network_networkWatchers-NetworkWatcher_westeurope.parameters.json#L23
        "Type": "Microsoft.Network/networkWatchers",
        "ResourceType": "Microsoft.Network/networkWatchers",
        "ExtensionResourceType": null,
        "Sku": null,
        "Tags": null,
        "SubscriptionId": "76c9af94-ff5d-4bbe-8e7b-228db3bd1804",
Microsoft.Network_networkWatchers-NetworkWatcher_westeurope.parameters.json#L23
        "Type": "Microsoft.Network/networkWatchers",
        "ResourceType": "Microsoft.Network/networkWatchers",
        "ExtensionResourceType": null,
        "Sku": null,
        "Tags": null,
        "SubscriptionId": "d6cf79f8-681b-4d3e-82d3-dd5cc7c83604",
Microsoft.Network_networkWatchers-NetworkWatcher_westeurope.parameters.json#L23
        "Type": "Microsoft.Network/networkWatchers",
        "ResourceType": "Microsoft.Network/networkWatchers",
        "ExtensionResourceType": null,
        "Sku": null,
        "Tags": null,
        "SubscriptionId": "67f16219-3421-4cef-89b9-c06cf90d7bea",
Microsoft.Network_networkWatchers-NetworkWatcher_germanywestcentral.parameters.json#L23
        "Type": "Microsoft.Network/networkWatchers",
        "ResourceType": "Microsoft.Network/networkWatchers",
        "ExtensionResourceType": null,
        "Sku": null,
        "Tags": null,
        "SubscriptionId": "d6cf79f8-681b-4d3e-82d3-dd5cc7c83604",
Deploy-if-not-exists-DNS.json#L18
                    "type": "Microsoft.Network/networkWatchers",
                    "resourceGroupName": "networkWatcherRG",
                    "existenceCondition": {
                        "field": "location",
                        "equals": "[field('location')]"
                    },
Microsoft.Authorization_policyDefinitions-Deploy-Nsg-FlowLogs.parameters.json
{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "input": {

Parameters

  • name required - string
  • type required - string
  • apiVersion required - string
  • location required - string

    Resource location.

  • tags optional - string

    Resource tags.

  • properties required

    Frequently asked questions

    What is Azure Network Watcher Flow Log?

    Azure Network Watcher Flow Log is a resource for Network of Microsoft Azure. Settings can be wrote in Terraform.

    Where can I find the example code for the Azure Network Watcher Flow Log?

    For Terraform, the snyk-labs/infrastructure-as-code-goof, mgugger/terraform and leonidweinbergcx/mykics source code examples are useful. See the Terraform Example section for further details.

    For Azure Resource Manager, the kbroughton/azure_cis_scanner, kbroughton/azure_cis_scanner and manoj-murali/ARMTemplates source code examples are useful. See the Azure Resource Manager Example section for further details.