DEV Community

Cover image for How to provision and configure a Linux VM using Azure CLI
Paschal Kenechukwu Oruche
Paschal Kenechukwu Oruche

Posted on

How to provision and configure a Linux VM using Azure CLI

Whether you're a seasoned DevOps pro or just getting started on your cloud journey, exploring the wonders of the cloud using a command line interface is fulfilling. It can not be overemphasized that one of the frequent expectations of a Cloud/DevOps engineer is to provision resources in the cloud efficiently.

In this piece, we'll explore how we can leverage the Azure CLI to provision resources in the cloud, with a specific focus on creating and configuring a Linux virtual machine (VM).

*Rudiments /Requirements:
*

Before getting started, ensure that you have the following prerequisites in place:

  1. An Azure subscription
  2. Azure CLI installed on your local machine
  3. Basic knowledge of the Linux operating system
  4. Basic knowledge of Bash scripting

Device your environment variables

I'm using a Mac for this, so I'll define mine in my ~/.bashrc file located in my home direcroty.

$RESOURCE_GROUP_NAME
$LOCATION
$VM_NAME
$VM_IMAGE
$ADMIN_USERNAME

Env vars for Vm provisioning

We need to run the below command to persist the env vars in our shell

source ~/.bashrc

Login to Azure CLI

open your terminal or command prompt and enter command below:
az login

or
az login --use-device-code

Now, proceed to provision the Azure VM using CLI

az vm create \
--resource-group $RESOURCE_GROUP_NAME \
--name $VM_NAME \
--image $VM_IMAGE \
--admin-username $ADMIN_USERNAME \
--generate-ssh-keys \
--public-ip-sku Standard

Output of VM provisioning

Install Nginx server on newly provisioned vm:

az vm run-command invoke \
   --resource-group $RESOURCE_GROUP_NAME \
   --name $VM_NAME \
   --command-id RunShellScript \
   --scripts "sudo apt-get update && sudo apt-get install -y nginx"
Enter fullscreen mode Exit fullscreen mode

Nginx server installation

Start the Nginx web server

az vm run-command invoke \
   --resource-group $RESOURCE_GROUP_NAME \
   --name $VM_NAME \
   --command-id RunShellScript \
   --scripts "sudo systemctl start nginx”
Enter fullscreen mode Exit fullscreen mode

Expose HTTP port for web traffic:

az vm open-port --port 80 --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME
Enter fullscreen mode Exit fullscreen mode

Opening web port 80

Expose HTTPS port for encrypted web traffic:

az vm open-port --port 443 --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME
Enter fullscreen mode Exit fullscreen mode

At this point, I had a blocker whilst trying to run the command to allow HTTPS traffic, I was able to overcome it using the method below.

Blocker:

_> (SecurityRuleConflict) Security rule open-port-80 conflicts with rule open-port-443. Rules cannot have the same Priority and Direction. To learn more, see aka.ms/nsgrules.

Code: SecurityRuleConflict
Message: Security rule open-port-80 conflicts with rule open-port-443. Rules cannot have the same Priority and Direction. To learn more, see aka.ms/nsgrules._

Solution

az vm open-port --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME --port 443 --priority 940
Enter fullscreen mode Exit fullscreen mode

Network ports confirmed

Site running

Summary

we explored how to provision and configure a Linux VM using Azure CLI. We covered the steps from logging in to Azure CLI, creating a resource group, provisioning the VM, connecting to it via SSH, and performing Linux-specific configurations. With Azure CLI's powerful commands, managing and deploying Linux VMs on Azure becomes straightforward.

The End!!

Top comments (0)