This tutorial guides you through the fundamental commands and workflows of the Azure Command-Line Interface (CLI) to help you manage Azure resources efficiently.
Introduction to Azure CLI
The Azure CLI is a cross-platform command-line tool that enables you to connect to Azure and execute management operations on Azure resources using commands. It's ideal for managing resources from a terminal or for automation.
To get started, you first need to install the Azure CLI. Instructions for your operating system can be found on the official Azure CLI installation page.
Logging In
Once installed, the first step is to log in to your Azure account:
az login
This command will open a browser window where you can authenticate with your Azure credentials. After successful authentication, your default browser will close, and you'll be logged in within the CLI.
To log in to a specific subscription if you have multiple, use:
az login --subscription <subscription-id-or-name>
Managing Resource Groups
Resource groups are logical containers that hold related Azure resources for a solution.
Creating a Resource Group
Use the following command to create a new resource group:
az group create --name MyResourceGroup --location eastus
Replace MyResourceGroup with your desired name and eastus with your preferred Azure region.
Listing Resource Groups
To view all your resource groups:
az group list --output table
The --output table option provides a more readable, tabular format.
Deleting a Resource Group
Be cautious when deleting resource groups, as this action is irreversible and deletes all resources within it.
az group delete --name MyResourceGroup --yes --no-wait
--yes: Confirms the deletion without further prompts.--no-wait: Returns immediately without waiting for the operation to complete.
Deploying Resources
You can deploy various Azure resources, such as virtual machines, storage accounts, and web apps, using the Azure CLI.
Creating a Storage Account
This command creates a general-purpose storage account:
az storage account create \
--name mystorageaccountcli \
--location eastus \
--resource-group MyResourceGroup \
--sku Standard_LRS
Storage account names must be globally unique and between 3 and 24 characters long.
Listing Storage Accounts
To list all storage accounts within your subscription:
az storage account list --output table
Working with Virtual Machines
The Azure CLI provides extensive capabilities for managing virtual machines.
Creating a Virtual Machine
A basic Linux VM can be created with the following command:
az vm create \
--resource-group MyResourceGroup \
--name MyVM \
--image UbuntuLTS \
--admin-username azureuser \
--generate-ssh-keys
This will create a virtual machine named MyVM in the MyResourceGroup using the latest Ubuntu LTS image and generate SSH keys for access.
Connecting to a VM
Once the VM is created, you can connect to it using SSH:
ssh azureuser@<public-ip-address>
You can retrieve the public IP address using:
az vm show --resource-group MyResourceGroup --name MyVM --show-details --query publicIps --output tsv
Common CLI Concepts
The Azure CLI follows a consistent structure for its commands:
az <group> <subgroup> <command> [options]
az: The base command.<group>: A command group (e.g.,group,vm,storage).<subgroup>: Optional subgroup for more specific actions.<command>: The specific action to perform (e.g.,create,list,delete).[options]: Parameters that modify the command's behavior.
Getting Help
You can get help on any command by appending -h or --help.
az vm --help
az vm create --help
Output Formats
The Azure CLI supports various output formats, including:
json(default)tabletsvyaml
You can specify the output format using the --output or -o flag.
Next Steps
This tutorial covered the basics. For more advanced scenarios and a comprehensive list of commands, explore the full Azure CLI reference documentation. Consider learning about: