Skip to content

Commit 23b57cb

Browse files
authored
Merge pull request #297525 from mumian/0402-update-subnet
update vnet subnet info
2 parents 22855f7 + 604a31a commit 23b57cb

File tree

1 file changed

+15
-84
lines changed

1 file changed

+15
-84
lines changed

articles/azure-resource-manager/bicep/scenarios-virtual-networks.md

Lines changed: 15 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Create virtual network resources by using Bicep
33
description: Describes how to create virtual networks, network security groups, and route tables by using Bicep.
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 03/17/2025
6+
ms.date: 04/25/2025
77
---
88

99
# Create virtual network resources by using Bicep
@@ -16,69 +16,12 @@ Define your virtual networks by creating a resource with the type [`Microsoft.Ne
1616

1717
### Configure subnets by using the subnets property
1818

19-
Virtual networks contain subnets, which are logical groups of IP addresses within the virtual network. There are two ways to define subnets in Bicep: by using the `subnets` property on the virtual network resource, and by creating a [child resource](child-resource-name-type.md) with type `Microsoft.Network/virtualNetworks/subnets`.
19+
Virtual networks contain subnets, which are logical groupings of IP addresses within the network. Subnets should always be managed as child resources, and the **subnets** property should never be defined within the virtual network resource. This approach ensures a safe and independent lifecycle for both resource types.
2020

21-
> [!WARNING]
22-
> Avoid defining subnets as child resources. This approach can result in downtime for your resources during subsequent deployments, or failed deployments.
21+
> [!NOTE]
22+
> The Azure Virtual Network API is updated to allow modifications to virtual networks without requiring the inclusion of the subnet property in PUT requests. Previously, omitting the subnet property would result in the deletion of existing subnets. With the new behavior, if the subnet property isn't included in a PUT request, the existing subnets remain unchanged. Explicitly setting the subnet property to an empty value deletes all existing subnets, while providing specific subnet configurations creates or updates subnets accordingly. This change simplifies virtual network management by preventing unintended subnet deletions during updates. For more information, see [Azure Virtual Network now supports updates without subnet property](https://techcommunity.microsoft.com/blog/azurenetworkingblog/azure-virtual-network-now-supports-updates-without-subnet-property/4067952).
2323
24-
It's best to define your subnets within the virtual network definition, as in this example:
25-
26-
> The following example is part of a larger example. For a Bicep file that you can deploy, [see the complete file](https://raw.githubusercontent.com/Azure/azure-docs-bicep-samples/main/samples/scenarios-virtual-networks/vnet.bicep).
27-
28-
```bicep
29-
param location string = resourceGroup().location
30-
31-
var virtualNetworkName = 'my-vnet'
32-
var subnet1Name = 'Subnet-1'
33-
var subnet2Name = 'Subnet-2'
34-
35-
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-11-01' = {
36-
name: virtualNetworkName
37-
location: location
38-
properties: {
39-
addressSpace: {
40-
addressPrefixes: [
41-
'10.0.0.0/16'
42-
]
43-
}
44-
subnets: [
45-
{
46-
name: subnet1Name
47-
properties: {
48-
addressPrefix: '10.0.0.0/24'
49-
}
50-
}
51-
{
52-
name: subnet2Name
53-
properties: {
54-
addressPrefix: '10.0.1.0/24'
55-
}
56-
}
57-
]
58-
}
59-
60-
resource subnet1 'subnets' existing = {
61-
name: subnet1Name
62-
}
63-
64-
resource subnet2 'subnets' existing = {
65-
name: subnet2Name
66-
}
67-
}
68-
69-
output subnet1ResourceId string = virtualNetwork::subnet1.id
70-
output subnet2ResourceId string = virtualNetwork::subnet2.id
71-
```
72-
73-
Although both approaches enable you to define and create your subnets, there is an important difference. When you define subnets by using child resources, the first time your Bicep file is deployed, the virtual network is deployed. Then, after the virtual network deployment is complete, each subnet is deployed. This sequencing occurs because Azure Resource Manager deploys each individual resource separately.
74-
75-
When you redeploy the same Bicep file, the same deployment sequence occurs. However, the virtual network is deployed without any subnets configured on it because the `subnets` property is effectively empty. Then, after the virtual network is reconfigured, the subnet resources are redeployed, which re-establishes each subnet. In some situations, this behavior causes the resources within your virtual network to lose connectivity during your deployment. In other situations, Azure prevents you from modifying the virtual network and your deployment fails.
76-
77-
### Access subnet resource IDs
78-
79-
You often need to refer to a subnet's resource ID. When you use the `subnets` property to define your subnet, [you can use the `existing` keyword](existing-resource.md) to also obtain a strongly typed reference to the subnet, and then access the subnet's `id` property:
80-
81-
> The following example is part of a larger example. For a Bicep file that you can deploy, [see the complete file](https://raw.githubusercontent.com/Azure/azure-docs-bicep-samples/main/samples/scenarios-virtual-networks/vnet.bicep).
24+
It's best to define your subnets as [child resources](./child-resource-name-type.md#within-parent-resource), as in this example:
8225

8326
```bicep
8427
param location string = resourceGroup().location
@@ -87,7 +30,7 @@ var virtualNetworkName = 'my-vnet'
8730
var subnet1Name = 'Subnet-1'
8831
var subnet2Name = 'Subnet-2'
8932
90-
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-11-01' = {
33+
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2024-05-01' = {
9134
name: virtualNetworkName
9235
location: location
9336
properties: {
@@ -96,38 +39,27 @@ resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-11-01' = {
9639
'10.0.0.0/16'
9740
]
9841
}
99-
subnets: [
100-
{
101-
name: subnet1Name
102-
properties: {
103-
addressPrefix: '10.0.0.0/24'
104-
}
105-
}
106-
{
107-
name: subnet2Name
108-
properties: {
109-
addressPrefix: '10.0.1.0/24'
110-
}
111-
}
112-
]
11342
}
11443
115-
resource subnet1 'subnets' existing = {
44+
resource subnet1 'subnets' = {
11645
name: subnet1Name
117-
}
46+
properties: {
47+
addressPrefix: '10.0.0.0/24'
48+
} }
11849
119-
resource subnet2 'subnets' existing = {
50+
resource subnet2 'subnets' = {
12051
name: subnet2Name
52+
properties: {
53+
addressPrefix: '10.0.1.0/24'
54+
}
12155
}
12256
}
12357
12458
output subnet1ResourceId string = virtualNetwork::subnet1.id
12559
output subnet2ResourceId string = virtualNetwork::subnet2.id
12660
```
12761

128-
Because this example uses the `existing` keyword to access the subnet resource, instead of defining the complete subnet resource, it doesn't have the risks outlined in the previous section.
129-
130-
You can also combine the `existing` and `scope` keywords to refer to a virtual network or subnet resource in another resource group.
62+
To reference a nested resource outside the parent resource, it must be qualified with the containing resource name and the :: operator as shown in the preceding example.
13163

13264
## Network security groups
13365

@@ -148,4 +80,3 @@ Private endpoint approval is an operation, so you can't perform it directly with
14880
- Quickstart templates
14981
- [Create a Virtual Network with two Subnets](https://azure.microsoft.com/resources/templates/vnet-two-subnets/)
15082
- [Virtual Network with diagnostic logs](https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.network/vnet-create-with-diagnostic-logs)
151-

0 commit comments

Comments
 (0)