Get started
Cheatsheet

Azure

The Azure CLI commands for sign-in, groups, storage, VMs, networks, access, Key Vault, templates and clean-up, with the options you forget.

Practice this path

Sign in and context

  • az login

    Sign in (browser or device code on a real machine)

  • az account show --query name -o tsv

    The name of the active subscription, as plain text

  • az account list -o table

    Every subscription you can use, and which one is the default

  • az account set --subscription "Dev Sandbox"

    Make another subscription the default (name or id)

  • az account list-locations --query "[].name" -o tsv

    The region names you can use with --location

  • az configure --defaults group=rg-app location=centralindia

    Save a default resource group and region

  • az configure --list-defaults

    See which defaults are set

  • az logout

    Sign out

Output and --query

  • az group list -o table

    A readable table (json is the default)

  • az group list --query "[].name" -o tsv

    One value per line, ready for scripts

  • az resource list --query "[?type=='Microsoft.Storage/storageAccounts'].name" -o tsv

    Filter with a JMESPath condition

  • az vm list --query "[].{name:name, size:hardwareProfile.vmSize}" -o table

    Rename and pick fields into a new object

  • az vm list-sizes --query "length(@)" -o tsv

    Count the items of a result

  • az group show -n rg-app -o yaml

    The same data as YAML

  • az <group> <command> --help

    Options and examples for any command

Resource groups and tags

  • az group create -n rg-app -l centralindia --tags env=dev owner=asha

    Create a resource group with tags

  • az group update -n rg-app --set tags.costcenter=cc-1042

    Change one tag (--tags replaces the whole set)

  • az group list --tag env=test --query "[].name" -o tsv

    Find groups by tag

  • az group exists -n rg-app

    Prints true or false

  • az resource list -g rg-app -o table

    Everything inside a group

  • az resource tag --tags costcenter=training --ids $(az resource list -g rg-app --query "[].id" -o tsv)

    Tag many resources at once

  • az group delete -n rg-app --yes --no-wait

    Delete the group and everything in it

Storage

  • az storage account check-name --name labstore42x

    Is the (globally unique) name free and valid?

  • az storage account create -n labstore42x -g rg-app --sku Standard_LRS --kind StorageV2

    Create a storage account

  • az storage container create -n docs --account-name labstore42x

    Create a private container

  • az storage blob upload -c docs -f a.txt -n a.txt --account-name labstore42x --auth-mode login

    Upload one file, authorised by your Microsoft Entra identity

  • az storage blob upload-batch -s site -d docs --destination-path site --account-name labstore42x

    Upload a whole folder

  • az storage blob list -c docs --account-name labstore42x --query "[].name" -o tsv

    List blob names

  • az storage blob download -c docs -n a.txt -f copy.txt --account-name labstore42x

    Download a blob

  • az storage blob generate-sas -c docs -n a.txt --permissions r --expiry 2030-01-01T00:00Z --full-uri --account-name labstore42x

    A read-only link that expires

Virtual machines

  • az vm create -g rg-vm -n vm-web --image Ubuntu2204 --size Standard_B1s --generate-ssh-keys

    Create a Linux VM and its disk, NIC, IP and NSG

  • az vm list -d -o table

    VMs with power state and public IP

  • az vm show -d -g rg-vm -n vm-web --query publicIps -o tsv

    The public IP address only

  • az vm stop -g rg-vm -n vm-web

    Power off (still billed for compute)

  • az vm deallocate -g rg-vm -n vm-web

    Release compute so billing stops

  • az vm start -g rg-vm -n vm-web

    Start it again

  • az vm resize -g rg-vm -n vm-web --size Standard_B2s

    Change the size

  • az vm delete -g rg-vm -n vm-web --yes

    Delete the VM (its disk, NIC and IP stay)

Virtual networks and NSGs

  • az network vnet create -g rg-net -n vnet-hub --address-prefixes 10.10.0.0/16 --subnet-name snet-web --subnet-prefixes 10.10.1.0/24

    A virtual network with its first subnet

  • az network vnet subnet create -g rg-net --vnet-name vnet-hub -n snet-app --address-prefixes 10.10.2.0/24

    Add a subnet

  • az network vnet update -g rg-net -n vnet-hub --address-prefixes 10.10.0.0/16 10.11.0.0/16

    Change the address space (give the whole list)

  • az network nsg create -g rg-sec -n nsg-web

    A network security group

  • az network nsg rule create -g rg-sec --nsg-name nsg-web -n allow-https --priority 100 --protocol Tcp --destination-port-ranges 443

    Allow HTTPS in (lower priority number is checked first)

  • az network nsg rule list -g rg-sec --nsg-name nsg-web --include-default -o table

    Your rules and the built-in ones

  • az network vnet subnet update -g rg-sec --vnet-name vnet-app -n snet-web --network-security-group nsg-web

    Attach an NSG to a subnet

Roles and access

  • az role definition list --name Reader --query "[0].permissions[0].actions"

    What a built-in role allows

  • az ad signed-in-user show --query id -o tsv

    Your own object id

  • az role assignment create --assignee user@contoso.com --role Reader --scope $(az group show -n rg-app --query id -o tsv)

    Give a role at a resource group scope

  • az role assignment list --scope <id> -o table

    Who has what at a scope

  • az role assignment delete --assignee user@contoso.com --role Reader --scope <id>

    Take access away

Key Vault

  • az keyvault create -n kvlab-42x -g rg-vault -l centralindia

    Create a vault (RBAC authorization is the default)

  • az role assignment create --assignee <id> --role "Key Vault Secrets Officer" --scope $(az keyvault show -n kvlab-42x --query id -o tsv)

    Allow yourself to read and write secrets

  • az keyvault secret set --vault-name kvlab-42x -n db-password --value '...' --expires 2030-12-31T00:00:00Z

    Store a secret (setting again adds a version)

  • az keyvault secret show --vault-name kvlab-42x -n db-password --query value -o tsv

    Read the value

  • az keyvault secret list-versions --vault-name kvlab-42x -n db-password -o table

    Every version of a secret

  • az keyvault secret delete --vault-name kvlab-42x -n db-password

    Soft delete (recoverable)

  • az keyvault secret recover --vault-name kvlab-42x -n db-password

    Bring a deleted secret back

Template deployments

  • az deployment group validate -g rg-iac --template-file azuredeploy.json

    Check a template without deploying it

  • az deployment group what-if -g rg-iac --template-file azuredeploy.json

    Preview Create (+), Modify (~) and Delete (-)

  • az deployment group create -g rg-iac --template-file azuredeploy.json --parameters @dev.parameters.json

    Deploy with a parameters file

  • az deployment group create -g rg-iac -f t.json --parameters env=prod sku=Standard_GRS

    Pass single parameters as name=value

  • az deployment group create -g rg-iac -f t.json --mode Complete

    Also delete anything not in the template (run what-if first)

  • az deployment group show -g rg-iac -n azuredeploy --query properties.outputs

    Read a deployment's outputs

  • az deployment group list -g rg-iac -o table

    Deployment history

Locks and clean-up

  • az lock create -n keep --lock-type CanNotDelete -g rg-prod --resource-name mystore --resource-type Microsoft.Storage/storageAccounts

    Protect one resource from deletion

  • az lock list -o table

    All locks in the subscription

  • az lock delete -n keep -g rg-prod --resource-name mystore --resource-type Microsoft.Storage/storageAccounts

    Remove a lock

  • az disk list --query "[?managedBy==null].name" -o tsv

    Disks no VM uses

  • az resource list --query "[?tags.costcenter==null].name" -o tsv

    Resources missing a cost tag

  • az keyvault list-deleted -o table

    Soft-deleted vaults that still hold their name

Cheatsheet for the Azure path · Deboistech · All cheatsheets