You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Customer intent: I want to connect two virtual networks so that virtual machines in one virtual network can communicate with virtual machines in the other virtual network.
11
19
---
12
20
13
-
# Tutorial: Connect virtual networks with virtual network peering using the Azure portal
21
+
# Tutorial: Connect virtual networks with virtual network peering
14
22
15
23
You can connect virtual networks to each other with virtual network peering. These virtual networks can be in the same region or different regions (also known as global virtual network peering). Once virtual networks are peered, resources in both virtual networks can communicate with each other over a low-latency, high-bandwidth connection using Microsoft backbone network.
16
24
@@ -26,11 +34,29 @@ In this tutorial, you learn how to:
26
34
27
35
## Prerequisites
28
36
37
+
### [Portal](#tab/portal)
38
+
39
+
- An Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
40
+
41
+
### [PowerShell](#tab/powershell)
42
+
29
43
- An Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 1.0.0 or later. Run `Get-Module -ListAvailable Az` to find the installed version. If you need to upgrade, see [Install Azure PowerShell module](/powershell/azure/install-azure-powershell). If you're running PowerShell locally, you also need to run `Connect-AzAccount` to create a connection with Azure.
@@ -47,14 +73,173 @@ Repeat the previous steps to create a second virtual network with the following
47
73
| Subnet name |**subnet-1**|
48
74
| Subnet address range |**10.1.0.0/24**|
49
75
76
+
### [PowerShell](#tab/powershell)
77
+
78
+
Before creating a virtual network, you have to create a resource group for the virtual network, and all other resources created in this article. Create a resource group with [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup). The following example creates a resource group named **test-rg** in the **eastus** location.
79
+
80
+
```azurepowershell-interactive
81
+
$resourceGroup = @{
82
+
Name = "test-rg"
83
+
Location = "EastUS2"
84
+
}
85
+
New-AzResourceGroup @resourceGroup
86
+
```
87
+
88
+
Create a virtual network with [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork). The following example creates a virtual network named **vnet-1** with the address prefix **10.0.0.0/16**.
89
+
90
+
```azurepowershell-interactive
91
+
$vnet1 = @{
92
+
ResourceGroupName = "test-rg"
93
+
Location = "EastUS2"
94
+
Name = "vnet-1"
95
+
AddressPrefix = "10.0.0.0/16"
96
+
}
97
+
$virtualNetwork1 = New-AzVirtualNetwork @vnet1
98
+
```
99
+
100
+
Create a subnet configuration with [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig). The following example creates a subnet configuration with a **10.0.0.0/24** address prefix:
Create a subnet configuration for Azure Bastion with [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig). The following example creates a subnet configuration with a **10.0.1.0/24** address prefix:
Write the subnet configuration to the virtual network with [Set-AzVirtualNetwork](/powershell/module/az.network/Set-azVirtualNetwork), which creates the subnet:
123
+
124
+
```azurepowershell-interactive
125
+
$virtualNetwork1 | Set-AzVirtualNetwork
126
+
```
127
+
128
+
### Create Azure Bastion
129
+
130
+
Create a public IP address for the Azure Bastion host with [New-AzPublicIpAddress](/powershell/module/az.network/new-azpublicipaddress). The following example creates a public IP address named *public-ip-bastion* in the *vnet-1* virtual network.
131
+
132
+
```azurepowershell-interactive
133
+
$publicIpParams = @{
134
+
ResourceGroupName = "test-rg"
135
+
Name = "public-ip-bastion"
136
+
Location = "EastUS2"
137
+
AllocationMethod = "Static"
138
+
Sku = "Standard"
139
+
}
140
+
New-AzPublicIpAddress @publicIpParams
141
+
```
142
+
143
+
Create an Azure Bastion host with [New-AzBastion](/powershell/module/az.network/new-azbastion). The following example creates an Azure Bastion host named *bastion* in the *AzureBastionSubnet* subnet of the *vnet-1* virtual network. Azure Bastion is used to securely connect Azure virtual machines without exposing them to the public internet.
144
+
145
+
```azurepowershell-interactive
146
+
$bastionParams = @{
147
+
ResourceGroupName = "test-rg"
148
+
Name = "bastion"
149
+
VirtualNetworkName = "vnet-1"
150
+
PublicIpAddressName = "public-ip-bastion"
151
+
PublicIpAddressRgName = "test-rg"
152
+
VirtualNetworkRgName = "test-rg"
153
+
}
154
+
New-AzBastion @bastionParams -AsJob
155
+
```
156
+
157
+
### Create a second virtual network
158
+
159
+
Create a second virtual network with [New-AzVirtualNetwork](/powershell/module/az.network/new-azvirtualnetwork). The following example creates a virtual network named **vnet-1** with the address prefix **10.1.0.0/16**.
160
+
161
+
>[!NOTE]
162
+
>The second virtual network can be in the same region as the first virtual network or in a different region. You don't need a Bastion deployment for the second virtual network. After the network peer, you can connect to both virtual machines with the same Bastion deployment.
163
+
164
+
```azurepowershell-interactive
165
+
$vnet1 = @{
166
+
ResourceGroupName = "test-rg"
167
+
Location = "EastUS2"
168
+
Name = "vnet-2"
169
+
AddressPrefix = "10.1.0.0/16"
170
+
}
171
+
$virtualNetwork1 = New-AzVirtualNetwork @vnet1
172
+
```
173
+
174
+
Create a subnet configuration with [Add-AzVirtualNetworkSubnetConfig](/powershell/module/az.network/add-azvirtualnetworksubnetconfig). The following example creates a subnet configuration with a **10.1.0.0/24** address prefix:
Create a peering with [Add-AzVirtualNetworkPeering](/powershell/module/az.network/add-azvirtualnetworkpeering). The following example peers **vnet-1** to **vnet-2**.
200
+
201
+
```azurepowershell-interactive
202
+
$peerConfig1 = @{
203
+
Name = "vnet-1-to-vnet-2"
204
+
VirtualNetwork = $virtualNetwork1
205
+
RemoteVirtualNetworkId = $virtualNetwork2.Id
206
+
}
207
+
Add-AzVirtualNetworkPeering @peerConfig1
208
+
```
209
+
210
+
In the output returned after the previous command executes, you see that the **PeeringState** is **Initiated**. The peering remains in the **Initiated** state until you create the peering from **vnet-2** to **vnet-1**. Create a peering from **vnet-2** to **vnet-1**.
211
+
212
+
```azurepowershell-interactive
213
+
$peerConfig2 = @{
214
+
Name = "vnet-2-to-vnet-1"
215
+
VirtualNetwork = $virtualNetwork2
216
+
RemoteVirtualNetworkId = $virtualNetwork1.Id
217
+
}
218
+
Add-AzVirtualNetworkPeering @peerConfig2
219
+
```
220
+
221
+
In the output returned after the previous command executes, you see that the **PeeringState** is **Connected**. Azure also changed the peering state of the **vnet-1-to-vnet-2** peering to **Connected**. Confirm that the peering state for the **vnet-1-to-vnet-2** peering changed to **Connected** with [Get-AzVirtualNetworkPeering](/powershell/module/az.network/get-azvirtualnetworkpeering).
Resources in one virtual network cannot communicate with resources in the other virtual network until the **PeeringState** for the peerings in both virtual networks is **Connected**.
232
+
233
+
### [CLI](#tab/cli)
234
+
235
+
---
236
+
54
237
## Create virtual machines
55
238
56
239
Create a virtual machine in each virtual network to test the communication between them.
Repeat the previous steps to create a second virtual machine in the second virtual network with the following values:
@@ -68,6 +253,44 @@ Repeat the previous steps to create a second virtual machine in the second virtu
68
253
| Public IP |**None**|
69
254
| Network security group name |**nsg-2**|
70
255
256
+
### [PowerShell](#tab/powershell)
257
+
258
+
### Create the first VM
259
+
260
+
Create a VM with [New-AzVM](/powershell/module/az.compute/new-azvm). The following example creates a VM named **vm-1** in the **vnet-1** virtual network. The `-AsJob` option creates the VM in the background, so you can continue to the next step. When prompted, enter the user name and password for the virtual machine.
261
+
262
+
```azurepowershell-interactive
263
+
$vm1 = @{
264
+
ResourceGroupName = "test-rg"
265
+
Location = "EastUS2"
266
+
VirtualNetworkName = "vnet-1"
267
+
SubnetName = "subnet-1"
268
+
ImageName = "Win2019Datacenter"
269
+
Name = "vm-1"
270
+
}
271
+
New-AzVm @vm1 -AsJob
272
+
```
273
+
274
+
### Create the second VM
275
+
276
+
```azurepowershell-interactive
277
+
$vm2 = @{
278
+
ResourceGroupName = "test-rg"
279
+
Location = "EastUS2"
280
+
VirtualNetworkName = "vnet-2"
281
+
SubnetName = "subnet-1"
282
+
ImageName = "Win2019Datacenter"
283
+
Name = "vm-2"
284
+
}
285
+
New-AzVm @vm2
286
+
```
287
+
288
+
The VM takes a few minutes to create. Don't continue with the later steps until Azure creates **vm-2** and returns output to PowerShell.
289
+
290
+
### [CLI](#tab/cli)
291
+
292
+
---
293
+
71
294
Wait for the virtual machines to be created before continuing with the next steps.
72
295
73
296
## Connect to a virtual machine
@@ -120,8 +343,16 @@ Use `ping` to test the communication between the virtual machines.
0 commit comments