Azure CLI essentials

Microsoft Azure · Essentials · 12 min

The CLI is the fastest way to explore Azure and the glue of most scripts and pipelines.

The Azure CLI (az) is a cross-platform command line for everything in Azure Resource Manager. Commands follow az <group> <subgroup> <verb>: az storage account create, az vm list, az webapp log tail.

Getting started:

  • az login opens a browser (or use --use-device-code); az account show prints the current subscription; az account set -s <name or id> switches.
  • az configure --defaults group=rg-demo location=westeurope saves you from repeating -g and -l.
  • Every command has --help, and az find "create storage" suggests examples.

Output is JSON by default. -o table, -o tsv and -o yaml change the format, and --query takes a JMESPath expression to filter and reshape it, client side. -o tsv with a single field is the idiom for capturing a value in a shell variable.

az rest calls any ARM REST endpoint with your login token, which is handy for brand-new API features the CLI doesn't wrap yet. Cloud Shell in the portal has az preinstalled and already logged in.

In automation, never use a personal login: use a service principal or, better, a managed identity or workload identity federation, covered later.

Example

az login
az account set --subscription "Contoso Dev"
az configure --defaults group=rg-demo location=westeurope

az group create --name rg-demo
az storage account create --name stdemo$RANDOM --sku Standard_LRS

# JMESPath: names and SKUs of every storage account
az storage account list --query "[].{name:name, sku:sku.name}" -o table

# Capture one value into a variable
ID=$(az storage account list --query "[0].id" -o tsv)
echo "$ID"   # /subscriptions/.../resourceGroups/rg-demo/providers/Microsoft.Storage/...

Remember

  • Commands read az <group> <verb>; --help on anything
  • --query (JMESPath) filters output; -o tsv captures single values
  • az configure --defaults removes repetitive -g/-l flags

Watch out for

  • Parsing JSON output with grep or sed instead of using --query.

Check yourself

  1. Which command prints only the names of all resource groups, one per line?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: learn.microsoft.com/en-us/cli/azure/query-azure-cli