Adam
Adam

Reputation: 28858

Why is the ID for a subnet created using Azure Resource Manager Powershell cmdlets null?

I am trying to provision some network resources in Azure and I am running into a blocker with a step that tries to reference a previously created subnet's Id property.

Here's the command to retrieve the subnet details:

$gwsubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -VirtualNetwork $vnet

And then outputting the value of $gwsubnet in the console:

Name : GatewaySubnet Id : Etag : ProvisioningState : AddressPrefix : 10.1.1.0/24 IpConfigurations : null NetworkSecurityGroup : null RouteTable : null

Note that the Id property is null. This causes an error for a future step, e.g.:

$gwipconfig = New-AzureRmVirtualNetworkGatewayIpConfig -Name config1 -SubnetId $gwsubnet.Id -PublicIpAddressId $gwpip.Id

Gives the following error:

New-AzureRmVirtualNetworkGatewayIpConfig : Cannot validate argument on parameter 'SubnetId'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. At line:1 char:99 + ... nfig -SubnetId $gwsubnet.Id -PublicIpAddressId $gwpip.Id + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [New-AzureRmVirtualNetworkGatewayIpConfig], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Network.NewAzureVirtualNetworkGatewayIpConfigCommand

I'm struggling with how to have my subnet have an Id assigned to it. Is this something that I need to logout/login again to see? thanks for the help.

Upvotes: 3

Views: 30598

Answers (1)

Michael B
Michael B

Reputation: 12228

The problem you are having is that the subnet Id isn't created until you have created the virtual network.

When you run Get-AzureRmVirtualNetworkSubnetConfig all you create is a configuration to attach to New-AzureRmVirtualNetwork Once you have run that command it will connect to Azure, deploy the Vnet, create the subnet and at that state it will create a subnet Id.

The following code will show you what I mean...

$DNSNameLabel = "mydnsname"
$NetworkName = "MyNet"
$NICName = "MyNIC"
$PublicIPAddressName = "MyPIP"
$SubnetName = "MySubnet"
$SubnetAddressPrefix = "10.0.0.0/24"
$VnetAddressPrefix = "10.0.0.0/16"

$SingleSubnet = New-AzureRmVirtualNetworkSubnetConfig `
                -Name $SubnetName `
                -AddressPrefix $SubnetAddressPrefix

#At this point nothing is created, so there is no valid subnet id

$Vnet = New-AzureRmVirtualNetwork -Name $NetworkName `
                -ResourceGroupName $ResourceGroupName `
                -Location $LocationName `
                -AddressPrefix $VnetAddressPrefix `
                -Subnet $SingleSubnet

#vnet and subnet are created, both get an id and etag

 $vnet = Get-AzureRmVirtualNetwork -Name $NetworkName 
                -ResourceGroupName $ResourceGroupName 

$vnet.Subnets[0].Id 

Which will give

/subscriptions/{Subscription ID}/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/MyNet/subnets/MySubnet

Upvotes: 9

Related Questions