Skip to content
7 min read Updated

Breaking the Loop: Solving Circular Dependencies in Azure Firewall Routing

Azure Firewall forced tunneling in Terraform without cycle errors, and why a plain 0.0.0.0/0 route instantly breaks your Windows VMs.


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.

You are building an Azure Hub & Spoke network. VNet peerings: done. Subnets: defined. An Azure Firewall sitting in the Hub, ready to inspect every packet leaving your environment.

You add a Route Table to force all internet-bound traffic (0.0.0.0/0) from the Spokes into the Firewall. You run terraform plan.

Error: Cycle: azurerm_subnet_route_table_association.spoke_binding,
azurerm_route_table.spoke_udr, azurerm_firewall.fw ...

Terraform can’t order the resources, so nothing gets built. Below is why the cycle happens, how to break it, and why fixing it is only half the job: a bare 0.0.0.0/0 route then silently breaks Windows activation and Microsoft Entra ID traffic.

Get the basic Cycle-Error fix free on GitHub

The Anatomy of a Cycle Error

To understand the fix, you need to understand how Terraform builds its dependency graph for Azure routing.

When you configure Forced Tunneling, you tell a Spoke Subnet to send traffic to the Azure Firewall’s private IP. Terraform interprets this as follows:

  1. The Route Table needs the Firewall’s private_ip_address for next_hop_in_ip_address
  2. The Firewall needs AzureFirewallSubnet to exist first so it can attach to it
  3. The Subnet Association tries to bind the Route Table to Spoke Subnets simultaneously

Those edges alone are fine. The cycle appears when something adds an edge pointing back, typically an explicit depends_on from the Firewall (or one of its prerequisites) to the spoke route table association. Terraform then can’t decide whether the Firewall must exist before the Route Table or the Subnet before the Firewall, and it stops.

The fix is correct resource ordering. By referencing azurerm_firewall.fw.ip_configuration[0].private_ip_address in the Route Table resource, Terraform can unambiguously resolve the graph:

Firewall → (has IP) → Route Table → (references IP) → Subnet Association

Terraform derives the order from that reference, and no extra depends_on is needed.

The PaaS Trap: Why 0.0.0.0/0 Breaks Your Environment

Many engineers fix the cycle error, force all 0.0.0.0/0 traffic to the firewall, and move on.

Then new Windows Server VMs in the spokes fail to activate, and existing ones fail their next KMS renewal. Workloads that talk to Microsoft Entra ID or Azure PaaS endpoints start timing out.

Routing all outbound traffic to a freshly deployed Azure Firewall with no rules for these destinations cuts the spokes off from the Azure platform services they depend on.

The Windows KMS Failure

Azure Windows VMs activate against Azure’s own KMS servers on TCP 1688, and Microsoft documents that the activation request must come from an Azure public IP address. With forced tunneling, the request either egresses from somewhere KMS doesn’t accept (an on-premises edge, an NVA without the right outbound path) or is dropped by a firewall that has no rule for it. Activation fails without an obvious error.

The Entra ID Blackout

Anything that authenticates against Microsoft Entra ID (formerly Azure AD) over the network needs to reach its endpoints: service principals using client secrets or certificates, Entra ID sign-in for VMs, agents that fetch tokens from login.microsoftonline.com. Forced through a firewall with no rule for that traffic, authentication fails. Managed identity token requests go to the Instance Metadata Service at 169.254.169.254, which UDRs don’t affect, but the Key Vault or Storage calls made with those tokens still go out through the firewall and fail just the same if it doesn’t allow them.

Step 1: Injecting the PaaS Survival Routes

The Route Table needs explicit bypass routes before it is attached to any subnet. General internet traffic goes to the Firewall; KMS and Entra ID traffic goes straight to the Azure backbone.

next_hop_type = "Internet" for Azure-owned IP ranges does not mean the public internet. Traffic to Azure service public IPs with that next hop stays on Microsoft’s backbone network.

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

  # 1. Forced Tunneling: 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
  }

  # 2. KMS Bypass - required for Windows VM activation
  # azkms.core.windows.net / kms.core.windows.net (Azure Global), per Microsoft docs
  route {
    name           = "bypass-azure-kms-1"
    address_prefix = "20.118.99.224/32"
    next_hop_type  = "Internet"
  }

  route {
    name           = "bypass-azure-kms-2"
    address_prefix = "40.83.235.53/32"
    next_hop_type  = "Internet"
  }

  # 3. Entra ID (Azure AD) Bypass - keeps token endpoints reachable
  route {
    name           = "bypass-azure-ad"
    address_prefix = "AzureActiveDirectory"
    next_hop_type  = "Internet"
  }
}

The two KMS addresses come from Microsoft’s KMS activation with forced tunneling article, which recommends exactly these /32 routes with next hop Internet. Many older guides still list 23.102.135.246. Microsoft retired that address in March 2023, and kms.core.windows.net now resolves to 20.118.99.224, so remove the old route if you have it. Sovereign clouds use different addresses.

Step 2: Dynamic Subnet Binding at Scale

The module binds the Route Table to its own self-contained spoke subnet by default. If you peer in additional external spokes instead of using the built-in one, don’t duplicate the association resource for each. Bind them with for_each over a variable map:

resource "azurerm_subnet_route_table_association" "spoke_routing" {
  for_each       = var.spoke_subnet_ids
  subnet_id      = each.value
  route_table_id = azurerm_route_table.spoke_udr.id
}

Pass your Spoke subnet IDs as a map variable and every subnet is automatically bound in a single resource block. Add a new spoke to the map and run terraform apply. No copy-pasting, no drift.

Step 3: Firewall Policies That Actually Scale

Hardcoded IP addresses in Firewall network rules turn into a maintenance burden fast. Azure IP Groups fix this: populate them from Terraform variables and reference the group ID in every rule:

resource "azurerm_ip_group" "spokes" {
  name                = "ipg-spokes-${var.environment}"
  location            = var.location
  resource_group_name = var.resource_group_name
  cidrs               = var.spoke_subnet_cidr
}

When a new Spoke CIDR is added to var.spoke_subnet_cidr, every firewall rule that references azurerm_ip_group.spokes.id automatically inherits it. No rule editing required.

The Result

Getting Azure Firewall routing right in Terraform means solving a graph dependency problem and handling Azure platform dependencies that are documented but easy to miss, and that often surface only days after deployment.

With correct resource ordering, explicit PaaS bypass routes, dynamic subnet binding, and IP Group-based policies, you end up with infrastructure that:

  • Deploys without cycle errors
  • Keeps Windows VMs activating against Azure KMS
  • Keeps Entra ID authentication working
  • Scales to new Spokes by changing a variable, not rewriting rules

The repository at the top covers everything in this article (cycle error fix, KMS and Entra ID bypasses, the baseline Route Table structure, dynamic IP Groups, and FQDN baseline policies), free and MIT licensed.

If the routing/NAT concepts here are the unfamiliar part rather than just the Terraform syntax, Netzwerke: Verstehen, Einrichten, Administrieren* is the German-language reference I keep going back to.

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.