How to use Azure CLI to create a Virtual Machine
Tags: azurecli, virtualmachine, commandlineinterface, cloudshell
Published: 2023-05-15 16:12:51

There are different ways of creating virtual machine. These include azure [portal](https://dev.to/yemmyoye/creating-windows-virtual-machine-on-microsoft-azure-portal-1n0j), [PowerShell](https://dev.to/yemmyoye/deploying-a-resource-group-and-virtual-machine-using-powershell-4mi4) , [Azure Resources Manager template](https://dev.to/yemmyoye/using-a-custom-template-to-deploy-a-linux-virtual-machine-828) and the Azure Command-Line Interface (CLI).In our previous blog posts, we discussed the first three methods, and we will now cover the last one, CLI, in this blog.
CLI is a cross-platform command-line tool that allows users to login to Azure and run administrative commands on its resources. It enables the use of interactive command-line prompts or scripts to execute commands through a terminal.
There are two ways to access CLI and they are
(i)from a browser through the Azure CloudShell
(ii)from inside a Docker Container

Let's proceed to our task of the day

1.Log in to Azure portal and click on CloudShell

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmjjlywbwiak292ppoc3.png)


Select Bash

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcbt895h7vffqf85xv2e.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru9455ri1gsgcufy9izq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j60lss4e5re96pp5fyt2.png)




2.Copy the Command for creating your Resource group and paste it on the terminal window. Here I will create a resource group with the name AbbyRg using this command:
**az group create --name AbbyRg --location EastUS**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lycow7s988xgi1ax04w4.png)
Then press enter

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eoet9bmu23u60eg01jb2.png)
From the image above, the Provisioning state is showing succeeded indicating that the Resource group, AbbyRg has been created

3.Copy the Command for creating your Virtual machine and paste it on the terminal window.I will name my Virtual machine AbbyVMcli and use the command below:
**az vm create \
--resource-group AbbyRg \
--name AbbyVMcli \
--image UbuntuLTS \
--generate-ssh-keys**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdl0h4dxbhkihnm3pc4x.png)

Then press enter

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0pvbngfur7ivr7v3qd5.png)
The Powerstate in the image above shows that the Virtual machine is running indicating that it has been created

4.To confirm the status about the Resource group and Virtual machine created, go to your azure account and search for the two.The images below show the Resource group AbbyRg and Virtual machine AbbyVMcli on the Azure portal 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrchjgrxrqvupclby184.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2va7q56eymli8d3vkdy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jz8m7ampgjiikw61eejh.png)

5.For compliance with Azure best practices, we will check Azure Advisor for recommendations in respect of reliability, security, operational excellence, performance, and cost of the resources created.
Click on the Hamburger Icon on the top left-hand corner of Azure portal and Select Advisor from the dropdown menu 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nry4m5syntqf4hzov71.png)
The image below shows a Recommendation on Reliability. The recommendation is to use NAT gateway for Outbound Connectivity

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7qk7gwm993fojw6i1io.png)

I hope you find this blog post useful.