Skip to content
10 min read Updated

NIS2 Article 21 in Azure: Implementing Network Security Controls with Terraform

How the network security measures of NIS2 Article 21 map to Azure resources, implemented with Terraform. Concrete code, not legal theory.


Disclosure: This post contains Amazon affiliate links (marked with *). As an Amazon Associate I earn from qualifying purchases. Buying through them costs you nothing extra.

Most NIS2 content on the internet is written by lawyers for lawyers. It explains the directive in broad strokes, cites recitals and annexes, and leaves engineers staring at legal text wondering what to build.

This article covers only the network security controls relevant to NIS2 Article 21 and maps each one to concrete Azure resources, managed with Terraform.

Disclaimer: This article covers the technical network infrastructure controls relevant to NIS2 Article 21. Full NIS2 compliance requires organizational measures, incident response procedures, supply chain controls, and additional technical layers beyond what infrastructure code alone can provide. Nothing here constitutes legal advice.

What NIS2 Article 21 Actually Requires

Article 21(1) of Directive (EU) 2022/2555 requires essential and important entities to take “appropriate and proportionate technical, operational and organisational measures” to manage the risks to their network and information systems. Article 21(2) lists ten areas those measures must at least cover, points (a) to (j). The directive doesn’t prescribe specific network controls. For network infrastructure, the measures translate into four engineering areas:

1. Network Segmentation Systems must be isolated based on criticality and function. Unrestricted lateral movement between workloads is not acceptable.

2. Access Control Access to systems must be strictly controlled. Default-allow is not acceptable, at either the network layer or the identity layer.

3. Minimizing Attack Surface Public exposure of internal systems must be eliminated where possible. Every publicly reachable endpoint that doesn’t need to be public is a finding.

4. Monitoring and Auditability Infrastructure must be auditable. Changes must be traceable. Ad-hoc ClickOps does not produce an audit trail.

A well-built Azure Hub & Spoke environment managed with Terraform addresses all four, if you implement it correctly.

Mapping NIS2 Requirements to Azure Architecture

NIS2 Article 21 RequirementAzure Control
Network SegmentationHub & Spoke VNet topology
NSG with Default-Deny
Access Control (Network)Azure Firewall + Forced Tunneling
Private Endpoints (no public access)
Access Control (Identity)Managed Identities + RBAC
local_auth_enabled = false
Minimize Attack Surfacepublic_network_access_enabled = false
Private DNS Zones
AuditabilityTerraform IaC (version-controlled)
Azure Policy compliance reporting

How those controls line up with the actual points of Article 21(2):

Article 21(2) pointDirective wording (short)Controls in this article
(a)Risk analysis and information system security policiesSupports it; the policy itself is organizational
(b)Incident handlingFirewall and NSG logs as detection and investigation data
(c)Business continuity, backup, disaster recoveryNot covered here
(d)Supply chain securityNot covered here
(e)Security in acquisition, development and maintenanceVersion-controlled, reviewed IaC changes
(f)Assessing effectiveness of risk-management measuresAzure Policy compliance reporting
(g)Cyber hygiene and trainingNot covered here
(h)Cryptography and encryptionTLS to Private Endpoints; key management is out of scope
(i)HR security, access control policies, asset managementSegmentation, Default-Deny NSGs, RBAC, Terraform state as inventory
(j)MFA, secured communicationsEntra ID side, not covered here

Control 1: Network Segmentation via Hub & Spoke

NIS2 requires that critical systems are isolated from each other. A flat VNet where all workloads can communicate freely fails this requirement immediately.

The Hub & Spoke topology enforces segmentation by design. Spokes cannot reach each other directly; all traffic routes through the Hub:

# Hub VNet - centralized shared services
resource "azurerm_virtual_network" "hub" {
  name          = "vnet-hub-${var.environment}"
  address_space = var.hub_vnet_cidr
}

# Spoke VNets - isolated workload environments
resource "azurerm_virtual_network" "spoke1" {
  name          = "vnet-spoke-01-${var.environment}"
  address_space = var.spoke1_vnet_cidr
}

# Bidirectional peering with forwarded traffic
resource "azurerm_virtual_network_peering" "hub_to_spoke1" {
  name                      = "peer-hub-to-spoke1"
  virtual_network_name      = azurerm_virtual_network.hub.name
  remote_virtual_network_id = azurerm_virtual_network.spoke1.id
  allow_forwarded_traffic   = true
}

Spoke-to-Spoke traffic is blocked by default because VNet peering is not transitive: each Spoke peers with the Hub, not with the other Spokes. Any cross-Spoke communication must be explicitly routed through the Hub and inspected by the Azure Firewall.

Control 2: Default-Deny at the Subnet Level

Network segmentation without enforcement at the subnet level is incomplete. An NSG with an explicit deny rule on every Spoke subnet keeps unexpected internet traffic away from workloads. Azure’s built-in DenyAllInBound rule at priority 65500 already does this implicitly; the explicit rule makes the intent visible in code and in audit exports:

resource "azurerm_network_security_group" "zero_trust" {
  name                = "nsg-zero-trust-${var.environment}"
  location            = var.location
  resource_group_name = var.resource_group_name

  # Allow internal VNet traffic (peering)
  security_rule {
    name                       = "Allow-VNet-Inbound"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "*"
    source_address_prefix      = "VirtualNetwork"
    destination_address_prefix = "VirtualNetwork"
    source_port_range          = "*"
    destination_port_range     = "*"
  }

  # Block all internet traffic - explicit deny
  security_rule {
    name                       = "Deny-Internet-Inbound"
    priority                   = 4096
    direction                  = "Inbound"
    access                     = "Deny"
    protocol                   = "*"
    source_address_prefix      = "Internet"
    destination_address_prefix = "*"
    source_port_range          = "*"
    destination_port_range     = "*"
  }
}

# Bind immediately - an unbound NSG enforces nothing
resource "azurerm_subnet_network_security_group_association" "spoke1_nsg" {
  subnet_id                 = azurerm_subnet.spoke1_default.id
  network_security_group_id = azurerm_network_security_group.zero_trust.id
}

The priority gap (100 to 4096) leaves room for application-specific rules without touching the baseline, so your security team can add rules for specific ports and protocols without restructuring the NSG.

Control 3: Egress Control via Azure Firewall

Blocking inbound traffic is necessary but not sufficient. Controlling what leaves your network matters just as much: it is how you detect data exfiltration, block C2 callbacks, and enforce least-privilege egress.

Azure Firewall with Forced Tunneling routes all outbound Spoke traffic through a central inspection point:

resource "azurerm_route_table" "spoke_udr" {
  name                          = "rt-spoke-forced-tunneling-${var.environment}"
  location                      = var.location
  resource_group_name           = var.resource_group_name
  bgp_route_propagation_enabled = false

  # All internet traffic through Azure Firewall
  route {
    name                   = "route-to-firewall"
    address_prefix         = "0.0.0.0/0"
    next_hop_type          = "VirtualAppliance"
    next_hop_in_ip_address = azurerm_firewall.fw.ip_configuration[0].private_ip_address
  }

  # KMS bypass - required for Windows VM activation
  route {
    name           = "bypass-azure-kms"
    address_prefix = "23.102.135.246/32"
    next_hop_type  = "Internet"
  }

  # Azure AD bypass - prevents Managed Identity lockouts
  route {
    name           = "bypass-azure-ad"
    address_prefix = "AzureActiveDirectory"
    next_hop_type  = "Internet"
  }
}

The Firewall then enforces FQDN-based Application Rules: only explicitly allowed destinations can be reached, and everything else is dropped and logged. Those logs feed incident handling under Article 21(2)(b).

The KMS route above covers the classic kms.core.windows.net address. Microsoft has since added newer KMS endpoints (azkms.core.windows.net), so check the current list in the Azure Windows activation docs before relying on a single /32.

Control 4: Eliminating Public PaaS Endpoints

Every PaaS service deployed with a public endpoint is attack surface. For Azure PaaS services, minimizing exposure means Private Endpoints with public access explicitly disabled:

resource "azurerm_key_vault" "kv" {
  name                          = "kv-${var.environment}-${random_string.suffix.result}"
  public_network_access_enabled = false  # No public endpoint
  enable_rbac_authorization     = true   # No access policies - RBAC only

  network_acls {
    default_action = "Deny"
    bypass         = "AzureServices"
  }
}

resource "azurerm_private_endpoint" "kv_pe" {
  name      = "pe-kv-${var.environment}"
  subnet_id = azurerm_subnet.endpoints.id

  private_service_connection {
    name                           = "psc-kv"
    private_connection_resource_id = azurerm_key_vault.kv.id
    subresource_names              = ["vault"]
    is_manual_connection           = false
  }

  private_dns_zone_group {
    name                 = "default"
    private_dns_zone_ids = [azurerm_private_dns_zone.kv.id]
  }
}

From the internet, the vault’s public FQDN still resolves, but with public network access disabled the service rejects every request that doesn’t arrive through the Private Endpoint. Inside the VNet, the Private DNS zone resolves the same name to the endpoint’s private IP. This is access control at the network layer, which falls under Article 21(2)(i).

Control 5: Identity Without Static Credentials

Access keys and connection strings are an audit finding under any serious compliance framework. Article 21(2)(i) requires access control policies, and in Azure the cleanest way to meet that is identity issued by the platform, not shared secrets.

The Azure pattern: Managed Identities with scoped RBAC assignments, local authentication disabled:

resource "azurerm_linux_function_app" "app" {
  # ...
  identity {
    type = "SystemAssigned"
  }
}

# Least-privilege RBAC - only what the app needs
resource "azurerm_role_assignment" "app_kv_secrets" {
  scope                = azurerm_key_vault.kv.id
  role_definition_name = "Key Vault Secrets User"
  principal_id         = azurerm_linux_function_app.app.identity[0].principal_id
}

When the resource is deleted, its system-assigned identity is deleted with it. Azure leaves the role assignments behind as orphaned “Identity not found” entries, but Terraform removes them in the same destroy when they live in the same configuration. There are no credentials for you to rotate and no keys to leak into Git history.

Control 6: Auditability via Infrastructure as Code

Article 21(2)(e) covers security in the maintenance of network and information systems, which in practice means changes must be traceable. This is where Terraform adds compliance value beyond automation.

Every resource in this architecture is:

  • Version-controlled: every change is a Git commit with author, timestamp, and diff
  • Reviewable: pull requests enforce four-eyes review before changes reach production
  • Reproducible: the same code and inputs produce the same infrastructure, recorded in state

A terraform plan output is an audit artifact. It shows exactly what will change and what the current state is. No ClickOps, no undocumented manual changes, no “someone did something in the Portal last Tuesday.”

What This Covers and What It Doesn’t

This architecture addresses the technical network security controls of NIS2 Article 21. For full compliance, your organization also needs:

  • Incident handling procedures (b): documented playbooks for security events, plus the reporting obligations in Article 23
  • Supply chain security (d): vendor assessments, third-party controls
  • Business continuity (c): backup, recovery, resilience testing
  • Organizational measures (a, f, g): policies, effectiveness reviews, cyber hygiene and training, governance

Infrastructure code is the foundation, not the whole building.

The modules that implement this architecture (Hub & Spoke segmentation, Default-Deny NSGs, forced-tunneled egress through Azure Firewall, Private Endpoints for every data-plane service, Managed Identity instead of static credentials) are available on the templates page. Each one is tested against these controls and documented for audit purposes.

If the network-segmentation concepts behind Article 21 are the unfamiliar part, Netzwerke: Verstehen, Einrichten, Administrieren* is the reference I still reach for.

Was this helpful?
Share
DW

David Woitzik

Hybrid Cloud Engineer

I work mostly with Azure, Terraform, and Zero-Trust network design, and publish the Terraform templates and write-ups I wanted when I was solving the same problems.

Keep reading

More like this in your inbox

New Terraform modules and articles by email. No spam.