Azure 3: Azure CLI Essentials
easy⏱ 5 mincourseazure
Azure CLI Structure
Every Azure CLI command follows the pattern: az <group> <subcommand> --<param> <value>. Groups map to Azure services: az vm for Virtual Machines, az storage for Storage, az webapp for App Service, etc. Common flags include --resource-group (-g), --name (-n), --location (-l), and --output (-o) for format (table, json, tsv).
// Common Azure CLI commands
az login // authenticate
az account list --output table // list subscriptions
az group create -n myRG -l eastus // create resource group
az vm create -g myRG -n myVM --image Ubuntu2204
Build an Azure CLI command builder
Create a class AzCli with a command method that takes a group (e.g., 'vm') and subcommand (e.g., 'create'). Chain .param(key, value) calls to add parameters. A .build() method returns the full command string. Use it to build a vm create command and log it.
Use --query for JMESPath filtering
The --query flag uses JMESPath to filter JSON output. For example: az vm list --query "[].{Name:name, State:powerState}" returns only name and state fields. Combined with --output table, this gives you clean, readable output.
Quiz: CLI Command Structure
What is the correct CLI command to create a resource group named 'rg-dev' in West US? Answer: az group create --name rg-dev --location westus. The group is group, subcommand is create, and required params are --name and --location.