Subnets in Azure: Segmenting Your Virtual Network
A subnet is a slice of a VNet’s address space assigned to a specific purpose. A VNet without subnets is like a warehouse with no shelves — everything piles up in one space with no order. Subnets let you organize resources, apply different security rules to different groups, and control traffic flow between tiers of your application.
What a subnet actually does
When you create a VNet with the address space 10.0.0.0/16, that gives you 65,536 IP addresses. A subnet carves a portion of those addresses for a specific group of resources. For example:
snet-web might use 10.0.1.0/24 — that’s 251 usable IPs for web servers.
snet-db might use 10.0.2.0/24 — 251 usable IPs for databases.
Resources in the same subnet can communicate freely. Resources in different subnets communicate through Azure’s virtual router, which means you can apply route tables and Network Security Groups at the subnet boundary to control or inspect that traffic.
This separation is the foundation of network segmentation. Even if an attacker compromises a web server in snet-web, they cannot automatically reach the database in snet-db — your NSG rules determine whether that traffic is allowed.
The 5 reserved addresses rule
Azure reserves 5 IP addresses in every subnet. Understanding which ones they are helps you plan subnet sizes correctly.
For a subnet with range 10.0.1.0/24:
| Address | Purpose |
|---|---|
| 10.0.1.0 | Network address (identifies the subnet) |
| 10.0.1.1 | Azure default gateway |
| 10.0.1.2 | Azure DNS mapping |
| 10.0.1.3 | Reserved for future Azure use |
| 10.0.1.255 | Broadcast address |
A /24 subnet gives you 256 total addresses minus 5 reserved = 251 usable IPs. A /28 gives you 16 total minus 5 = 11 usable IPs. A /29 gives 8 total minus 5 = 3 usable IPs.
Never create a /30 or smaller subnet for regular workloads. The minimum recommended size for any subnet with resources is /28.
Subnet design patterns
There is no single right way to design subnets, but several patterns work well for most architectures:
Tier-based segmentation. One subnet per application tier: web, app, data, management. This is the most common pattern for traditional three-tier applications. NSG rules control traffic between tiers.
Environment-based segmentation. Separate subnets for production, staging, and dev resources within the same VNet. This works when you want to keep all environments in one VNet but apply different security rules.
Service-based segmentation. One subnet per Azure service type: one for VMs, one for App Service integration, one for private endpoints, one for the gateway. This works well in hub-and-spoke architectures.
Some Azure services require dedicated subnets. Azure Bastion requires a subnet named exactly AzureBastionSubnet, VPN gateways require GatewaySubnet, and Azure Firewall requires AzureFirewallSubnet. Reserve these subnet names even if you haven’t deployed those services yet.
Managing subnets with Azure CLI
All subnet operations use az network vnet subnet. The VNet must exist before you can create subnets inside it.
Create a subnet:
az network vnet subnet create \
--name snet-web \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001 \
--address-prefixes 10.0.1.0/24Create a subnet and attach an NSG at the same time:
# First create the NSG
az network nsg create \
--name nsg-web \
--resource-group rg-prod-networking \
--location eastus
# Then create the subnet with the NSG attached
az network vnet subnet create \
--name snet-web \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001 \
--address-prefixes 10.0.1.0/24 \
--network-security-group nsg-webUpdate an existing subnet to attach an NSG:
az network vnet subnet update \
--name snet-web \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001 \
--network-security-group nsg-webList all subnets in a VNet:
az network vnet subnet list \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001 \
--output tableShow available IP addresses in a subnet:
az network vnet subnet show \
--name snet-web \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001 \
--query "{name:name, prefix:addressPrefix, available:ipConfigurations}" \
--output jsonDelete a subnet (only possible when no resources are attached):
az network vnet subnet delete \
--name snet-web \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001Service endpoints on subnets
Service endpoints extend a subnet’s identity to Azure PaaS services like Azure Storage and Azure SQL. When you enable a service endpoint, traffic from that subnet to the PaaS service travels over the Azure backbone rather than the public internet.
Enable the Azure Storage service endpoint on a subnet:
az network vnet subnet update \
--name snet-app \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001 \
--service-endpoints Microsoft.StorageEnable multiple service endpoints at once:
az network vnet subnet update \
--name snet-app \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001 \
--service-endpoints Microsoft.Storage Microsoft.Sql Microsoft.KeyVaultService endpoints are different from Private Endpoints. Service endpoints extend the subnet’s routing to a PaaS service but the service still has a public IP. Private Endpoints give the PaaS service a private IP inside your VNet. For maximum security, prefer Private Endpoints.
Subnet delegation
Some Azure services need to inject resources directly into your subnet. Subnet delegation is how you grant that permission. When you delegate a subnet to a service, that service can create and manage network interfaces in the subnet on your behalf.
Examples of services that require subnet delegation: Azure App Service VNet integration, Azure Container Instances, Azure NetApp Files.
Delegate a subnet to Azure App Service:
az network vnet subnet update \
--name snet-appservice \
--resource-group rg-prod-networking \
--vnet-name vnet-prod-eastus-001 \
--delegations Microsoft.Web/serverFarmsA delegated subnet can only be used by the service it is delegated to. Do not try to put VMs in a subnet delegated to App Service — the deployment will fail.
Common mistakes
- Making subnets too small. A /28 sounds fine until you deploy an AKS cluster that needs dozens of IPs per node. For anything that scales horizontally — VM scale sets, AKS, App Service environments — start with /22 or larger.
- Not reserving special subnet names. AzureBastionSubnet, GatewaySubnet, and AzureFirewallSubnet must use those exact names. If you name them something else and then deploy those services, Azure will fail the deployment or require you to create a new correctly-named subnet.
- Putting databases in the same subnet as web servers. Even if you trust NSG rules, a flat subnet means a compromised web server has a direct network path to your database. Defense in depth requires physical subnet separation.
Summary
- Subnets divide a VNet’s address space into segments for different workload types or tiers.
- Azure reserves 5 IP addresses in every subnet — account for this when sizing subnets.
- Apply NSGs at the subnet level to control traffic between tiers.
- Some Azure services require specially named subnets: GatewaySubnet, AzureBastionSubnet, AzureFirewallSubnet.
- Use service endpoints to route PaaS traffic over the Azure backbone; use Private Endpoints for maximum security.
Frequently asked questions
How many subnets can a VNet have?
A single VNet supports up to 3,000 subnets. In practice, most production VNets use between 4 and 20 subnets organized by workload tier, environment, or service type.
Can I resize a subnet after creating it?
You can expand a subnet (make it larger) if no resources are deployed into it and the new range does not overlap with other subnets. Shrinking a subnet or changing its range when resources are present requires removing all resources first.
Why does Azure reserve 5 IP addresses in every subnet?
Azure reserves the first four addresses (network address, Azure gateway, Azure DNS, future use) and the last address (broadcast). For a /24 subnet with 256 total addresses, you get 251 usable IPs.