5. Detailed Azure CLI Commands
5.1 Resource Group Creation
az group create \
--name myResourceGroup \
--location eastus
Parameter Explanation:
- --name: Name of the resource group (must be unique within subscription)
- --location: Azure region where resources will be deployed
Alternative locations: westus, centralus, northeurope, southeastasia
Purpose: Creates a logical container for all related resources. This is the foundation step that must be completed first.
5.2 Virtual Network Creation
az network vnet create \
--resource-group myResourceGroup \
--name myVNet \
--address-prefix 10.0.0.0/16 \
--location eastus
Parameter Explanation:
- --resource-group: Reference to the resource group created in step 1
- --name: Name for the virtual network
- --address-prefix: CIDR block for the entire VNet (10.0.0.0/16 provides 65,536 IPs)
- --location: Must match the resource group location
Alternative address spaces: 172.16.0.0/16, 192.168.0.0/16
Purpose: Creates the virtual network foundation. The address space must be large enough to accommodate all planned subnets.
5.3 Subnet Creation
az network vnet subnet create \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name myDelegatedSubnet \
--address-prefix 10.0.1.0/24
Parameter Explanation:
- --resource-group: Resource group containing the VNet
- --vnet-name: Name of the parent virtual network
- --name: Name for the subnet that will be delegated
- --address-prefix: CIDR block for the subnet (10.0.1.0/24 provides 254 usable IPs)
Subnet sizing considerations: Some services require specific minimum sizes (e.g., SQL MI needs /24 or larger)
Purpose: Creates the subnet that will be dedicated to the Azure service. This subnet will be isolated from other subnets.
5.4 Subnet Delegation Configuration
az network vnet subnet update \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name myDelegatedSubnet \
--delegations Microsoft.ContainerInstance/containerGroups
Parameter Explanation:
- --resource-group: Resource group containing the subnet
- --vnet-name: Parent virtual network name
- --name: Subnet to be delegated
- --delegations: Service delegation type
Common delegation types:
- Microsoft.ContainerInstance/containerGroups - for Azure Container Instances
- Microsoft.Sql/managedInstances - for SQL Managed Instance
- Microsoft.Web/serverFarms - for App Service Environment
- Microsoft.NetApp/volumes - for Azure NetApp Files
Purpose: This is the critical step that reserves the subnet exclusively for the specified service. Once delegated, only that service can deploy resources to this subnet.
5.5 Network Security Group Creation
az network nsg create \
--resource-group myResourceGroup \
--name myDelegatedSubnetNSG \
--location eastus
Parameter Explanation:
- --resource-group: Resource group for the NSG
- --name: Name for the network security group
- --location: Azure region (must match other resources)
Purpose: Creates a network security group that will control traffic to/from the delegated subnet. NSGs act as a virtual firewall.
5.6 NSG Security Rules Configuration
az network nsg rule create \
--resource-group myResourceGroup \
--nsg-name myDelegatedSubnetNSG \
--name AllowHTTPS \
--protocol Tcp \
--priority 100 \
--direction Inbound \
--source-address-prefixes '*' \
--source-port-ranges '*' \
--destination-address-prefixes '*' \
--destination-port-ranges 443 \
--access Allow
Parameter Explanation:
- --resource-group: Resource group containing the NSG
- --nsg-name: Name of the network security group
- --name: Name for this specific rule
- --protocol: Network protocol (Tcp, Udp, or *)
- --priority: Rule priority (100-4096, lower numbers processed first)
- --direction: Traffic direction (Inbound or Outbound)
- --source-address-prefixes: Source IP addresses or ranges
- --destination-port-ranges: Destination ports to allow
- --access: Allow or Deny the traffic
Common port ranges: 80 (HTTP), 443 (HTTPS), 22 (SSH), 3389 (RDP)
Purpose: Defines specific traffic rules for the delegated subnet. Each service may require different port configurations.
5.7 Associate NSG with Subnet
az network vnet subnet update \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name myDelegatedSubnet \
--network-security-group myDelegatedSubnetNSG
Parameter Explanation:
- --resource-group: Resource group containing the subnet
- --vnet-name: Parent virtual network
- --name: Subnet to associate with NSG
- --network-security-group: NSG to associate with the subnet
Purpose: Links the network security group to the delegated subnet, activating the firewall rules for all traffic to/from the subnet.
5.8 Route Table Creation
az network route-table create \
--resource-group myResourceGroup \
--name myDelegatedSubnetRouteTable \
--location eastus \
--disable-bgp-route-propagation false
Parameter Explanation:
- --resource-group: Resource group for the route table
- --name: Name for the route table
- --location: Azure region
- --disable-bgp-route-propagation: Whether to disable BGP route propagation
BGP Route Propagation: Set to false to allow ExpressRoute/VPN gateway routes, true to prevent them
Purpose: Creates a custom route table for controlling traffic flow from the delegated subnet.
5.9 Custom Route Creation
az network route-table route create \
--resource-group myResourceGroup \
--route-table-name myDelegatedSubnetRouteTable \
--name DefaultRoute \
--next-hop-type VirtualAppliance \
--address-prefix 0.0.0.0/0 \
--next-hop-ip-address 10.0.2.4
Parameter Explanation:
- --resource-group: Resource group containing the route table
- --route-table-name: Name of the route table
- --name: Name for this specific route
- --next-hop-type: Type of next hop (VirtualAppliance, VirtualNetworkGateway, Internet, None)
- --address-prefix: Destination address prefix (0.0.0.0/0 means all traffic)
- --next-hop-ip-address: IP address of the next hop (required for VirtualAppliance)
Next hop types:
- VirtualAppliance - Route to a firewall or NVA
- VirtualNetworkGateway - Route to VPN/ExpressRoute gateway
- Internet - Route directly to internet
- None - Drop the traffic
Purpose: Defines custom routing behavior for traffic from the delegated subnet. This example routes all traffic through a network virtual appliance.
5.10 Associate Route Table with Subnet
az network vnet subnet update \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name myDelegatedSubnet \
--route-table myDelegatedSubnetRouteTable
Parameter Explanation:
- --resource-group: Resource group containing the subnet
- --vnet-name: Parent virtual network
- --name: Subnet to associate with route table
- --route-table: Route table to associate
Purpose: Links the route table to the delegated subnet, activating the custom routing rules for all traffic from the subnet.
6. Configuration Stanzas
6.1 Service Endpoint Configuration
az network vnet subnet update \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name myDelegatedSubnet \
--service-endpoints Microsoft.Storage Microsoft.Sql
Service Endpoints Configuration:
- --service-endpoints: List of Azure services to enable direct connectivity
- Available endpoints: Microsoft.Storage, Microsoft.Sql, Microsoft.KeyVault, Microsoft.AzureActiveDirectory
Purpose: Enables direct, secure connectivity from the delegated subnet to Azure services without going through the public internet. This improves security and performance.
When to use: When your delegated service needs to access Azure Storage, SQL Database, or other Azure services securely.
6.2 Private Endpoint Configuration
az network private-endpoint create \
--resource-group myResourceGroup \
--name myPrivateEndpoint \
--vnet-name myVNet \
--subnet myDelegatedSubnet \
--private-connection-resource-id /subscriptions/subscription-id/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount \
--group-ids blob \
--connection-name myPrivateConnection
Private Endpoint Configuration:
- --name: Name for the private endpoint
- --vnet-name: Virtual network containing the subnet
- --subnet: Subnet where the private endpoint will be created
- --private-connection-resource-id: Full resource ID of the target Azure service
- --group-ids: Sub-resource of the target service (blob, file, queue for Storage)
- --connection-name: Name for the private connection
Purpose: Creates a private endpoint that brings Azure services directly into your VNet, providing private IP connectivity.
When to use: When you need private connectivity to Azure services without using service endpoints or public internet.
6.3 NAT Gateway Configuration
az network nat gateway create \
--resource-group myResourceGroup \
--name myNATGateway \
--location eastus \
--idle-timeout 4 \
--public-ip-addresses myPublicIP
NAT Gateway Configuration:
- --name: Name for the NAT gateway
- --location: Azure region
- --idle-timeout: Idle timeout in minutes (4-120)
- --public-ip-addresses: Public IP addresses to use for outbound connectivity
Purpose: Provides predictable outbound internet connectivity for resources in the delegated subnet.
When to use: When you need consistent outbound IP addresses for services in the delegated subnet.
az network vnet subnet update \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name myDelegatedSubnet \
--nat-gateway myNATGateway
Associate NAT Gateway with Subnet:
Purpose: Links the NAT gateway to the delegated subnet, enabling all outbound traffic from the subnet to use the NAT gateway's public IP addresses.
Execution order: This command must be run after creating the NAT gateway and public IP addresses.
8. Troubleshooting
8.1 Common Issues and Solutions
Issue 1: Subnet Delegation Failure
Symptom: "Subnet cannot be delegated because it contains existing resources"
Solution: Remove all existing resources from the subnet before attempting delegation
# Check for existing resources in subnet
az network vnet subnet show \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name mySubnet \
--query "ipConfigurations"
Issue 2: Service Deployment Failure
Symptom: Service fails to deploy to delegated subnet
Solution: Verify delegation type matches service requirement and subnet size is adequate
# Verify delegation configuration
az network vnet subnet show \
--resource-group myResourceGroup \
--vnet-name myVNet \
--name mySubnet \
--query "delegations"
8.2 Validation Commands
# Validate VNet configuration
az network vnet show \
--resource-group myResourceGroup \
--name myVNet \
--query "{name:name,addressSpace:addressSpace,subnets:subnets[].{name:name,addressPrefix:addressPrefix,delegations:delegations[].serviceName}}"
# Check effective routes
az network nic show-effective-route-table \
--resource-group myResourceGroup \
--name myNIC
# Verify NSG associations
az network nsg show \
--resource-group myResourceGroup \
--name myNSG \
--query "subnets[].id"
📋 Summary
This comprehensive guide covers all aspects of Azure subnet delegation, from basic concepts to advanced scenarios. The key to successful implementation is following the correct sequence of commands and understanding the specific requirements of each Azure service you're integrating.
Remember: Always validate your configuration at each step and test connectivity before deploying production workloads.