Azure
beginner
15-20 minutes

Installing the Azure CLI

Install and configure the Azure CLI for command-line deployments and resource management.

What you'll accomplish

Azure CLI installed, authenticated, and configured for your subscription

Prerequisites

  • Azure account created
  • Subscription with billing enabled

What is the Azure CLI?

The az command-line interface lets you manage Azure resources from your terminal:

bash
# Deploy a web app
az webapp up --name myapp --runtime "NODE:20-lts"

# Create a database
az sql db create --name mydb --server myserver

# View resources
az resource list --resource-group myapp-rg

The Azure CLI is:

  • Cross-platform - Windows, macOS, Linux
  • Scriptable - Automate deployments
  • Consistent - Same commands everywhere
  • Integrated - Works with Azure DevOps, GitHub Actions

Installation

Initial Configuration

Step 1: Login to Azure

bash
az login

This opens a browser window:

  1. Sign in with your Microsoft account
  2. Authorize the Azure CLI
  3. Return to terminal

For headless environments:

bash
az login --use-device-code

Step 2: Verify Authentication

bash
# List subscriptions
az account list --output table

# Output:
# Name              SubscriptionId                        State
# ----------------  ------------------------------------  -------
# Free Trial        12345678-1234-1234-1234-123456789012  Enabled

Step 3: Set Default Subscription

bash
# Set default subscription (if you have multiple)
az account set --subscription "Free Trial"

# Or use subscription ID
az account set --subscription "12345678-1234-1234-1234-123456789012"

# Verify current subscription
az account show --output table

Configuration Commands

Set Defaults

bash
# Set default resource group
az config set defaults.group=myapp-rg

# Set default location
az config set defaults.location=eastus

# View all config
az config get

Output Formats

bash
# Table format (human-readable)
az account list --output table

# JSON format (default)
az account list --output json

# TSV format (for scripting)
az account list --output tsv

# YAML format
az account list --output yaml

# Set default output
az config set core.output=table

Query Results

Use JMESPath queries to filter output:

bash
# Get just subscription names
az account list --query "[].name" --output tsv

# Get specific fields
az account list --query "[].{Name:name, ID:id, State:state}" --output table

# Filter results
az account list --query "[?state=='Enabled'].name" --output tsv

Working with Resource Groups

bash
# List resource groups
az group list --output table

# Create resource group
az group create --name myapp-dev --location eastus

# Delete resource group
az group delete --name myapp-dev --yes

# Show resource group details
az group show --name myapp-dev

Working with Resources

bash
# List all resources
az resource list --output table

# List resources in a group
az resource list --resource-group myapp-dev --output table

# Delete a resource
az resource delete --ids /subscriptions/.../resourceGroups/.../...

# Tag resources
az resource tag --tags Environment=Production --ids RESOURCE_ID

Common Service Commands

App Service

bash
# Create App Service plan
az appservice plan create \
  --name myplan \
  --resource-group myapp-rg \
  --sku B1 \
  --is-linux

# Create web app
az webapp create \
  --name myapp \
  --resource-group myapp-rg \
  --plan myplan \
  --runtime "NODE:20-lts"

# Deploy code
az webapp up --name myapp

Storage

bash
# Create storage account
az storage account create \
  --name mystorageacct \
  --resource-group myapp-rg \
  --location eastus \
  --sku Standard_LRS

# Create container
az storage container create \
  --name mycontainer \
  --account-name mystorageacct

# Upload file
az storage blob upload \
  --account-name mystorageacct \
  --container-name mycontainer \
  --name myfile.txt \
  --file ./local-file.txt

SQL Database

bash
# Create SQL server
az sql server create \
  --name myserver \
  --resource-group myapp-rg \
  --location eastus \
  --admin-user sqladmin \
  --admin-password "SecureP@ssw0rd!"

# Create database
az sql db create \
  --name mydb \
  --server myserver \
  --resource-group myapp-rg \
  --edition Basic

Extensions

Extend Azure CLI with additional modules:

bash
# List installed extensions
az extension list

# List available extensions
az extension list-available --output table

# Install extension
az extension add --name containerapp

# Update extension
az extension update --name containerapp

# Remove extension
az extension remove --name containerapp

Common extensions:

  • containerapp - Azure Container Apps
  • webapp - Additional web app features
  • interactive - Interactive mode

Interactive Mode

bash
# Enter interactive mode
az interactive

# Features:
# - Auto-completion
# - Command suggestions
# - Inline documentation

Service Principal Authentication

For scripts and CI/CD:

Create Service Principal

bash
# Create service principal with contributor role
az ad sp create-for-rbac \
  --name "myapp-deploy" \
  --role contributor \
  --scopes /subscriptions/SUBSCRIPTION_ID \
  --sdk-auth

# Output (save these securely!):
# {
#   "clientId": "xxx",
#   "clientSecret": "xxx",
#   "subscriptionId": "xxx",
#   "tenantId": "xxx"
# }

Login with Service Principal

bash
az login --service-principal \
  --username CLIENT_ID \
  --password CLIENT_SECRET \
  --tenant TENANT_ID

Use in CI/CD

Set as environment variables:

bash
export AZURE_CLIENT_ID="xxx"
export AZURE_CLIENT_SECRET="xxx"
export AZURE_TENANT_ID="xxx"
export AZURE_SUBSCRIPTION_ID="xxx"

az login --service-principal \
  -u $AZURE_CLIENT_ID \
  -p $AZURE_CLIENT_SECRET \
  --tenant $AZURE_TENANT_ID

Cloud Shell Alternative

Azure provides a browser-based shell:

  1. Go to shell.azure.com
  2. Or click the shell icon in Azure Portal
  3. Choose Bash or PowerShell

Features:

  • Pre-installed Azure CLI
  • 5 GB persistent storage
  • Integrated file editor
  • No local setup required

Update the CLI

bash
# macOS with Homebrew
brew update && brew upgrade azure-cli

# Windows with MSI
# Download new installer and run

# Linux
# Re-run the install script

Check for updates:

bash
az version
az upgrade

Troubleshooting

"az: command not found"

bash
# Check if installed
which az

# Add to PATH (Linux/macOS)
export PATH=$PATH:$HOME/bin

# Restart terminal or source profile
source ~/.bashrc

"Please run 'az login'"

bash
# Login to Azure
az login

# If browser doesn't open
az login --use-device-code

"Subscription not found"

bash
# List available subscriptions
az account list --output table

# Set correct subscription
az account set --subscription "Subscription Name"

Clear credentials and re-login

bash
# Logout
az logout

# Clear account cache
az account clear

# Login fresh
az login

Command Reference

Account Management

bash
az account list              # List subscriptions
az account show              # Current subscription
az account set               # Switch subscription
az login                     # Authenticate
az logout                    # Sign out

Resource Management

bash
az group list                # List resource groups
az group create              # Create resource group
az group delete              # Delete resource group
az resource list             # List resources
az resource delete           # Delete resource

Configuration

bash
az config get                # View config
az config set                # Set config value
az config unset              # Remove config value
az configure                 # Interactive configuration

Help

bash
az --help                    # General help
az webapp --help             # Service-specific help
az webapp create --help      # Command-specific help
az find "deploy web app"     # Search for commands
Note

Checkpoint: Run az account show to verify you're logged in and connected to the correct subscription.