ARM模板是否需要同时在父资源内部及外部配置同一资源?
Great question! Let's clear up this confusion around ARM template subnet configurations once and for all.
First, the core answer: You do NOT need to declare the same subnet both inside the parent VNet resource and as a separate standalone/nested resource. Each approach below is fully valid on its own—pick the one that fits your template's structure best, no duplication required.
1. Nested Subnet Configuration (Inside VNet Resource)
This is a concise, all-in-one method where you define subnets directly within the subnets array of your VNet's properties. All VNet and subnet configurations live together in one resource block:
{ "type": "Microsoft.Network/virtualNetworks", "apiVersion": "2020-11-01", "name": "vnetName", ... "properties": { "subnets": [ { "name": "subnetName", "properties": { ... // Subnet details like address prefix, NSG associations, etc. } } ] } }
2. Standalone/Nested Child Subnet Resource
If you prefer modularity (or need to deploy subnets separately later), you can split the VNet and subnet into separate resources:
Step 1: Declare the VNet without subnets
{ "type": "Microsoft.Network/virtualNetworks", "apiVersion": "2020-11-01", "name": "vnetName", ... "properties": { ... // VNet properties like address space, no subnets array needed } }
Step 2: Declare the subnet as a child resource
You can either define this at the top level of your template, or nest it inside the VNet's resources block (as you saw in Microsoft docs). Both work the same way—just make sure to use the parent/child naming format and set a dependsOn rule to ensure the VNet exists first:
Top-level standalone subnet
{ "type": "Microsoft.Network/virtualNetworks/subnets", "apiVersion": "2020-11-01", "name": "[concat('vnetName', '/', 'subnetName')]", // Critical: Use parent/child naming convention "dependsOn": [ "[resourceId('Microsoft.Network/virtualNetworks', 'vnetName')]" ], "properties": { ... // Subnet details } }
Nested inside the VNet resource
{ "type": "Microsoft.Network/virtualNetworks", "apiVersion": "2020-11-01", "name": "vnetName", ... "properties": { ... // VNet properties }, "resources": [ { "type": "subnets", "apiVersion": "2020-11-01", "name": "subnetName", "dependsOn": [ "[resourceId('Microsoft.Network/virtualNetworks', 'vnetName')]" ], "properties": { ... // Subnet details } } ] }
Key Takeaway
Duplicating subnet declarations (both inside the VNet's subnets array and as a child resource) is unnecessary, creates template redundancy, and could even cause deployment conflicts if configurations don't match. Choose the approach that aligns with your workflow:
- Use the nested
subnetsarray for simple, single-template deployments where you want all VNet-related config in one place. - Use standalone/nested child resources for modular templates, or when you need to deploy subnets independently from the parent VNet.
内容的提问来源于stack exchange,提问作者Michael McCarthy

