Azure Command-Line Interface (CLI) Documentation

Introduction

The Azure CLI is a powerful, cross-platform command-line tool for managing Azure resources. It allows you to interact with Azure services through a simple command-line interface. Whether you're creating virtual machines, managing storage accounts, or deploying applications, the Azure CLI provides a streamlined way to perform these tasks.

Key features include:

  • Extensive support for Azure services.
  • Scripting and automation capabilities.
  • Cross-platform compatibility (Windows, macOS, Linux).
  • Interactive mode and auto-completion.

Installation

Install the Azure CLI on your preferred operating system to start managing your Azure resources.

Windows

Download the MSI installer from the official Microsoft documentation.

macOS

Install using Homebrew:

brew update && brew upgrade
brew install azure-cli

Linux

Use the script provided by Microsoft:

curl -sL https://aka.ms/InstallAzureCLIDebian | sudo bash

For other Linux distributions, please refer to the official installation guide.

Getting Started

After installation, log in to your Azure account:

Log in to Azure

az login

This command will open a browser window for you to authenticate. Once logged in, you can start executing commands.

To see your active subscription:

Show current subscription

az account show --output table

Managing Resources

The Azure CLI is organized into commands and subgroups. For example, to manage resource groups, you use the az group command.

Resource Groups

Resource groups are logical containers for your Azure resources.

Create a resource group:

Create a resource group named 'myResourceGroup' in the 'eastus' region.

az group create --name myResourceGroup --location eastus

List all resource groups:

List resource groups with output as a table.

az group list --output table

Virtual Machines

Manage your Azure Virtual Machines (VMs).

Create a VM:

Create a Ubuntu VM named 'myVM' in 'myResourceGroup' using a standard B1s size.

az vm create \
  --resource-group myResourceGroup \
  --name myVM \
  --image Ubuntu2204 \
  --admin-username azureuser \
  --generate-ssh-keys

List all VMs in a resource group:

List VMs in 'myResourceGroup'.

az vm list --resource-group myResourceGroup --output table

Storage

Work with Azure Storage accounts.

Create a storage account:

Create a storage account with a unique name in 'eastus'.

az storage account create \
  --name mystorageaccount[unique_name] \
  --resource-group myResourceGroup \
  --location eastus \
  --sku Standard_LRS

Databases

Manage Azure SQL databases.

Create an Azure SQL server and database:

Create an Azure SQL server and a simple database.

az sql server create \
  --name mysqlexampleserver \
  --resource-group myResourceGroup \
  --location eastus \
  --admin-user sqladmin \
  --admin-password 

az sql db create \
  --resource-group myResourceGroup \
  --server mysqlexampleserver \
  --name mySampleDatabase \
  --edition Basic

Deployment

Deploy applications and infrastructure to Azure.

Deploy an ARM template:

Deploy a local ARM template named 'azuredeploy.json'.

az deployment group create \
  --resource-group myResourceGroup \
  --template-file azuredeploy.json

Automation

Automate your Azure tasks using scripts.

Tip: You can combine multiple CLI commands in a single script to automate complex workflows.

Example: Create a resource group and then a storage account within it:

# Create Resource Group
az group create --name automationRG --location westus2 --output none

# Create Storage Account
az storage account create \
  --name mystorageauto[unique_name] \
  --resource-group automationRG \
  --location westus2 \
  --sku Standard_RAGRS \
  --output none

echo "Resource group 'automationRG' and storage account created successfully."

Authentication

The Azure CLI supports several methods for authentication:

  • Interactive Login: az login (used above)
  • Service Principal: For automated scenarios.
  • Managed Identity: For Azure resources accessing other Azure resources.

To authenticate using a service principal:

Login with service principal credentials.

az login --service-principal -u  -p  --tenant