Azure

Mastering Azure CLI: A Complete Reference Guide

Azure CLI is a powerful cross-platform command-line tool for managing Azure resources. This guide covers installation, authentication, core commands, scripting, CI/CD integration, and best practices for efficient cloud management.

T
Tran Quang
Author
⏱️5 min read
📅March 19, 2025
Azure & Cloud
👀Tech
#Kubernetes#Containers#Storage#Microsoft#Best Practices

Azure CLI (Command-Line Interface) is a cross-platform tool that enables developers and administrators to manage Azure resources directly from a terminal. Available on Windows, macOS, and Linux, it provides a consistent scripting experience for automating cloud operations.

What is Azure CLI?

Azure CLI is an open-source command-line tool built on Python that interacts with Azure Resource Manager APIs. It lets you create, update, delete, and query Azure resources without leaving your terminal, making it ideal for automation, DevOps pipelines, and day-to-day cloud management.

Installing Azure CLI

On Windows

winget install -e --id Microsoft.AzureCLI

On macOS

brew update && brew install azure-cli

On Linux (Ubuntu/Debian)

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

Verify the installation:

az --version

Authenticating with Azure

Interactive Login

az login

This opens a browser window for authentication. After login, your subscription list is displayed.

Service Principal Login (for CI/CD)

az login --service-principal \
  --username <APP_ID> \
  --password <PASSWORD_OR_CERT> \
  --tenant <TENANT_ID>

Managed Identity (on Azure VMs)

az login --identity

Managing Subscriptions

# List all subscriptions
az account list --output table

# Set the active subscription
az account set --subscription "<SUBSCRIPTION_NAME_OR_ID>"

# Show current subscription
az account show

Working with Resource Groups

# Create a resource group
az group create --name MyResourceGroup --location eastus

# List all resource groups
az group list --output table

# Delete a resource group
az group delete --name MyResourceGroup --yes --no-wait

Core Commands and Concepts

Output Formats

Azure CLI supports multiple output formats:

az vm list --output json      # Default JSON
az vm list --output table     # Human-readable table
az vm list --output tsv       # Tab-separated values (for scripting)
az vm list --output yaml      # YAML format

Querying with JMESPath

Use --query to filter output:

# Get only VM names and locations
az vm list --query "[].{Name:name, Location:location}" --output table

# Find VMs in a specific location
az vm list --query "[?location=='eastus'].name" --output tsv

Using --no-wait for Async Operations

az vm create --resource-group MyRG --name MyVM --no-wait

Common Azure CLI Commands by Service

Virtual Machines

# Create a VM
az vm create \
  --resource-group MyRG \
  --name MyVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

# Start / Stop / Restart a VM
az vm start --resource-group MyRG --name MyVM
az vm stop --resource-group MyRG --name MyVM
az vm restart --resource-group MyRG --name MyVM

# List VM sizes in a region
az vm list-sizes --location eastus --output table

Azure App Service

# Create an App Service Plan
az appservice plan create \
  --name MyPlan \
  --resource-group MyRG \
  --sku B1 \
  --is-linux

# Create a Web App
az webapp create \
  --resource-group MyRG \
  --plan MyPlan \
  --name MyWebApp \
  --runtime "NODE|18-lts"

# Deploy code via zip
az webapp deployment source config-zip \
  --resource-group MyRG \
  --name MyWebApp \
  --src app.zip

Azure Storage

# Create a storage account
az storage account create \
  --name mystorageacct \
  --resource-group MyRG \
  --location eastus \
  --sku Standard_LRS

# Upload a blob
az storage blob upload \
  --account-name mystorageacct \
  --container-name mycontainer \
  --name myfile.txt \
  --file ./localfile.txt

Azure Kubernetes Service (AKS)

# Get credentials for a cluster
az aks get-credentials --resource-group MyRG --name MyCluster

# Scale node count
az aks scale --resource-group MyRG --name MyCluster --node-count 5

Scripting and Automation

Azure CLI is designed for scripting. Combine it with bash/PowerShell for powerful automation:

#!/bin/bash
RESOURCE_GROUP="prod-rg"
LOCATION="eastus"
APP_NAME="my-app-$(date +%s)"

# Create resource group if not exists
az group show --name $RESOURCE_GROUP &>/dev/null || \
  az group create --name $RESOURCE_GROUP --location $LOCATION

# Deploy app
az webapp create \
  --resource-group $RESOURCE_GROUP \
  --plan MyPlan \
  --name $APP_NAME \
  --runtime "DOTNET|8.0"

echo "Deployed: $APP_NAME"

Azure CLI Extensions

Extend functionality with add-on extensions:

# List available extensions
az extension list-available --output table

# Add an extension
az extension add --name aks-preview

# Update all extensions
az extension update --all

Using Azure CLI in CI/CD Pipelines

GitHub Actions

- name: Azure Login
  uses: azure/login@v1
  with:
    creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Deploy to Web App
  run: |
    az webapp deployment source config-zip \
      --resource-group ${{ secrets.RESOURCE_GROUP }} \
      --name ${{ secrets.APP_NAME }} \
      --src release.zip

Azure DevOps Pipeline

- task: AzureCLI@2
  inputs:
    azureSubscription: 'MyServiceConnection'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az group list --output table

Useful Tips and Best Practices

  1. Use --help liberally: Every command supports --help (e.g., az vm create --help).
  2. Enable auto-upgrade: az config set auto-upgrade.enable=yes
  3. Use config profiles: Store common defaults like location/resource group.
    az config set defaults.location=eastus
    az config set defaults.group=MyRG
    
  4. Use Cloud Shell: Azure Portal includes a browser-based Cloud Shell with CLI pre-installed.
  5. Pipe to jq: For complex JSON processing, combine az with jq.

Azure CLI vs Azure PowerShell

Feature Azure CLI Azure PowerShell
Language Bash/shell native PowerShell native
Cross-platform Yes Yes
Best for Linux/macOS/CI pipelines Windows environments
Output JSON/table/tsv/yaml PowerShell objects

Conclusion

Azure CLI is an indispensable tool for any cloud developer or DevOps engineer working with Azure. Its cross-platform support, rich command set, powerful querying capabilities, and seamless CI/CD integration make it the go-to choice for automating and managing Azure infrastructure at scale.

For the full command reference, visit Azure CLI Documentation.

Found this helpful?

Share it with others who might benefit from this content.

Related Articles

Continue exploring these related topics

Want to Learn More?

Explore more articles and tutorials on software engineering, cloud technologies, and best practices. Join the community of passionate developers!