4 Ways to Create Virtual Machine in Microsoft Azure
Tags: azure, devops, cloud
Published: 2023-05-13 12:45:34

A virtual machine is an important element of most successful businesses using cloud resources.

Think of a virtual machine as a personal computer with your desired specifications to run different tasks for you. For instance, you need a PC with at least 32GB RAM and over 1TB storage among other specifications. But due to limited financial capabilities, you are unable to purchase the PC. This is one of the ways a virtual machine comes in. 

In this article, I will show you five (5) different ways to create virtual machine in Microsoft Azure.

Microsoft Azure is one of the foremost cloud services provider with high credibility. Before you access the Azure portal, you need to sign up for an account - a free tier account is also available with enough resources to get started including a $200 credit.

Now to the real business. How can you create a virtual machine in Microsoft Azure?

**Option 1: Using the Azure Portal**

Access the Azure Portal by visiting https://portal.azure.com. Input Virtual Machine in the search bar.
![Azure Portal Search](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hliqkqgf2vnyzsddud5n.png)

In the search results, click "create" on the Virtual Machine icon
![Azure Popular Resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2f8m424va19n5e4pt6u0.png)

Select your subscription. If you do not have an active subscription, create one or a free trial subscription from Microsoft.

After selecting your subscription, select the resource group this virtual machine should belong to or create a new resource group
![Creating Virtual Machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/th6ee5nx5nz43gj1vhj4.png)

Give your virtual machine a name - like a computer name. Select an OS (image) for your VM.
![Virtual Machine details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssjq5uez9jo2rdd58sbb.png)

After selecting your image and check other information, click on "review + create" to create your virtual machine.
![Available image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jmvpnrb3mtt3hlvvzos.png)

Deployment will take a moment. After deployment completes, you have successfully created a virtual machine and can go to the resource.
![VM Deployed with azure quickstart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ins8tityh9kq9dmwcvmb.png)

**Option 2: Using Custom Template from Azure Quickstart**

Creating a virtual machine by deploying using a custom template is a fast and easy method that does not require a user to do too much.

_Select "Deploy custom template"_
![Azure Quickstart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/337xy0fhl0qo5py3xc7v.png)

On the next page, provide your virtual machine details including an admin username and password.
![Azure Virtual Machine Custom Template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ref86gun7j9t5iistoqi.png)

Click on "Review + create" and watch the magic! 
![Create Virtual Machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0rg1w70dis4thusiwem.png)

**Option 3: Using Azure Cloud Shell**
Azure Cloud Shell provides us with a simple way to create resource group and virtual machines using simple shell commands.

Open up the cloud shell terminal by clicking on the icon on the azure portal.
![Azure Cloud Shell](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odxmlyqfkdhyxozcljof.png)

Select PowerShell in the presented tab.
![Cloud Shell](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7o3jchhbdu0syvlt0qv.png)

Ensure you are in the right directory. In the image below, we are in the "/home/seidu" directory.
![PowerShell Terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0fjpjn9ggd4qzv413l0.png)

Before crating a virtual machine, there must be a resource group. The following commands should be run in this form: _**New-AzResourceGroup -Name "Your Resource Group Name" -Location "Your resource location".**_
**Example:** 

```
New-AzResourceGroup -Name "myresourcegroup" -Location "EAST US"
```
![Create resource group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lroj9hneh98yjf2phewr.png)

After successfully creating a resource group, we will go ahead to create our virtual machine using this command: 
_**New-AzVM -ResourceGroupName "Resource group name" -Name "vm name" -Location "EAST US"**_
**Example:**
```
New-AzVM -ResourceGroupName "myresourcegroup" -Name MyVm -Location "EAST US"
```

![VM PowerShell command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3nlhy2ey49mvkznb7ow.png)
You will be prompted to provide a username and password for the virtual machine. Virtual machine will then be created and details displayed as in the image below.

![VM Created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7p2m46yhpqos8wdh83y.png)

**Option 4: Using Azure Cli**
The microsoft azure CLI command is similar to using the power shell commands. 

To access the CLI, click on the "BASH" terminal in the cloud shell window.

![Azure CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqohlw6xwevwshiqa35s.png)

To create a resource group, run:
**_az group create --name "resource group name" --location "Resource Location"_**
**Example:**
```
az group create --name mytestgroup --location EastUS
```
![Resoure Group Azure CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9a7fgazfgz1kz73x5a34.png)

To create a virtual machine in our resource group, we run:
az vm create \
--resource-group "resource group name" \
--name "vm machine name" \
--image "os image" \
--generate-ssh-keys

Exmple:

```
az vm create \
--resource-group myRgGroup \
--name myVmMachine \
--image UbuntuLTS \
--generate-ssh-keys
```
![VM CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tvk4rk6aluau8u24j3e.png)

**Conclusion:**
Virtual machines are essential for today's businesses as it helps to scale in/out up/down depending on the needs. Microsoft Azure has done so well in this regards by providing developers with all they need.

**_Important Notice:_** Remember to delete any resource you create which is not in use in order not to incur unwanted expenses.