Azure Network Peering

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

azurerm_virtual_network_peering (Terraform)

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

Example Usage from GitHub

main.tf#L6
resource "azurerm_virtual_network_peering" "server_to_transit" {
  name                        = "server_to_transit"
  resource_group_name         = var.rg.name
  virtual_network_name        = var.server_vnet.name
  remote_virtual_network_id   = var.transit_vnet.id
}
main.tf#L109
resource "azurerm_virtual_network_peering" "aks1_to_aks2" {
  name                      = "aks1_to_aks2"
  resource_group_name       = azurerm_resource_group.rg.name
  virtual_network_name      = azurerm_virtual_network.vnet1.name
  remote_virtual_network_id = azurerm_virtual_network.vnet2.id
  allow_virtual_network_access = true
peering.tf#L1
resource "azurerm_virtual_network_peering" "hub_ondo-p-use-app01-vnet" {
  name                      = "hub_ondo-p-use-app01-vnet"
  resource_group_name       = "ondo-p-use-hub-rg"
  virtual_network_name      = azurerm_virtual_network.ondo-p-use-hub-vnet.name
  remote_virtual_network_id = azurerm_virtual_network.ondo-p-use-app01-vnet.id
}
peering.tf#L3
resource "azurerm_virtual_network_peering" "infra2hub-peer" {
  name                      = "infra2hub"
  resource_group_name       = azurerm_resource_group.spoke2-vnet-rg.name
  virtual_network_name      = azurerm_virtual_network.infra-vnet.name
  remote_virtual_network_id = azurerm_virtual_network.hub-vnet.id
}
sandbox.tf#L103
resource "azurerm_virtual_network_peering" "hub-spoke1" {
  depends_on = [azurerm_virtual_network_gateway.network_gateway]

  name                      = "hub-to-spoke1"
  resource_group_name       = azurerm_resource_group.resource_group.name
  virtual_network_name      = azurerm_virtual_network.virtual_network_hub.name
peerings.tf#L1
resource "azurerm_virtual_network_peering" "hub-spoke1" {
  name                      = "hub_to_spoke1"
  resource_group_name       = azurerm_resource_group.test.name
  virtual_network_name      = azurerm_virtual_network.hub.name
  remote_virtual_network_id = azurerm_virtual_network.spoke1.id

vnet_peering.tf#L11
resource "azurerm_virtual_network_peering" "ansible_staging_peering" {
  name                      = var.peering_name_staging
  resource_group_name       = azurerm_resource_group.rg.name
  virtual_network_name      = azurerm_virtual_network.vnet.name
  remote_virtual_network_id = data.azurerm_virtual_network.staging_vnet.id
}
6.vnet_peerings.tf#L1
 resource "azurerm_virtual_network_peering" "vnet-hub-to-vnet-global-peering" {
   name                      = "vnet-hub-to-vnet-global-peering"
   resource_group_name       = azurerm_resource_group.hub-net-rg.name
   virtual_network_name      = azurerm_virtual_network.vnet-hub.name
   remote_virtual_network_id = azurerm_virtual_network.vnet-global.id
   allow_forwarded_traffic   = true
main.tf#L23
resource "azurerm_virtual_network_peering" "example-1" {
  name                      = "peer1to2"
  resource_group_name       = azurerm_resource_group.example.name
  virtual_network_name      = azurerm_virtual_network.example1.name
  remote_virtual_network_id = azurerm_virtual_network.example2.id
}
peering.tf#L1
resource "azurerm_virtual_network_peering" "peerHUBtoWUS" {
  name                      = "peerHUBtoWUS"
  resource_group_name       = azurerm_resource_group.example.name
  virtual_network_name      = azurerm_virtual_network.WUS.name
  remote_virtual_network_id = azurerm_virtual_network.HUB.id
  allow_forwarded_traffic   = true

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).

Parameters

Explanation in Terraform Registry

Manages a virtual network peering which allows resources to access other resources in the linked virtual network.

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.

risk-label

azurerm_network_watcher_flow_log

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

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/virtualNetworks/virtualNetworkPeerings (Azure Resource Manager)

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

Example Usage from GitHub

An example could not be found in GitHub.

Parameters

  • name required - string
  • type required - string
  • apiVersion required - string
  • properties required
      • allowVirtualNetworkAccess optional - boolean

        Whether the VMs in the local virtual network space would be able to access the VMs in remote virtual network space.

      • allowForwardedTraffic optional - boolean

        Whether the forwarded traffic from the VMs in the local virtual network will be allowed/disallowed in remote virtual network.

      • allowGatewayTransit optional - boolean

        If gateway links can be used in remote virtual networking to link to this virtual network.

      • useRemoteGateways optional - boolean

        If remote gateways can be used on this virtual network. If the flag is set to true, and allowGatewayTransit on remote peering is also true, virtual network will use gateways of remote virtual network for transit. Only one peering can have this flag set to true. This flag cannot be set if virtual network already has a gateway.

      • remoteVirtualNetwork required
          • id required - string

            Resource ID.

      • remoteAddressSpace optional
          • addressPrefixes required - array

            A list of address blocks reserved for this virtual network in CIDR notation.

      • remoteBgpCommunities optional
          • virtualNetworkCommunity required - string

            The BGP community associated with the virtual network.

      • peeringState optional - string

        The status of the virtual network peering.

Frequently asked questions

What is Azure Network Peering?

Azure Network Peering 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 Peering?

For Terraform, the yz4898/att_mvm, i0rek/aksfed and Xergy/AzMIMDeploy source code examples are useful. See the Terraform Example section for further details.