Playing Around VMs With Azure CLI 2.0

This is a sequel to my previous series on Azure CLI 2.0 and you can refer to these articles: 

This is the 4th article in this series. To understand this, 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.

Create a resource group

We will create a resource group with the name azureguyvmdemo and host it in the location Central US.

az group create --name azureguyvmdemo --location “Central US”

Azure CLI

Create a virtual network

We need to create a virtual network, where our VM will be hosted. We will create a Vnet with the name vmdemonet with a subnet vmdemosubnet.

az network vnet create --resource-group azureguyvmdemo --name vmdemovnet --subnet-name vmdemosubnet

Azure CLI

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

Azure CLI

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

Azure CLI

Create a virtual network card and associate with public IP address and NSG

We need to create a NIC, assign it to the public IP address and NSG, which we have created in the steps given above. 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

Azure CLI

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, which you need to use, you can run the command given below.

az vm list –query “[].urnAlias”

The command given above is basically going to get the top 10 commonly used images, followed by the command basically giving multiple properties, so we are querying it on the urnAlias property. Also, as you can see in the screenshot given below, we are getting only the top 10 mostly used images but if you can’t find your image in the list, 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 it as well.

Azure CLI

We are going to use the win2012datacenter image and will assign it to the respective resource group, NIC and location, which we have created in the steps given above. 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

Azure CLI

Open port 3389 to allow RDP traffic to host.

In order to get 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 the port no 443 for HTTPS and 80 for HTTP.

az vm open-port --port 3389 --resource-group myResourceGroup --name demovm

Azure CLI