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.

<br />
#Login Azure<br />
az login<br />

Create a new resource group.

<br />
#create a resource group<br />
az group create --location southeastasia --name clivm<br />

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

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

Create a VNet for VM deployment

<br />
#Create VNET<br />
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<br />

Create Network Security Group

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

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

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

Create NIC for VM

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

Create a VM (CentOS)

<br />
#Create a VM<br />
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<br />

Open SSH port to communicate with the VM

<br />
#Open port 22 for SSH<br />
az vm open-port --name linuxvm --port 22 --resource-group clivm --apply-to-subnet --priority 1000<br />
#OR<br />
# 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<br />

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

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

<br />
#!/bin/bash</p>
<p>#Login Azure<br />
az login </p>
<p>#create a resource group<br />
az group create --location southeastasia --name clivm </p>
<p>#Create a Public IP<br />
az network  public-ip create --name linuxvm-ip --resource-group clivm --location southeastasia </p>
<p>#Create VNET<br />
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 </p>
<p>#Create Network Security Group<br />
az network nsg create --name vmsubnet --resource-group clivm --location southeastasia </p>
<p>#Associate the NSG to the subnet<br />
az network vnet subnet update --name vmsubnet --resource-group clivm --vnet-name vm-test --network-security-group vmsubnet </p>
<p>#Create NIC<br />
az network nic create --resource-group clivm --name linuxvm-nic --subnet vmsubnet --vnet-name vm-test --location southeastasia --public-ip-address linuxvm-ip </p>
<p>#Create a VM<br />
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</p>
<p>#Open port 22 for SSH<br />
az vm open-port --name linuxvm --port 22 --resource-group clivm --apply-to-subnet --priority 1000<br />
#OR<br />
# 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<br />

 

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

, , , , , ,

Post navigation