Script to Deploy Azure VM (Azure CLI)

The Azure CLI 2.0 is Azure’s new command line experience for managing Azure resources. It can be used on MacOS, Linux, and Windows.

We can use Azure CLI for administrating Azure resources and building automation scripts against Azure Resource Manager.To start with Azure CLI follow this doc for step by step guide for installation.

From this post I will walk-through how we can use Azure CLI to create a virtual machine.

After installing the Azure CLI first we should login to the subscription.When you run the following command it will prompt a URL,browse the URL and enter the code, then select the login credential of the subscription.

#Login Azure 
az login 

Create a new resource group.

#create a resource group 
az group create --location southeastasia --name clivm 

Create Public IP, We use this public IP for the VM and it attached to the NIC of the VM

#Create a Public IP 
az network  public-ip create --name linuxvm-ip --resource-group clivm --location southeastasia 

Create a VNet for VM deployment

#Create VNET 
az network vnet create --name vm-test --resource-group clivm --address-prefixes 10.10.0.0/16 --location southeastasia --subnet-name vmsubnet --subnet-prefix 10.10.1.0/24 

Create Network Security Group

#Create Network Security Group
az network nsg create --name vmsubnet --resource-group clivm --location southeastasia 

For this NSG is attached to the subnet alternatively we can attach the NSG to NIC

#Associate the NSG to the subnet 
az network vnet subnet update --name vmsubnet --resource-group clivm --vnet-name vm-test --network-security-group vmsubnet 

Create NIC for VM

#Create NIC
az network nic create --resource-group clivm --name linuxvm-nic --subnet vmsubnet --vnet-name vm-test --location southeastasia --public-ip-address linuxvm-ip 

Create a VM (CentOS)

#Create a VM
az vm create --name linuxvm --resource-group clivm --admin-username localadmin --admin-password kasunraj@1990 --image CentOS --authentication-type password --nics linuxvm-nic  --size Standard_DS1 --location southeastasia

Open SSH port to communicate with the VM

#Open port 22 for SSH
az vm open-port --name linuxvm --port 22 --resource-group clivm --apply-to-subnet --priority 1000 
#OR
# az network nsg rule create --name SSH --nsg-name vmsubnet --priority 1100 --resource-group clivm --source-address-prefix * --source-port-range * --destination-address-prefix * --destination-port-range 22 --protocol Tcp 

Following is the full script if your running this as a bash file

We use #!/bin/bash as the interpreter then it will start to execute the commands.
Before execute the script make sure the script has execute permissions

#Giving Read,Write,Execute permissions 
chmod 700 ./CreateLinuxVM.sh

#!/bin/bash

#Login Azure 
az login 

#create a resource group 
az group create --location southeastasia --name clivm 

#Create a Public IP 
az network  public-ip create --name linuxvm-ip --resource-group clivm --location southeastasia 

#Create VNET 
az network vnet create --name vm-test --resource-group clivm --address-prefixes 10.10.0.0/16 --location southeastasia --subnet-name vmsubnet --subnet-prefix 10.10.1.0/24 

#Create Network Security Group
az network nsg create --name vmsubnet --resource-group clivm --location southeastasia 

#Associate the NSG to the subnet 
az network vnet subnet update --name vmsubnet --resource-group clivm --vnet-name vm-test --network-security-group vmsubnet 

#Create NIC
az network nic create --resource-group clivm --name linuxvm-nic --subnet vmsubnet --vnet-name vm-test --location southeastasia --public-ip-address linuxvm-ip 

#Create a VM
az vm create --name linuxvm --resource-group clivm --admin-username localadmin --admin-password kasunraj@1990 --image CentOS --authentication-type password --nics linuxvm-nic  --size Standard_DS1 --location southeastasia

#Open port 22 for SSH
az vm open-port --name linuxvm --port 22 --resource-group clivm --apply-to-subnet --priority 1000 
#OR
# az network nsg rule create --name SSH --nsg-name vmsubnet --priority 1100 --resource-group clivm --source-address-prefix * --source-port-range * --destination-address-prefix * --destination-port-range 22 --protocol Tcp 

 

After the script runs we can SSH to the sever as bellow. Here I use Azure Cloud Shell to SSH the server.

, , , , , ,

Post navigation