How do I build an Azure network virtual network peering?
In this guide, we will create two Azure Virtual Networks and establish a peering connection between them using Pulumi in TypeScript. Virtual Network Peering enables you to connect two Azure Virtual Networks seamlessly, allowing resources in both networks to communicate with each other.
Key Points
- Define two Azure Virtual Networks.
- Create a peering connection from the first network to the second.
- Create a peering connection from the second network to the first.
Below is the complete Pulumi program to achieve this:
import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure-native";
// Create the first Virtual Network
const vnet1 = new azure.network.VirtualNetwork("vnet1", {
resourceGroupName: "myResourceGroup",
location: "WestUS",
addressSpace: { addressPrefixes: ["10.0.0.0/16"] },
subnets: [{ name: "default", addressPrefix: "10.0.1.0/24" }],
});
// Create the second Virtual Network
const vnet2 = new azure.network.VirtualNetwork("vnet2", {
resourceGroupName: "myResourceGroup",
location: "WestUS",
addressSpace: { addressPrefixes: ["10.1.0.0/16"] },
subnets: [{ name: "default", addressPrefix: "10.1.1.0/24" }],
});
// Create peering from vnet1 to vnet2
const vnet1ToVnet2Peering = new azure.network.VirtualNetworkPeering("vnet1ToVnet2Peering", {
resourceGroupName: "myResourceGroup",
virtualNetworkName: vnet1.name,
remoteVirtualNetwork: { id: vnet2.id },
allowVirtualNetworkAccess: true,
allowForwardedTraffic: true,
allowGatewayTransit: false,
useRemoteGateways: false,
});
// Create peering from vnet2 to vnet1
const vnet2ToVnet1Peering = new azure.network.VirtualNetworkPeering("vnet2ToVnet1Peering", {
resourceGroupName: "myResourceGroup",
virtualNetworkName: vnet2.name,
remoteVirtualNetwork: { id: vnet1.id },
allowVirtualNetworkAccess: true,
allowForwardedTraffic: true,
allowGatewayTransit: false,
useRemoteGateways: false,
});
Summary
In this program, we created two Azure Virtual Networks (vnet1
and vnet2
). We then established a peering connection from vnet1
to vnet2
and another from vnet2
to vnet1
. This setup allows resources in both virtual networks to communicate with each other seamlessly.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.