What it is
The Azure CLI is a command-line tool for managing Azure resources, enabling you to interact with Azure services from your terminal.
Installation
Linux
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
# Or for RPM-based systems:
curl -sL https://aka.ms/InstallAzureCLIRpm | sudo bash
macOS
brew update && brew install azure-cli
Windows
Download and run the MSI installer from the official Azure CLI documentation. Alternatively, you can install it via Chocolatey:
choco install azure-cli
Login
To use the Azure CLI, you first need to log in to your Azure account:
az login
This will open a browser window for authentication.
Core Concepts
- Resource Groups: Logical containers for Azure resources. Resources must belong to a resource group.
- Resources: Individual Azure services like virtual machines, storage accounts, and web apps.
- Subscriptions: An Azure subscription is a logical grouping of resources that is associated with an Azure account.
- Locations: Azure regions where your resources are deployed (e.g.,
eastus,westeurope). - Resource IDs: Unique identifiers for Azure resources.
Commands / Usage
Account Management
-
List subscriptions:
az account list --output tableLists all Azure subscriptions accessible by your logged-in account.
-
Set default subscription:
az account set --subscription "My Azure Subscription Name or ID"Sets the active subscription for subsequent commands.
-
Show current account details:
az account showDisplays information about the currently active subscription.
Resource Group Management
-
Create a resource group:
az group create --name myResourceGroup --location eastusCreates a new resource group named
myResourceGroupin theeastuslocation. -
List resource groups:
az group list --output tableLists all resource groups in your current subscription.
-
Show resource group details:
az group show --name myResourceGroupDisplays details about a specific resource group.
-
Delete a resource group:
az group delete --name myResourceGroup --yesDeletes a resource group and all resources within it.
--yesconfirms deletion without prompting.
Virtual Machine Management
-
Create a virtual machine:
az vm create \ --resource-group myResourceGroup \ --name myVM \ --image UbuntuLTS \ --admin-username azureuser \ --generate-ssh-keysCreates a new Ubuntu LTS virtual machine named
myVMinmyResourceGroup, using SSH key-based authentication. -
List virtual machines:
az vm list --resource-group myResourceGroup --output tableLists all virtual machines in a specified resource group.
-
Show virtual machine details:
az vm show --resource-group myVMResourceGroup --name myVM --show-detailsDisplays detailed information about a specific virtual machine.
-
Start a virtual machine:
az vm start --resource-group myVMResourceGroup --name myVMStarts a stopped virtual machine.
-
Stop a virtual machine:
az vm stop --resource-group myVMResourceGroup --name myVMStops a running virtual machine (deallocates it).
-
Restart a virtual machine:
az vm restart --resource-group myVMResourceGroup --name myVMRestarts a virtual machine.
-
Delete a virtual machine:
az vm delete --resource-group myVMResourceGroup --name myVM --yesDeletes a virtual machine.
Storage Account Management
-
Create a storage account:
az storage account create \ --resource-group myResourceGroup \ --name mystorageaccount123 \ --location eastus \ --sku Standard_LRSCreates a standard locally-redundant storage account named
mystorageaccount123inmyResourceGroup. -
List storage accounts:
az storage account list --resource-group myResourceGroup --output tableLists all storage accounts in a resource group.
-
Show storage account keys:
az storage account keys list --resource-group myResourceGroup --account-name mystorageaccount123Retrieves the access keys for a storage account.
-
Delete a storage account:
az storage account delete --resource-group myResourceGroup --name mystorageaccount123 --yesDeletes a storage account.
Web App Management
-
Create an App Service Plan:
az appservice plan create \ --resource-group myResourceGroup \ --name myAppServicePlan \ --sku F1 \ --is-linuxCreates a free-tier Linux App Service plan.
-
Create a Web App:
az webapp create \ --resource-group myResourceGroup \ --plan myAppServicePlan \ --name mywebapp123 \ --runtime "python|3.9"Creates a Python 3.9 web app named
mywebapp123undermyAppServicePlan. -
List Web Apps:
az webapp list --resource-group myResourceGroup --output tableLists all web apps in a resource group.
-
Deploy to a Web App (from local directory):
az webapp deploy \ --resource-group myResourceGroup \ --name mywebapp123 \ --src-path /path/to/your/app/contentDeploys content from a local directory to a web app.
-
Delete a Web App:
az webapp delete --resource-group myResourceGroup --name mywebapp123 --yesDeletes a web app.
Azure Kubernetes Service (AKS)
-
Create an AKS cluster:
az aks create \ --resource-group myResourceGroup \ --name myAKSCluster \ --node-count 1 \ --enable-addons monitoring \ --generate-ssh-keysCreates an AKS cluster with one node and enables monitoring.
-
List AKS clusters:
az aks list --resource-group myResourceGroup --output tableLists all AKS clusters in a resource group.
-
Get AKS credentials:
az aks get-credentials --resource-group myResourceGroup --name myAKSClusterDownloads credentials for an AKS cluster and configures
kubectlto connect to it. -
Delete an AKS cluster:
az aks delete --resource-group myResourceGroup --name myAKSCluster --yesDeletes an AKS cluster.
Networking
-
Create a virtual network:
az network vnet create \ --resource-group myResourceGroup \ --name myVNet \ --address-prefix 10.0.0.0/16 \ --subnet-name mySubnet \ --subnet-prefix 10.0.0.0/24Creates a virtual network and a subnet.
-
Create a public IP address:
az network public-ip create \ --resource-group myResourceGroup \ --name myPublicIP \ --allocation-method Static \ --sku StandardCreates a static, standard SKU public IP address.
-
Create a Network Security Group (NSG):
az network nsg create \ --resource-group myResourceGroup \ --name myNSGCreates an empty network security group.
-
Add a security rule to an NSG:
az network nsg rule create \ --resource-group myResourceGroup \ --nsg-name myNSG \ --name AllowHTTP \ --priority 100 \ --protocol Tcp \ --destination-port-range 80 \ --access Allow \ --direction InboundAdds a rule to allow inbound TCP traffic on port 80.
Querying and Output
-
Show resource JSON:
az vm show --resource-group myVMResourceGroup --name myVM --query "{Name:name, OS:storageProfile.osDisk.osType, State:powerState}"Retrieves specific properties of a resource using JMESPath queries.
-
Output formats:
az vm list --output json az vm list --output table az vm list --output tsv az vm list --output yamlSpecifies the output format for commands.
Scripting and Automation
-
Template deployment:
az deployment group create \ --resource-group myResourceGroup \ --template-file azuredeploy.json \ --parameters azuredeploy.parameters.jsonDeploys resources using an ARM template.
-
List deployments:
az deployment group list --resource-group myResourceGroup --output tableLists all deployments in a resource group.
Azure CLI Extensions
-
List installed extensions:
az extension listLists all installed Azure CLI extensions.
-
Install an extension:
az extension add --name azure-iotInstalls the
azure-iotextension. -
Update extensions:
az extension update --upgrade-allUpdates all installed extensions.
Other Useful Commands
-
Show Azure CLI version:
az versionDisplays the installed Azure CLI version and its components.
-
Show help for a command:
az vm --help az vm create --helpDisplays help documentation for a command or subcommand.
-
List all commands:
az find "virtual machine"Searches for commands related to a keyword.
Common Patterns
-
Create a resource group and deploy a web app in one go:
az group create --name myWebAppRG --location westus2 && az appservice plan create --resource-group myWebAppRG --name myPlan --sku B1 --is-linux && az webapp create --resource-group myWebAppRG --plan myPlan --name myuniqueappname --runtime "python|3.10"Creates a resource group, an App Service plan, and a web app sequentially.
-
Get the public IP of a VM and SSH into it:
VM_IP=$(az vm show -g myVMResourceGroup -n myVM -d --query publicIps -o tsv) && ssh azureuser@$VM_IPRetrieves the public IP address of a VM and then uses it to SSH.
-
Deploy a static web app from a local folder:
az storage blob upload-batch \ --account-name mystorageaccount123 \ --destination '$web' \ --source /path/to/your/static/siteUploads all files from a local directory to the
$webcontainer for static website hosting. -
List all resources of a specific type in a subscription:
az resource list --resource-type "Microsoft.Compute/virtualMachines" --output tableLists all virtual machines across all resource groups in the current subscription.
-
Delete all resources in a resource group:
az group delete --name myResourceGroup --yes --no-waitDeletes a resource group and all its contents without waiting for completion.
-
Check for available VM sizes in a region:
az vm list-sizes --location eastus --output tableLists all available VM sizes in a specific Azure region.
Gotchas
- Resource Naming: Many Azure resources have globally unique naming requirements (e.g., storage accounts, web apps). If a name is taken, the command will fail.
- Permissions: Ensure the account you are logged in with (
az login) has the necessary permissions to perform the requested operations. Role-Based Access Control (RBAC) is crucial. - Default Resource Group: If you don’t specify
--resource-groupfor a resource creation command, the CLI might default to a resource group if one is already set as default in your configuration, or it might prompt you or fail. It’s best practice to always explicitly specify the resource group. --yesFlag: The--yesflag bypasses confirmation prompts for destructive operations (like deleting resource groups or VMs). Use with caution.--no-waitFlag: Commands ending with--no-waitreturn immediately without waiting for the operation to complete on Azure. This is useful for scripting but means the resource might not be fully provisioned when the command returns.- Case Sensitivity: Resource names are generally case-insensitive, but some commands or parameters might be case-sensitive.
- Extension Management: Extensions are separate from the core CLI and might have their own dependencies or update cycles. Always ensure extensions are up-to-date if you encounter issues.
- Location Specificity: Many commands require a
--locationparameter. If omitted, it might use a default or prompt. Always specify the desired region for consistency and predictability. - Output Parsing: While
--output tableand--output tsvare human-readable, for scripting,--output jsonor--output tsvare generally preferred. Be mindful of whitespace and formatting when parsing output. - Idempotency: Many
createcommands are not idempotent. Runningaz vm createtwice with the same name and parameters will result in an error on the second attempt, as the resource already exists. Useaz vm showoraz vm updatefor modifications.