Mastering the Azure CLI Command "az" (with examples)
The Azure CLI command, denoted as az
, is a powerful tool that enables users to manage Azure resources directly from the command line. It is the official command-line interface for interacting with Microsoft Azure services, providing a vast array of subcommands to perform various tasks, ranging from managing virtual machines to handling complex Azure Kubernetes Services. For developers, IT professionals, and anyone working with Azure, mastering the az
command is crucial for efficient resource management and automation. This article will walk you through practical use cases of the az
command, providing examples and detailed explanations.
Log in to Azure
Code:
az login
Motivation:
Before you can begin using Azure services through the CLI, you need to authenticate your access. The az login
command is essential because it establishes a connection between your local CLI and Azure subscription. This authentication process usually opens a browser window where you can enter your Azure credentials, ensuring secure communication with your cloud resources.
Explanation:
The az login
command does not require additional arguments in its simplest form. It operates in interactive mode, prompting you to provide credentials through a browser interface. Optionally, you can use additional flags to automate login in non-interactive environments, suitable for scripts and automation scenarios.
Example output:
[
{
"cloudName": "AzureCloud",
"homeTenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"id": "YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY",
"isDefault": true,
"name": "Free Trial",
"state": "Enabled",
"tenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"user": {
"name": "user@example.com",
"type": "user"
}
}
]
Manage Azure Subscription Information
Code:
az account
Motivation:
Azure operates on a subscription model, where each account can have multiple subscriptions for organizing resources. The az account
command is vital for users who need to manage or switch between different subscriptions, ensuring that actions are performed within the correct resource context.
Explanation:
The command az account
works as a reference point for managing subscriptions under your Azure account. It allows further subcommands like list
to view available subscriptions, set
to switch the active subscription, and show
to display details of the current subscription.
Example output:
Command group 'account' is in preview. It may be changed/removed in a future release.
Usage: az account [sub-commands] [arguments]
- az account list
- az account show --subscription <name or id>
- az account set --subscription <name or id>
List all Azure Managed Disks
Code:
az disk list
Motivation:
Managing storage is a fundamental aspect of cloud resource management. The az disk list
command is crucial for users who need an overview of all managed disks in their Azure environment. It aids in monitoring disk usage, verifying configurations, and auditing operations.
Explanation:
The az disk list
command lists all managed disks associated with the active Azure subscription. Additional flags and options can be added to filter results, such as specifying a resource group with --resource-group <name>
to limit the output to disks within a specific group.
Example output:
[
{
"id": "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/ExampleGroup/providers/Microsoft.Compute/disks/ExampleDisk",
"location": "eastus",
"managedBy": null,
"name": "ExampleDisk",
"provisioningState": "Succeeded",
"resourceGroup": "ExampleGroup"
}
]
List all Azure Virtual Machines
Code:
az vm list
Motivation:
Virtual machines (VMs) are a core component of Azure’s Infrastructure as a Service (IaaS) offering, used for hosting applications, running databases, and more. The az vm list
command is important for administrators who need to manage and monitor their VM instances efficiently across their Azure environment.
Explanation:
Executing az vm list
retrieves all virtual machines within the current Azure subscription. By default, it provides a list with detailed information about each VM. To filter results or narrow down the query, one can use flags like --resource-group <name>
to display VMs from a specific resource group only.
Example output:
[
{
"id": "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/ExampleGroup/providers/Microsoft.Compute/virtualMachines/ExampleVM",
"location": "westus",
"name": "ExampleVM",
"osProfile": {
"adminUsername": "azureuser",
"computerName": "examplevm"
},
"resourceGroup": "ExampleGroup",
"vmId": "ZZZZZZZZ-ZZZZ-ZZZZ-ZZZZ-ZZZZZZZZZZZZ"
}
]
Manage Azure Kubernetes Services
Code:
az aks
Motivation:
Azure Kubernetes Service (AKS) simplifies deploying, managing, and operating Kubernetes clusters. The az aks
command is pivotal for developers and IT professionals working with containerized applications, allowing them to create, update, and manage their Kubernetes environments more efficiently.
Explanation:
The az aks
command works as a suite of subcommands for various AKS tasks, such as create
to set up a new Kubernetes cluster, list
to view existing clusters, and delete
to remove clusters that are no longer needed. Each subcommand may require specific options like --name
for naming resources and --resource-group
to specify a resource context.
Example output:
Group
az aks : Manage Azure Kubernetes Services.
Subcommands
create : Create a new managed Kubernetes cluster.
delete : Delete a managed Kubernetes cluster.
list : List managed Kubernetes clusters.
show : Show the details of a managed Kubernetes cluster.
Manage Azure Network Resources
Code:
az network
Motivation:
Network management is a core task in organizing cloud resources, and the az network
command provides comprehensive control over networking elements within Azure. This command is particularly useful for network engineers and administrators who need to set up, configure, and optimize their Azure network environments.
Explanation:
The az network
command encompasses a broad range of subcommands to deal with Azure network resources, such as virtual networks, subnets, public IP addresses, and more. It includes operations like vnet
for managing virtual networks, nsg
for network security groups, and nic
for network interfaces. Each of these subcommands can be used with specific options to make meaningful modifications.
Example output:
Group
az network : Manage Azure Network resources.
Subcommands
vnet : Manage virtual networks.
nsg : Manage network security groups (NSG).
nic : Manage network interfaces.
public-ip : Manage public IP addresses.
Start in Interactive Mode
Code:
az interactive
Motivation:
For users new to Azure CLI or those looking to explore its features in a more guided way, the az interactive
command offers an exploratory environment. This interactive mode helps users understand command structures, providing real-time suggestions and auto-completions as they type.
Explanation:
The az interactive
command starts an interactive shell session tailored to Azure CLI operations. Within this session, users receive automated suggestions and inline help, facilitating easier and faster command execution and learning experience.
Example output:
Azure CLI Interactive Shell loaded.
Type `az find` to explore the commands.
Press `tab` to see available commands or auto-complete options.
Display Help
Code:
az --help
Motivation:
With a vast array of commands and options available in Azure CLI, understanding the functionality and syntax can be overwhelming. The az --help
command is indispensable for users of all proficiency levels who need instant guidance on command usage, options, and syntax.
Explanation:
By using the az --help
command, users can access the help documentation directly from the command line. It provides a brief overview of all available commands and flags within the Azure CLI, serving as a quick reference guide to understand the command-line interface’s capabilities.
Example output:
Welcome to the cool AI powered Azure CLI!
Here are the base command groups:
account : Manage Azure subscription information.
aks : Manage Azure Kubernetes Services.
disk : Manage Azure Managed Disks.
login : Log in to Azure.
network : Manage Azure Network resources.
Conclusion:
The az
command-line tool is an essential utility for anyone working extensively with Azure. From logging in to managing subscriptions, VMs, Kubernetes services, and networks, the command provides tools to efficiently manage, deploy, and automate cloud operations. By mastering these use cases and examples, users can significantly enhance their productivity and control over Azure resources, leveraging the full potential of cloud computing.