This article is focuses on how to use Azure CLI to provision a Windows virtual machine on Azure.
You would need Azure account in order to execute these script. If you don’t have one, you can create a free Azure account here.
CLI stands for command line interface. A bash prompt can be used to run the Azure CLI commands. These commands can be used form Windows / Linux / MacOS.
You can install the Azure CLI tools from this link depending on which operating system you are on.
If you are not planning for any Azure certification, I would strongly recommend to at least go for AZ-203 certification which is about knowing Azure products from developer perspective.
Please refer my blog article below and it should help you get prepared for the exam.
Let’s Get Started for creating a windows VM on Azure.
Few things to know before starting…
Let’s recap few concepts before moving further…
Resource Group – All Azure resources are organized in Resource Groups. We are going to create new resource i.e. Virtual Machine inside resource group of this name.
Location – The Azure region in which we want to place the new resource
VM Name – to specify the name of virtual machine
Let’s setup few variables to help us in VM creation:
# Update for your admin username and password AdminUsername=azureuser AdminPassword=SpecifyYourPassword # Update information about resource group, location and machine name ResourceGroupName=MyResourceGroup MachineLocation=westeurope MachineName=MyWinVM
Login to Azure
Below command would help to login to Azure. Once this command is executed, it would open a popup where you can enter your email id and password for logging into Azure.
# Login to Azure Az Login
Create Resource Group
This statement would create the resource group. The virtual machine would be placed inside this resource group.
If this resource group is deleted, all resources inside this resource groups would be deleted.
# Create a resource group az group create \ --name $ResourceGroupName \ --location $MachineLocation
Create Virtual Network
The virtual network allows virtual machines to securely communicate with one another.
# Create a virtual network az network vnet create \ --resource-group $ResourceGroupName \ --name myVnet \ --subnet-name mySubnet
Create public IP
This command to reserve a static public IP address. Even if you boot multiple times, the static IP does not change. This IP can be used to connect with virtual machine using RDP.
# Create a public IP address az network public-ip create \ --resource-group $ResourceGroupName \ --name myPublicIP
Create Network Security Group and NIC
The Network Security Group has rules which either allow or deny the traffic inbound to virtual network or outbound from virtual network.
The 3389 port is the port for allowing remote desktop.
# Create a network security group. az network nsg create \ --resource-group $ResourceGroupName \ --name myNetworkSecurityGroup # Create a virtual network card # and associate with public IP address and NSG. az network nic create \ --resource-group $ResourceGroupName \ --name myNic \ --vnet-name myVnet \ --subnet mySubnet \ --network-security-group myNetworkSecurityGroup \ --public-ip-address myPublicIP
Create Virtual Machine
This command uses all the configurations done till now and creates the virtual machine.
You will have to wait around 5 minutes of time to get the virtual machine created.
# Create a virtual machine. az vm create \ --resource-group $ResourceGroupName \ --name $MachineName \ --location $MachineLocation \ --nics myNic \ --image win2016datacenter \ --admin-username $AdminUsername \ --admin-password $AdminPassword
Open 3389 port for RDP
This script would create a rule for remote desktop connection. Once this step is complete, you can go to Azure portal and try login to the virtual machine using the user name and password you have provided in previous command.
# Open port 3389 to allow RDP traffic to host. az vm open-port --port 3389 \ --resource-group $ResourceGroupName \ --name $MachineName
Something to Remember…
If you have created this VM only for learning purpose, please note that there is cost associated with every running virtual machine. So, please do not forget to delete your newly created resource group and all its contents by running below Azure CLI command.
#Removes all resources within this resource group az group delete --name $ResourceGroupName --yes
I hope you enjoyed this article. Please do not forget to comment and let me know if this information helped you.