Create VM and associate the VM to existing virtual network

6.4k views Asked by At

I created virtual network and I want to deploy a new VM to this virtual network. The network

I tried to create the VM using this command:

az vm create --resource-group myGroup --name VMTestNet1 --location eastus --image eastus1Image --vnet-name eastusVNet1 --admin-username azureuser --size Standard_F4S --public-ip-address ""

I got this exception:

Deployment failed. {
  "error": {
    "code": "InUseSubnetCannotBeDeleted",
    "message": "Subnet GatewaySubnet is in use by /subscriptions/subscriptionId/resourceGroups/Automationsystem/providers/Microsoft.Network/virtualNetworkGateways/eastusGW/ipConfigurations/vnetGatewayConfig0 and cannot be deleted.",
    "details": []
  }
}

How can I deploy my VM to an existing Virtual network?

2

There are 2 answers

0
Sa Yang On BEST ANSWER

I have successfully reproduced the issue using the subnet which are used by another VM and non-gateway subnet. So this issue may caused by Another instance was using the subnet and you didn’t supplied this subnet information when you created a new VM. We can fix it supply your subnet information in your vnet when you create a new VM.

Then we can create a new vm and associate it to exiting Vnet like this:

az vm create --resource-group myGroup 
--name VMTestNet1 --location eastus 
--image eastus1Image 
--vnet-name eastusVNet1 
--subnet <your subnet> 
--admin-username <your user name>  --admin-password <your password> 
--size standard_F4S 
--public-ip-address ""

I test these script and it worked.

0
Satish Devan On
  1. Export subnet information to a variable

    export SUBNETID =$(az network vnet subnet show --resource-group *RESOURCEGROUPNAME* -name *SUBNETNAME* --vnet-name *VNETNAME* --query id -o tsv)
    
  2. Create VM using this command

    az vm create --name *VNNAME* --resource-group *RESOURCEGROUPNAME* --image "RHEL" --size "Standard_B2s" --authentication-type password --admin-password “XXXXXXXX" --admin-username "admin" --public-ip-address "" --location "westus" --nsg "" --subnet $SUBNETID