Azure Zero to Hero: Day 10 — Automate Azure Resources using Azure CLI
This chapter covers the fundamentals of the Azure Command Line Interface (Azure CLI). You will learn why DevOps engineers rely on CLI over the graphical Azure Portal for automation and quick operations, how to install and configure it across operating systems, and how to programmatically manage resource groups, virtual networks, virtual machines, and storage accounts.
1. Why Do We Need Azure CLI? (Problem Statement vs. Status Quo)
Up to Day 9 of the series, all resource provisioning on Azure was performed exclusively through the Azure Console (Graphical User Interface). While the Azure Portal offers an intuitive user experience for beginners, it introduces severe limitations in enterprise DevOps environments:
- The Repetitive Task Bottleneck: Provisioning a multi-tier stack (e.g., an AKS cluster, virtual network, and file storage) manually via the console takes roughly 30 minutes. Repeating this exact workflow across 10 different projects wastes hours of engineering bandwidth.
- Human Error & Configuration Drift: Clicking through dozens of configuration wizards increases the risk of selecting incorrect parameters (e.g., choosing a non-standard OS image or missing a subnet binding). Debugging these hidden configuration drifts is time-consuming.
- Auditability and Sharing: Manual UI actions leave behind no easily shareable, version-controlled blueprint for team members or new hires joining a project.
Real-Life Analogy: Restaurant Ordering vs. Automated Meal Kits
Navigating the Azure Console is like walking up to a restaurant counter and ordering a custom meal item-by-item every single time you eat. It is great for a first-time visitor, but inefficient if you need 50 identical meals delivered across different branches. Azure CLI is like a standardized recipe and delivery script: you write it once, store it in a Git repository, and execute it identically in minutes whenever needed.
2. Core Mental Model: Quick Actions & Automation
DevOps engineers leverage Azure CLI primarily for two distinct operational vectors:
- Quick Operational Inspection: Instead of logging into the portal, navigating to resource groups, and searching across services, engineers use fast CLI commands (
az ...) to query resources across projects instantly. - Infrastructure as Scripts (IaCs light): Packaging operational commands into shell scripts and checking them into version control (Git) so that any team member can provision identical environments safely.
3. Naive UI Approach vs. Automated CLI Workflow
4. Step-by-Step Installation & Configuration
Azure CLI can be installed on Windows, macOS, and Linux distributions.
A. Windows Installation
- Navigate to the official Azure CLI documentation.
- Select Windows, then choose the recommended Microsoft Installer (MSI) 64-bit package.
- Run the downloaded
.msifile, accept prompts, and complete the installation wizard.- Note: While PowerShell is the recommended shell for Windows environments due to Microsoft's deep platform integration, Command Prompt or Git Bash are also supported.
B. macOS Installation
Open your terminal and run Homebrew commands to update packages and install Azure CLI:
brew update
brew install azure-cli
C. Linux (Ubuntu / Debian) Installation
Execute the Microsoft package repository installation script via apt:
curl -sL https://aka.msi/InstallAzureCLIDeb | sudo bash
D. Verifying Installation & Authenticating
Once installed, verify the binary is active on your system path by checking its version:
az version
Next, authenticate your local CLI session with your Azure account:
az login
Behavior: Executing az login opens a browser window prompting you to sign in with your Azure credentials. Once authenticated, the CLI securely links to your active subscription (e.g., Free Trial).
5. Step-by-Step Resource Provisioning Workflow
Before creating resources like virtual machines, you must understand dependencies. For instance, a virtual machine requires a Resource Group and a Virtual Network (VNet). Attempting to create a VM without its parent resource group will result in an API error.
Step 5.1: Create a Resource Group
Create a dedicated resource group to neatly organize all practice resources for easy cleanup later:
az group create \
--name learning-azure-cli \
--location eastus
Step 5.2: Create a Virtual Machine & Virtual Network
Use the composite az vm create command, which automatically provisions an underlying virtual network, subnet, and network security group alongside the compute instance:
az vm create \
--resource-group learning-azure-cli \
--name test-cli-vm \
--image Ubuntu2204 \
--vnet-name test-cli-vnet \
--subnet example-subnet \
--generate-ssh-keys
--generate-ssh-keys: Automatically checks your local machine (~/.ssh/) for existing public keys. If none are found, it generates a fresh SSH key pair, injecting the public key into the remote Linux VM for secure access.
6. Complete Flow Walkthrough: Verification & Cleanup
Inspecting Created Resources
After execution completes, verify the resources exist via CLI or check the Azure Portal:
# List all resources inside the resource group in table format
az resource list --resource-group learning-azure-cli --output table
Deleting Resources (Cleanup)
To avoid incurring unwanted charges on your Azure subscription, you can delete the entire resource group and all its nested child resources with a single command:
az group delete --name learning-azure-cli --yes --no-wait
--yes: Automatically bypasses confirmation prompts (ideal for CI/CD pipelines).--no-wait: Triggers the deletion asynchronously, returning control to your terminal immediately without blocking execution.
7. Storage Account Provisioning Example
To provision an Azure Storage Account using CLI:
- Recreate the resource group if it was deleted:
BASH
az group create --name learning-azure-cli --location eastus - Create the storage account:
BASH
az storage account create \ --name testclistorageacct \ --resource-group learning-azure-cli \ --location eastus \ --sku Standard_LRS \ --kind StorageV2
8. Real-World DevOps Use Cases & Best Practices
- Version Control Integration: Store your
azcommand scripts inside a team Git repository (e.g., under adeploy/orscripts/folder). When a developer receives a Jira ticket for environment setup, they clone the repo and execute the script. - Referencing Documentation: Bookmark the official Azure CLI A-Z Article List. Whenever you need to provision a new service (e.g., Azure Kubernetes Service), search for
az aksin the documentation to review required flags. - Handling Permissions: Ensure your authenticated user or Service Principal possesses adequate Role-Based Access Control (RBAC) permissions (such as Contributor) over the target subscription before running provisioning commands.
Summary and Key Takeaways
- Operational Efficiency: Azure CLI allows DevOps engineers to perform lightning-fast resource inspections and execute batch provisioning without navigating slow graphical user interfaces.
- Automation & Repeatability: CLI scripts eliminate manual UI configuration errors and standardize deployments across teams and projects.
- Git-Backed Runbooks: Storing CLI commands in Git repositories creates reliable, shareable infrastructure templates for new team members.
- Installation Flexibility: Azure CLI installs smoothly on Windows (PowerShell/CMD), macOS (Homebrew), and Linux (
apt/yum). - Authentication: Running
az loginbinds your local terminal session to your specific Azure cloud subscription securely. - Dependency Awareness: Always provision parent resources (such as Resource Groups and VNets) before child resources (such as VMs or subnets).
- SSH Key Management: The
--generate-ssh-keysflag automates public/private key provisioning for secure Linux virtual machine access. - Safe Cleanup: Use
az group deleteto purge entire resource stacks instantly and prevent accidental billing on trial accounts.