How to provision and configure a Linux VM using Azure CLI
Paschal Kenechukwu Oruche
Posted on May 20, 2023
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:
- An Azure subscription
- Azure CLI installed on your local machine
- Basic knowledge of the Linux operating system
- 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
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
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"
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”
Expose HTTP port for web traffic:
az vm open-port --port 80 --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME
Expose HTTPS port for encrypted web traffic:
az vm open-port --port 443 --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME
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
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!!
Posted on May 20, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.