⚡ Day 2: Azure Zero to Hero Series – Working with Azure CLI – Automate Your Infrastructure


Welcome to Day 2 of the Azure Zero to Hero series!
Today, we dive into one of the most powerful tools for automating Azure: the Azure Command-Line Interface (Azure CLI).
You’ll learn how to:
Install Azure CLI on your system
Log in and configure your Azure environment
Deploy your first resource (a virtual machine) using CLI
Understand scripting potential for automation
🛠️ What is Azure CLI?
Azure CLI is a cross-platform command-line tool to manage Azure resources.
It works on Linux, macOS, and Windows and helps automate tasks that you’d otherwise perform manually in the Azure Portal.
🧠 Think of it as a DevOps-friendly way to control Azure using scripts and commands!
🔽 Step 1: Install Azure CLI
Depending on your OS, follow the appropriate method:
✅ For Windows:
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows \
-OutFile .\AzureCLI.msi; Start-Process msiexec.exe \
-Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
✅ For Ubuntu/Debian:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
✅ For macOS:
brew update && brew install azure-cli
🔐 Step 2: Login to Azure via CLI
Once installed, open a terminal or PowerShell and run:
az login
This opens a browser window to authenticate your Microsoft account. After successful login, your terminal displays your subscription info.
📦 Step 3: Set Subscription and Default Region
(Optional if you have multiple subscriptions)
az account list --output table
az account list --query "[].{Name:name, ID:id}" --output table
az account set --subscription "<your-subscription-name>"
Set a default region to avoid typing it every time:
az group list --query "[].name" --output table
az configure --defaults location=eastus
📁 Step 4: Create a Resource Group using CLI
open git bash
# create a shell script to install and configure nginx webserver
vi create_nginx_server.sh
#!/bin/bash
# Set your Azure subscription ID
subscription_id="<SubscriptionId>"
# Set your Azure resource group, VNet, and subnet names
resource_group="web-rg"
vnet_name="web-vnet"
subnet_name="web-subnet"
# Set VM details
vm_name="testservervm"
vm_username="testuser"
vm_password="TestUser@123456"
# Set custom data script for Nginx installation and configuration
custom_data=$(cat <<EOF
#cloud-config
package_update: true
package_upgrade: true
packages:
- nginx
- wget
- unzip
runcmd:
- systemctl enable nginx
- systemctl start nginx
- wget -O /tmp/lugx_temp.zip https://templatemo.com/download/templatemo_589_lugx_gaming
- unzip -q /tmp/lugx_temp.zip -d /var/www/
- mv /var/www/templatemo_589_lugx_gaming/* /var/www/html/
- rm -rf /tmp/lugx_temp.zip /var/www/templatemo_589_lugx_gaming
- chown -R www-data:www-data /var/www/html
- chmod -R 755 /var/www/html
- systemctl restart nginx
EOF
)
# Set the active subscription
az account set --subscription "$subscription_id"
# Create a resource group
az group create --name "$resource_group" --location eastus
# Create a virtual network
az network vnet create --resource-group "$resource_group" --name "$vnet_name" --address-prefix 10.0.0.0/16 --subnet-name "$subnet_name" --subnet-prefix 10.0.0.0/24
# Create a public IP address
az network public-ip create --resource-group "$resource_group" --name "${vm_name}-pip" --sku Standard
# Create a network security group (NSG) and allow SSH and HTTP traffic
az network nsg create --resource-group "$resource_group" --name "${vm_name}-nsg"
az network nsg rule create --resource-group "$resource_group" --nsg-name "${vm_name}-nsg" --name allow-ssh --priority 100 --access Allow --protocol Tcp --direction Inbound --source-address-prefixes '*' --source-port-ranges '*' --destination-address-prefixes '*' --destination-port-ranges 22
az network nsg rule create --resource-group "$resource_group" --nsg-name "${vm_name}-nsg" --name allow-http --priority 200 --access Allow --protocol Tcp --direction Inbound --source-address-prefixes '*' --source-port-ranges '*' --destination-address-prefixes '*' --destination-port-ranges 80
# Create a virtual machine
az vm create \
--resource-group "$resource_group" \
--name "$vm_name" \
--image Ubuntu2204 \
--admin-username "$vm_username" \
--admin-password "$vm_password" \
--public-ip-address "${vm_name}-pip" \
--nsg "${vm_name}-nsg" \
--vnet-name "$vnet_name" \
--subnet "$subnet_name" \
--custom-data "$custom_data" \
--size Standard_B1s \
--generate-ssh-keys
# Open HTTP port in NSG (redundant but ensures it's open)
az vm open-port --resource-group "$resource_group" --name "$vm_name" --port 80
# Output VM details
public_ip=$(az vm show --show-details --resource-group "$resource_group" --name "$vm_name" --query publicIps --output tsv)
echo "VM $vm_name created successfully."
echo "Public IP: $public_ip"
echo "SSH username: $vm_username"
echo "Web server URL: http://$public_ip"
chmod +x create_nginx_server.sh
./create_nginx_server.sh
🧹 Step 7: Access the website
check on browsere URL: http://$public_ip
🧹 Step 8: Clean Up Resources
To delete the resource group and all resources:
# Delete all
az group delete --name $resource_group --yes
# az group delete --name $resource_group --yes --no-wait
🧹 Step 9: Check the resources are deleted
az group list --query "[].name" --output table
🚀 Why Use Azure CLI?
Feature | Benefit |
Scriptable | Great for automation and DevOps |
Cross-platform | Works on Windows, Linux, macOS |
Fast and Consistent | Same commands for all services |
Scripting-friendly | Easily embedded into shell scripts and pipelines |
✅ Summary
Step | Task |
1️⃣ | Installed Azure CLI |
2️⃣ | Logged into Azure |
3️⃣ | Set default region |
4️⃣ | Created a resource group |
5️⃣ | Launched a VM and added custom data script for nginx |
6️⃣ | Opened network ports |
7️⃣ | Cleaned up resources |
🔜 Coming Up: Day 3 – Understanding ARM Templates & IaC
We’ll learn how to define infrastructure as code (IaC) using ARM Templates, and compare them with tools like Terraform.
💬 Got questions or issues with commands?
Drop a comment below, and I’ll be happy to help!
🧵 Follow for the next episode in the Azure Zero to Hero series!
Subscribe to my newsletter
Read articles from SRINIVAS TIRUNAHARI directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
