Playing around VMs with Azure CLI 2.0
This is a sequel to my previous blog series on azure cli 2.0. If you have not gone through them check out the articles on Introduction, Getting started and working with Webapps. This is the 4th article in this series, in order to understand this one you need to go through the first two articles.
We are going to create a VM in azure CLI 2.0. For the people who have worked on azure classic vms, creating the VM in the Resource Manager model was a pain. With Azure CLI 2.0 you can create a vm with just one single command as well and all the necessary related items will be set to the default values. We are going to look into the detailed configuration steps one by one.
1) Create a resource group.
We will create a resource group with the name azureguyvmdemo and host it in location Central US
az group create --name azureguyvmdemo --location “Central US”
2) Create a virtual network.
We need to create a virtual network where our VM will be hosted. We will create a Vnet with name vmdemonet with a subnet vmdemosubnet.
az network vnet create --resource-group azureguyvmdemo --name vmdemovnet --subnet-name vmdemosubnet
3) Create a public IP address.
We need to assign a physical IP address to the VM and here we are going to name it myvmdemoIp.
az network public-ip create --resource-group azureguyvmdemo --name myvmdemoIp
4) Create a network security group.
We need to now create a network security group or NSG and we are naming it as demosng.
az network nsg create --resource-group azureguyvmdemo --name demonsg
5) Create a virtual network card and associate with public IP address and NSG.
We need to create a NIC and assign it to the public IP address and NSG that we have created in the above steps. We are going to name it demonic.
az network nic create --resource-group azureguyvmdemo --name demoNic --vnet-name vmdemovnet --subnet vmdemosubnet --network-security-group demonsg --public-ip-address myvmdemoIp
6) Create a virtual machine.
Now the whole environment is ready so we have to create a VM. Now in order to create a vm we need to supply the os image as a parameter. If you’re not sure about the exact image name that you need to use, you can run the following command:
az vm list –query “.urnAlias”
The above command is basically going to get the top 10 commonly used images and then that command basically give multiple properties so we are querying it on the urnAlias property. Also, as you can see in the screenshot below we are getting only top 10 mostly used images but if you can’t find your image in that list then you can use –-all in the command to get the list of all the images available in VM. You can also upload your Image and use that as well.
We are going to use the win2012datacenter image and will assign it to the respective resource group,NIC and location that we have created in the above steps. We need to pass on admin username and password as well to authenticate and rdp into the machine.
az vm create --resource-group azureguyvmdemo --name demovm --location Central US --nics demonic --image win2012datacenter --admin-username azureguy --admin-password azure123
7) Open port 3389 to allow RDP traffic to host.
In order to RDP into the machine we need to open the port no 3389. Also, if you want to host some Webapp on the VM’s IIS you need to open port no 443 for HTTPS and 80 for HTTP.
az vm open-port --port 3389 --resource-group myResourceGroup --name demovm