infrastructure / networking
Cross-Region Private Link: Reaching an Isolated Database with Terraform
Terraform • Azure Private Link • VNet Peering • Firewall Diagnostics

The problem

A serverless analytics platform needed to query a database that lived behind a corporate firewall, reachable only through an existing on-premises network tunnel. The platform's compute ran in one Azure region; the firewall and target network lived in another. Azure Private Link Service in "Direct Connect" mode — the feature designed for exactly this kind of private, cross-network access — had one hard requirement at the time: the private endpoint, the Private Link Service, and the destination all had to sit in the same Azure region.

They didn't. The fix wasn't a settings change — it meant building new infrastructure in the platform's home region and bridging it back to the existing network entirely with code.

Goal: give the analytics platform a private, same-region on-ramp, then quietly hand traffic off across a VNet peering link to infrastructure that already existed hundreds of miles away.

Architecture

Analytics Platform (Region A) | v Managed Private Endpoint | v Private Link Service (Direct Connect, Region A) | v VNet Peering (Region A <-> Region B) | v User-Defined Route -> On-Prem Firewall Appliance | v Firewall Policy -> Site-to-Site Tunnel | v Target Database (on the far side of the tunnel)

Building it with Terraform

Everything on the Azure side — the new resource group, a small transit VNet, a dedicated subnet, the Private Link Service itself, and the routing to get traffic to the firewall — was defined as code, with the existing production network only ever read, never modified, so the new infrastructure stayed additive.

terraform {
  required_version = ">= 1.0.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.100"
    }
    azapi = {
      source  = "azure/azapi"
      version = "~> 1.13"
    }
  }
}

provider "azurerm" {
  features {}
}

provider "azapi" {}

# --- Read the existing production network; never modify it directly ---
data "azurerm_resource_group" "network_rg" {
  name = var.existing_network_resource_group_name
}

data "azurerm_virtual_network" "prod_vnet" {
  name                = var.existing_vnet_name
  resource_group_name = data.azurerm_resource_group.network_rg.name
}

# --- New transit infrastructure, built fresh in the platform's home region ---
resource "azurerm_resource_group" "transit_rg" {
  name     = "rg-analytics-transit"
  location = var.transit_region
}

resource "azurerm_virtual_network" "transit_vnet" {
  name                = "vnet-analytics-transit"
  location            = azurerm_resource_group.transit_rg.location
  resource_group_name = azurerm_resource_group.transit_rg.name
  address_space       = ["10.90.0.0/24"]
}

resource "azurerm_subnet" "transit_pls_subnet" {
  name                 = "snet-pls-transit"
  resource_group_name  = azurerm_resource_group.transit_rg.name
  virtual_network_name = azurerm_virtual_network.transit_vnet.name
  address_prefixes     = ["10.90.0.0/26"]

  # Required: Private Link Service manages its own NAT IPs in this subnet
  private_link_service_network_policies_enabled = false
}

# --- Bidirectional peering back to the existing production network ---
resource "azurerm_virtual_network_peering" "prod_to_transit" {
  name                         = "peer-prod-to-transit"
  resource_group_name          = data.azurerm_resource_group.network_rg.name
  virtual_network_name         = data.azurerm_virtual_network.prod_vnet.name
  remote_virtual_network_id    = azurerm_virtual_network.transit_vnet.id
  allow_virtual_network_access = true
  allow_forwarded_traffic      = true
}

resource "azurerm_virtual_network_peering" "transit_to_prod" {
  name                         = "peer-transit-to-prod"
  resource_group_name          = azurerm_resource_group.transit_rg.name
  virtual_network_name         = azurerm_virtual_network.transit_vnet.name
  remote_virtual_network_id    = data.azurerm_virtual_network.prod_vnet.id
  allow_virtual_network_access = true
  allow_forwarded_traffic      = true
}

The Private Link Service itself needed a property — destinationIPAddress — that the standard Terraform provider didn't yet expose for this preview feature. Rather than wait on provider support, the fix was dropping to Azure's raw ARM API directly through the azapi provider:

# A preview-only ARM property (destinationIPAddress) wasn't yet exposed by the
# standard provider, so this resource talks to the ARM API directly instead.
resource "azapi_resource" "transit_pls" {
  type      = "Microsoft.Network/privateLinkServices@2023-09-01"
  name      = "pls-transit-database"
  location  = azurerm_resource_group.transit_rg.location
  parent_id = azurerm_resource_group.transit_rg.id

  # The provider's client-side schema hadn't caught up to this preview
  # property yet; this lets the raw request reach ARM unmodified.
  schema_validation_enabled = false

  body = jsonencode({
    properties = {
      destinationIPAddress = var.database_private_ip
      enableProxyProtocol  = false
      ipConfigurations = [
        {
          name = "pls-nat-primary"
          properties = {
            primary                 = true
            privateIPAddressVersion = "IPv4"
            subnet = { id = azurerm_subnet.transit_pls_subnet.id }
          }
        },
        {
          name = "pls-nat-secondary"
          properties = {
            primary                 = false
            privateIPAddressVersion = "IPv4"
            subnet = { id = azurerm_subnet.transit_pls_subnet.id }
          }
        }
      ]
    }
  })
}

Two NAT IP configurations were a hard platform requirement for this mode — one short of that and the deployment rejected itself outright. The last piece was a route table forcing traffic for the target network out toward the firewall appliance, rather than letting it fall through to a default route with nowhere useful to go:

resource "azurerm_route_table" "transit_udr" {
  name                = "rt-transit-to-firewall"
  location            = azurerm_resource_group.transit_rg.location
  resource_group_name = azurerm_resource_group.transit_rg.name
}

resource "azurerm_route" "to_target_network" {
  name                   = "to-target-via-firewall"
  resource_group_name    = azurerm_resource_group.transit_rg.name
  route_table_name       = azurerm_route_table.transit_udr.name
  address_prefix         = "10.20.0.0/16"
  next_hop_type          = "VirtualAppliance"
  next_hop_in_ip_address = var.firewall_private_ip
}

resource "azurerm_subnet_route_table_association" "transit_udr_assoc" {
  subnet_id      = azurerm_subnet.transit_pls_subnet.id
  route_table_id = azurerm_route_table.transit_udr.id
}

What actually broke, and how it got found

Terraform applied cleanly on the first real attempt — the infrastructure came up exactly as designed. The connection still didn't work, which turned the project from an infrastructure build into a packet-level investigation.

What the traces showed

Packet captures on the firewall confirmed outbound traffic was leaving the new transit network correctly, hitting the right policy, and reaching the destination network — but nothing ever came back.

The actual cause

The firewall had never been told how to route a reply back toward the new transit subnet. Every other network it talked to had a return path configured months earlier; this one, freshly built, simply didn't exist yet.

Once that return path was added, a live connection attempt from the analytics platform succeeded end to end on the very next test — the same infrastructure that had been standing idle for days worked immediately the moment both directions of the path existed.

What this proved out

  • Azure Private Link's region requirement can be worked around entirely with infrastructure-as-code, without touching the destination side at all.
  • Reading an existing network as a Terraform data source — rather than importing or managing it — kept the whole build additive and low-risk against production.
  • When a cloud provider's own resource definitions haven't caught up to a preview feature yet, dropping to the raw ARM API through a provider like azapi is a legitimate, well-supported pattern, not a workaround to avoid.
  • A fully correct cloud build can still fail for a one-line reason sitting entirely outside the cloud — methodical, layer-by-layer packet tracing on the firewall is what actually found it.