Skip to content

Commit 73a29bf

Browse files
committed
started consolidation of articles
1 parent 9dfe720 commit 73a29bf

File tree

1 file changed

+238
-7
lines changed

1 file changed

+238
-7
lines changed

articles/virtual-network/tutorial-connect-virtual-networks-portal.md

Lines changed: 238 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
---
2-
title: 'Tutorial: Connect virtual networks with VNet peering - Azure portal'
3-
description: In this tutorial, you learn how to connect virtual networks with virtual network peering using the Azure portal.
2+
title: 'Tutorial: Connect virtual networks with peering'
3+
description: In this tutorial, you learn how to connect virtual networks with virtual network peering.
44
author: asudbring
55
ms.service: azure-virtual-network
66
ms.topic: tutorial
7-
ms.date: 06/17/2024
7+
ms.date: 11/11/2024
88
ms.author: allensu
9-
ms.custom: template-tutorial, linux-related-content
9+
ms.custom:
10+
- template-tutorial
11+
- devx-track-azurecli
12+
- devx-track-azurepowershell
13+
- linux-related-content
14+
content_well_notification:
15+
- AI-contribution
16+
ai-usage: ai-assisted
17+
1018
# 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.
1119
---
1220

13-
# Tutorial: Connect virtual networks with virtual network peering using the Azure portal
21+
# Tutorial: Connect virtual networks with virtual network peering
1422

1523
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.
1624

@@ -26,11 +34,29 @@ In this tutorial, you learn how to:
2634
2735
## Prerequisites
2836

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+
2943
- An Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
3044

31-
## Sign in to Azure
45+
[!INCLUDE [cloud-shell-try-it.md](~/reusable-content/ce-skilling/azure/includes/cloud-shell-try-it.md)]
46+
47+
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.
48+
49+
### [CLI](#tab/cli)
50+
51+
[!INCLUDE [quickstarts-free-trial-note](~/reusable-content/ce-skilling/azure/includes/quickstarts-free-trial-note.md)]
52+
53+
[!INCLUDE [azure-cli-prepare-your-environment-no-header.md](~/reusable-content/azure-cli/azure-cli-prepare-your-environment-no-header.md)]
54+
55+
- This article requires version 2.0.28 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
56+
57+
---
3258

33-
Sign in to the [Azure portal](https://portal.azure.com).
59+
### [Portal](#tab/portal)
3460

3561
[!INCLUDE [virtual-network-create-with-bastion.md](~/reusable-content/ce-skilling/azure/includes/virtual-network-create-with-bastion.md)]
3662

@@ -47,14 +73,173 @@ Repeat the previous steps to create a second virtual network with the following
4773
| Subnet name | **subnet-1** |
4874
| Subnet address range | **10.1.0.0/24** |
4975

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:
101+
102+
```azurepowershell-interactive
103+
$subConfig = @{
104+
Name = "subnet-1"
105+
AddressPrefix = "10.0.0.0/24"
106+
VirtualNetwork = $virtualNetwork1
107+
}
108+
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subConfig
109+
```
110+
111+
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:
112+
113+
```azurepowershell-interactive
114+
$subConfig = @{
115+
Name = "AzureBastionSubnet"
116+
AddressPrefix = "10.0.1.0/24"
117+
VirtualNetwork = $virtualNetwork1
118+
}
119+
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subConfig
120+
```
121+
122+
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:
175+
176+
```azurepowershell-interactive
177+
$subConfig = @{
178+
Name = "subnet-1"
179+
AddressPrefix = "10.1.0.0/24"
180+
VirtualNetwork = $virtualNetwork1
181+
}
182+
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subConfig
183+
```
184+
185+
### [CLI](#tab/cli)
186+
187+
---
188+
189+
### [Portal](#tab/portal)
190+
50191
<a name="peer-virtual-networks"></a>
51192

52193
[!INCLUDE [virtual-network-create-network-peer.md](~/reusable-content/ce-skilling/azure/includes/virtual-network-create-network-peer.md)]
53194

195+
### [PowerShell](#tab/powershell)
196+
197+
## Peer virtual networks
198+
199+
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).
222+
223+
```azurepowershell-interactive
224+
$peeringState = @{
225+
ResourceGroupName = "test-rg"
226+
VirtualNetworkName = "vnet-1"
227+
}
228+
Get-AzVirtualNetworkPeering @peeringState | Select PeeringState
229+
```
230+
231+
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+
54237
## Create virtual machines
55238

56239
Create a virtual machine in each virtual network to test the communication between them.
57240

241+
### [Portal](#tab/portal)
242+
58243
[!INCLUDE [create-test-virtual-machine-linux.md](~/reusable-content/ce-skilling/azure/includes/create-test-virtual-machine-linux.md)]
59244

60245
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
68253
| Public IP | **None** |
69254
| Network security group name | **nsg-2** |
70255

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+
71294
Wait for the virtual machines to be created before continuing with the next steps.
72295

73296
## Connect to a virtual machine
@@ -120,8 +343,16 @@ Use `ping` to test the communication between the virtual machines.
120343
121344
1. Close the Bastion connection to **vm-2**.
122345
346+
### [Portal](#tab/portal)
347+
123348
[!INCLUDE [portal-clean-up.md](~/reusable-content/ce-skilling/azure/includes/portal-clean-up.md)]
124349
350+
### [PowerShell](#tab/powershell)
351+
352+
### [CLI](#tab/cli)
353+
354+
---
355+
125356
## Next steps
126357
127358
In this tutorial, you:

0 commit comments

Comments
 (0)