How To Create An Azure Virtual Machine Using Azure CLI
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In this blog post, we will discuss how to create an Azure Virtual Machine using Azure CLI.
What Is Azure CLI:
Azure Command Line Interface or Azure CLI is a collection of commands that can be used to interact with Azure service. Like in AWS we use AWS CLI or in GCP we use gcloud, in Azure you can use Azure CLI to create and manage your Azure resources.
What is Azure Virtual Machine:
Azure Virtual Machine or Azure VM is the compute service offered by Azure to host your virtual servers in Azure cloud. Azure Virtual Machine is basically a virtual server running on Azure datacenter. Like in AWS you use EC2 instance or in GCP you use Google Compute Engine Instance, in Azure, you will use Azure VM instance if you need to create and manage your own server on Azure cloud.
Prerequisites:
- One system with Azure CLI installed and configured.
You can follow the below blog post if you haven’t installed and configured Azure CLI yet.
https://cloudaffaire.com/how-to-install-and-configure-azure-cli/
How To Create An Azure Virtual Machine Using Azure CLI:
Step 1: Select the Azure location of your choice where you will deploy your Azure VM. For this demo, I am using southeastasia location.
1 2 3 4 5 6 7 8 |
############################################################ ## How To Create An Azure Virtual Machine Using Azure CLI ## ############################################################ ## I am executing the command on a PowerShell session. ## For command prompt replace ` by ^ and for Shell replace ` by \ ## List all available locations in Azure az account list-locations -o table; |
Step 2: Create a resource group.
1 2 3 4 |
## Create a resource group. az group create ` --name MyResourceGroup ` --location southeastasia; |
Step 3: Create a virtual network.
1 2 3 4 5 6 7 |
# Create a virtual network. az network vnet create ` --resource-group MyResourceGroup ` --name MyNetwork ` --address-prefixes 10.0.0.0/16 ` --subnet-name MySubNetwork ` --subnet-prefixes 10.0.1.0/24; |
Step 4: Create a public IP address.
1 2 3 4 |
# Create a public IP address. az network public-ip create ` --resource-group MyResourceGroup ` --name MyPublicIP; |
Step 5: Create a network security group.
1 2 3 4 |
# Create a network security group. az network nsg create ` --resource-group MyResourceGroup ` --name MyNetworkSecurityGroup; |
Step 6: Create a virtual network card with the public IP address and network security group.
1 2 3 4 5 6 7 8 |
# Create a virtual network card and associate with a public IP address and NSG. az network nic create ` --resource-group MyResourceGroup ` --name MyNic ` --vnet-name MyNetwork ` --subnet MySubNetwork ` --network-security-group MyNetworkSecurityGroup ` --public-ip-address MyPublicIP; |
Step 7: Select an OS image for your Azure VM. For this demo, I am using CentOS image.
1 2 3 4 5 6 7 8 |
## List all available images for a location az vm image list -f CentOS --location southeastasia --query urnAlias; ## az vm image list -f RHEL --location southeastasia; ## az vm image list -f Ubuntu --location southeastasia; ## az vm image list -f Debian --location southeastasia; ## az vm image list -f openSUSE --location southeastasia; ## az vm image list -f WindowsServer --location southeastasia; |
Step 8: Create your Azure VM and generate the ssh key for connection. Copy the public IP address from the output.
1 2 3 4 5 6 7 |
# Create a new virtual machine, this creates SSH keys if not present. az vm create ` --resource-group MyResourceGroup ` --name MyAzureVM ` --nics MyNic ` --image CentOS ` --generate-ssh-keys; |
Note: The ssh key will be stored under .ssh directory in your home directory.
Step 9: Open 22 port in your Azure VM for SSH connectivity.
1 2 3 4 5 |
# Open port 22 to allow SSh traffic to host. az vm open-port ` --port 22 ` --resource-group MyResourceGroup ` --name MyAzureVM; |
Step 10: Connect to your Azure VM.
1 2 3 |
## Connect to your Azure VM ssh -i exit |
Step 11: Cleanup
1 2 |
## Cleanup az group delete --name MyResourceGroup; |
Hope you have enjoyed this blog post. Please refer below Azure documentation for more details
https://docs.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest
https://docs.microsoft.com/en-us/cli/azure/
https://docs.microsoft.com/en-us/azure/virtual-machines/linux/cli-samples
https://docs.microsoft.com/en-us/azure/virtual-machines/