Skip to content

Commit a440462

Browse files
authored
Merge pull request #272247 from asudbring/vnet-freshness
Freshness update of vnet peering CLI article
2 parents 267d29e + f5eb881 commit a440462

File tree

2 files changed

+61
-54
lines changed

2 files changed

+61
-54
lines changed

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

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
---
2-
title: Connect virtual networks with VNet peering - Azure CLI
2+
title: Connect virtual networks with virtual network peering - Azure CLI
33
description: In this article, you learn how to connect virtual networks with virtual network peering, using the Azure CLI.
44
services: virtual-network
55
author: asudbring
66
ms.service: virtual-network
7-
ms.devlang: azurecli
87
ms.topic: how-to
9-
ms.tgt_pltfrm: virtual-network
10-
ms.date: 03/13/2018
8+
ms.date: 04/15/2024
119
ms.author: allensu
1210
ms.custom: devx-track-azurecli
1311
# 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.
1412
---
1513

1614
# Connect virtual networks with virtual network peering using the Azure CLI
1715

18-
You can connect virtual networks to each other with virtual network peering. Once virtual networks are peered, resources in both virtual networks are able to communicate with each other, with the same latency and bandwidth as if the resources were in the same virtual network. In this article, you learn how to:
16+
You can connect virtual networks to each other with virtual network peering. Once virtual networks are peered, resources in both virtual networks are able to communicate with each other, with the same latency and bandwidth as if the resources were in the same virtual network.
17+
18+
In this article, you learn how to:
1919

2020
* Create two virtual networks
21+
2122
* Connect two virtual networks with a virtual network peering
23+
2224
* Deploy a virtual machine (VM) into each virtual network
25+
2326
* Communicate between VMs
2427

2528
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
@@ -30,117 +33,119 @@ You can connect virtual networks to each other with virtual network peering. Onc
3033

3134
## Create virtual networks
3235

33-
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 [az group create](/cli/azure/group). The following example creates a resource group named *myResourceGroup* in the *eastus* location.
36+
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 [az group create](/cli/azure/group). The following example creates a resource group named **test-rg** in the **eastus** location.
3437

3538
```azurecli-interactive
36-
az group create --name myResourceGroup --location eastus
39+
az group create \
40+
--name test-rg \
41+
--location eastus
3742
```
3843

39-
Create a virtual network with [az network vnet create](/cli/azure/network/vnet). The following example creates a virtual network named *myVirtualNetwork1* with the address prefix *10.0.0.0/16*.
44+
Create a virtual network with [az network vnet create](/cli/azure/network/vnet#az-network-vnet-create). The following example creates a virtual network named **vnet-1** with the address prefix **10.0.0.0/16**.
4045

4146
```azurecli-interactive
4247
az network vnet create \
43-
--name myVirtualNetwork1 \
44-
--resource-group myResourceGroup \
48+
--name vnet-1 \
49+
--resource-group test-rg \
4550
--address-prefixes 10.0.0.0/16 \
46-
--subnet-name Subnet1 \
51+
--subnet-name subnet-1 \
4752
--subnet-prefix 10.0.0.0/24
4853
```
4954

50-
Create a virtual network named *myVirtualNetwork2* with the address prefix *10.1.0.0/16*:
55+
Create a virtual network named **vnet-2** with the address prefix **10.1.0.0/16**:
5156

5257
```azurecli-interactive
5358
az network vnet create \
54-
--name myVirtualNetwork2 \
55-
--resource-group myResourceGroup \
59+
--name vnet-2 \
60+
--resource-group test-rg \
5661
--address-prefixes 10.1.0.0/16 \
57-
--subnet-name Subnet1 \
62+
--subnet-name subnet-1 \
5863
--subnet-prefix 10.1.0.0/24
5964
```
6065

6166
## Peer virtual networks
6267

63-
Peerings are established between virtual network IDs, so you must first get the ID of each virtual network with [az network vnet show](/cli/azure/network/vnet) and store the ID in a variable.
68+
Peerings are established between virtual network IDs. Obtain the ID of each virtual network with [az network vnet show](/cli/azure/network/vnet#az-network-vnet-show) and store the ID in a variable.
6469

6570
```azurecli-interactive
66-
# Get the id for myVirtualNetwork1.
71+
# Get the id for vnet-1.
6772
vNet1Id=$(az network vnet show \
68-
--resource-group myResourceGroup \
69-
--name myVirtualNetwork1 \
73+
--resource-group test-rg \
74+
--name vnet-1 \
7075
--query id --out tsv)
7176
72-
# Get the id for myVirtualNetwork2.
77+
# Get the id for vnet-2.
7378
vNet2Id=$(az network vnet show \
74-
--resource-group myResourceGroup \
75-
--name myVirtualNetwork2 \
79+
--resource-group test-rg \
80+
--name vnet-2 \
7681
--query id \
7782
--out tsv)
7883
```
7984

80-
Create a peering from *myVirtualNetwork1* to *myVirtualNetwork2* with [az network vnet peering create](/cli/azure/network/vnet/peering). If the `--allow-vnet-access` parameter is not specified, a peering is established, but no communication can flow through it.
85+
Create a peering from **vnet-1** to **vnet-2** with [az network vnet peering create](/cli/azure/network/vnet/peering#az-network-vnet-peering-create). If the `--allow-vnet-access` parameter isn't specified, a peering is established, but no communication can flow through it.
8186

8287
```azurecli-interactive
8388
az network vnet peering create \
84-
--name myVirtualNetwork1-myVirtualNetwork2 \
85-
--resource-group myResourceGroup \
86-
--vnet-name myVirtualNetwork1 \
89+
--name vnet-1-to-vnet-2 \
90+
--resource-group test-rg \
91+
--vnet-name vnet-1 \
8792
--remote-vnet $vNet2Id \
8893
--allow-vnet-access
8994
```
9095

91-
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 *myVirtualNetwork2* to *myVirtualNetwork1*. Create a peering from *myVirtualNetwork2* to *myVirtualNetwork1*.
96+
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**.
9297

9398
```azurecli-interactive
9499
az network vnet peering create \
95-
--name myVirtualNetwork2-myVirtualNetwork1 \
96-
--resource-group myResourceGroup \
97-
--vnet-name myVirtualNetwork2 \
100+
--name vnet-2-to-vnet-1 \
101+
--resource-group test-rg \
102+
--vnet-name vnet-2 \
98103
--remote-vnet $vNet1Id \
99104
--allow-vnet-access
100105
```
101106

102-
In the output returned after the previous command executes, you see that the **peeringState** is *Connected*. Azure also changed the peering state of the *myVirtualNetwork1-myVirtualNetwork2* peering to *Connected*. Confirm that the peering state for the *myVirtualNetwork1-myVirtualNetwork2* peering changed to *Connected* with [az network vnet peering show](/cli/azure/network/vnet/peering).
107+
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 [az network vnet peering show](/cli/azure/network/vnet/peering#az-network-vnet-show).
103108

104109
```azurecli-interactive
105110
az network vnet peering show \
106-
--name myVirtualNetwork1-myVirtualNetwork2 \
107-
--resource-group myResourceGroup \
108-
--vnet-name myVirtualNetwork1 \
111+
--name vnet-1-to-vnet-2 \
112+
--resource-group test-rg \
113+
--vnet-name vnet-1 \
109114
--query peeringState
110115
```
111116

112-
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*.
117+
Resources in one virtual network can't communicate with resources in the other virtual network until the **peeringState** for the peerings in both virtual networks is **Connected**.
113118

114119
## Create virtual machines
115120

116121
Create a VM in each virtual network so that you can communicate between them in a later step.
117122

118123
### Create the first VM
119124

120-
Create a VM with [az vm create](/cli/azure/vm). The following example creates a VM named *myVm1* in the *myVirtualNetwork1* virtual network. If SSH keys do not already exist in a default key location, the command creates them. To use a specific set of keys, use the `--ssh-key-value` option. The `--no-wait` option creates the VM in the background, so you can continue to the next step.
125+
Create a VM with [az vm create](/cli/azure/vm#az-vm-create). The following example creates a VM named **vm-1** in the **vnet-1** virtual network. If SSH keys don't already exist in a default key location, the command creates them. To use a specific set of keys, use the `--ssh-key-value` option. The `--no-wait` option creates the VM in the background, so you can continue to the next step.
121126

122127
```azurecli-interactive
123128
az vm create \
124-
--resource-group myResourceGroup \
125-
--name myVm1 \
129+
--resource-group test-rg \
130+
--name vm-1 \
126131
--image Ubuntu2204 \
127-
--vnet-name myVirtualNetwork1 \
128-
--subnet Subnet1 \
132+
--vnet-name vnet-1 \
133+
--subnet subnet-1 \
129134
--generate-ssh-keys \
130135
--no-wait
131136
```
132137

133138
### Create the second VM
134139

135-
Create a VM in the *myVirtualNetwork2* virtual network.
140+
Create a VM in the **vnet-2** virtual network.
136141

137142
```azurecli-interactive
138143
az vm create \
139-
--resource-group myResourceGroup \
140-
--name myVm2 \
144+
--resource-group test-rg \
145+
--name vm-2 \
141146
--image Ubuntu2204 \
142-
--vnet-name myVirtualNetwork2 \
143-
--subnet Subnet1 \
147+
--vnet-name vnet-2 \
148+
--subnet subnet-1 \
144149
--generate-ssh-keys
145150
```
146151

@@ -149,13 +154,13 @@ The VM takes a few minutes to create. After the VM is created, the Azure CLI sho
149154
```output
150155
{
151156
"fqdns": "",
152-
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVm2",
157+
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Compute/virtualMachines/vm-2",
153158
"location": "eastus",
154159
"macAddress": "00-0D-3A-23-9A-49",
155160
"powerState": "VM running",
156161
"privateIpAddress": "10.1.0.4",
157162
"publicIpAddress": "13.90.242.231",
158-
"resourceGroup": "myResourceGroup"
163+
"resourceGroup": "test-rg"
159164
}
160165
```
161166

@@ -165,28 +170,30 @@ Take note of the **publicIpAddress**. This address is used to access the VM from
165170

166171
## Communicate between VMs
167172

168-
Use the following command to create an SSH session with the *myVm2* VM. Replace `<publicIpAddress>` with the public IP address of your VM. In the previous example, the public IP address is *13.90.242.231*.
173+
Use the following command to create an SSH session with the **vm-2** VM. Replace `<publicIpAddress>` with the public IP address of your VM. In the previous example, the public IP address is **13.90.242.231**.
169174

170175
```bash
171176
ssh <publicIpAddress>
172177
```
173178

174-
Ping the VM in *myVirtualNetwork1*.
179+
Ping the VM in *vnet-1*.
175180

176181
```bash
177182
ping 10.0.0.4 -c 4
178183
```
179184

180185
You receive four replies.
181186

182-
Close the SSH session to the *myVm2* VM.
187+
Close the SSH session to the **vm-2** VM.
183188

184189
## Clean up resources
185190

186-
When no longer needed, use [az group delete](/cli/azure/group) to remove the resource group and all of the resources it contains.
191+
When no longer needed, use [az group delete](/cli/azure/group#az-group-delete) to remove the resource group and all of the resources it contains.
187192

188193
```azurecli-interactive
189-
az group delete --name myResourceGroup --yes
194+
az group delete \
195+
--name test-rg \
196+
--yes
190197
```
191198

192199
## Next steps

includes/ephemeral-ip-note.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: asudbring
33
ms.service: virtual-network
44
ms.topic: include
5-
ms.date: 10/26/2022
5+
ms.date: 04/15/2024
66
ms.author: allensu
77
---
88
> [!NOTE]
@@ -11,7 +11,7 @@ ms.author: allensu
1111
> The default outbound access IP is disabled when one of the following events happens:
1212
> - A public IP address is assigned to the VM.
1313
> - The VM is placed in the backend pool of a standard load balancer, with or without outbound rules.
14-
> - An [Azure Virtual Network NAT gateway](../articles/virtual-network/nat-gateway/nat-overview.md) resource is assigned to the subnet of the VM.
14+
> - An [Azure NAT Gateway](../articles/virtual-network/nat-gateway/nat-overview.md) resource is assigned to the subnet of the VM.
1515
>
1616
> VMs that you create by using virtual machine scale sets in flexible orchestration mode don't have default outbound access.
1717
>

0 commit comments

Comments
 (0)