Provisioning Microsoft Virtual Machines Through Command Line Interface (CLI)

This blog explains about provisioning virtual machines through the CLI Tool. To start that, we need to have the Command Line Interface (CLI) tool installed on our Windows machine.
 
To install CLI, start the PowerShell command prompt as an administrator and run the below command.
  1. Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'  
The above code will download the CLI for your Windows and if the CLI tool already exists, this will update that with the latest version on your machine.
 
After installing the CLI, you can log in to your Azure account by typing the below code and hitting Enter on your command prompt or Windows PowerShell. 
  1. az login  
This will open a web browser asking to enter your credentials. Please provide your valid credentials to log in. Now, we are all set to perform some basic operations into logged on user's Azure Portal. Let's see some of the basic operations one by one.
 

Resource Group

 
Resource Group is a container that holds and stores your Azure solution in a specified group. By this resource group, you can retrieve metadata of your resources. Let's get to know how to create the Resource Group using CLI command.
  1. az group create \  
  2.       -- name "demo-rg" \  
  3.       -- location "centralus"  
Note
"\" - refers to continue the execution to the next line.
 
The above code will create the Resource group on your Azure portal with the name "demo-rg". If you want to verify the list of groups on your Aure account or to check the resource group you have created, run the below code on your CLI tool. This will execute the list of all resource groups available on your subscription. 
  1. az group list -o table  

Virtual Network and Subnet

 
Once we have created the resource group, we are going to create the Virtual Network under the same resource group. Below CLI code will help us to create the Virtual Network and subnet together on the "demo-rg" group we have created.
  1. az network vnet create \  
  2.    --resource-group "demo-rg" \  
  3.    --name "demo-vnet-1" \  
  4.    --address-prefix "10.1.0.0/16" \  
  5.    --subnet-name "demo-subnet-1"  \  
  6.    --subnet-prefix "10.1.1.0/24"  
To list all the virtual networks created on the "demo-rg" group, use the below code to run through CLI command.
  1. az network vnet list -o table  

Create Public IP Address

 
To provision Virtual Machines, we need to have the IP address created for our resource group. The below code helps us to create the public IP address inside the resource group we have created already.
  1. az network public-ip create \  
  2.    --resource-group "demo-rg" \  
  3.    --name "demo-linux-1-pip-1"  

Create Network Security Group

 
After creating the public IP address, create a network security group for your resource group. To do that, use the below CLI commands to perform the action.
  1. az network nsg create \  
  2.    --resource-group "demo-rg" \  
  3.    --name "demo-linux-nsg-1"  

Create Virtual Network Interface and associate with the public IP address and NSG

 
A virtual network interface is an important phase before creating the virtual machine on your Azure. We have to create this interface to associate both the IP address and Network security group 
  1. az network nic create \  
  2.    --resource-group "demo-rg" \  
  3.    --name "demo-linux-1-nic-1" \  
  4.    --vnet-name "demo-vnet-1" \  
  5.    --subnet "demo-subnet-1" \  
  6.    --network-security-group "demo-linux-nsg-1" \  
  7.    --public-ip-address "demo-linux-1-pip-1"  

Create Virtual Machine on your Azure Portal

 
Now, we are going to create a virtual machine with the attributes we have created so far.  Please find the code below to create the VM on your Azure portal.
  1. az vm create \  
  2.    --resource-group "demo-rg" \  
  3.    --location "centralus" \  
  4.    --name "demo-linux-1" \  
  5.    --nics "demo-linux-1-nic-1" \  
  6.    --image "rhel" \  
  7.    --admin-username "demoadmin" \  
  8.    --authentication-type "ssh" \  
  9.    --ssh-key-value ~/.ssh/id_rsa.pub  
After creating the VM, we should enable SSH traffic. This will enable remote connectivity to the machine we have created. To achieve this execute the below CLI command.
  1. az vm open-port \  
  2.   --resource-group "demo-rg"  
  3.   --name "demo-linux-1" \  
  4.   --port "22"  
Once it is enabled, we can connect the Virtual Machine from our Azure portal. Happy Learning !!