Azure CLI Tutorial
Introduction
Azure CLI is a command-line tool providing a great experience for managing Azure resources. It enables users to perform various operations using commands, making it a powerful tool for developers and administrators.
Installation
To get started with Azure CLI, you'll first need to install it. The installation process varies depending on your operating system.
Windows
Download the MSI installer from the official Azure CLI website and run it to install.
macOS
Use Homebrew to install Azure CLI:
brew install azure-cli
Linux
Use the following commands to install Azure CLI on a Debian-based system:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Logging In
Once installed, you need to log in to your Azure account:
az login
This command will open a browser window for you to complete the authentication process.
Output:
The default web browser has been opened at https://login.microsoftonline.com/common/oauth2/deviceauth...
Basic Commands
Here are some basic commands to get you started with Azure CLI:
List Subscriptions
az account list --output table
Output:
Name CloudName SubscriptionId State IsDefault ------------------------------------- ----------- --------------------- ------- ----------- My Subscription AzureCloud xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Enabled True
Create a Resource Group
az group create --name MyResourceGroup --location eastus
Output:
{ "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup", "location": "eastus", "managedBy": null, "name": "MyResourceGroup", "properties": { "provisioningState": "Succeeded" }, "tags": null, "type": "Microsoft.Resources/resourceGroups" }
List Resource Groups
az group list --output table
Output:
Name Location Status ---------------- ---------- --------- MyResourceGroup eastus Succeeded
Managing Resources
Azure CLI allows you to manage various Azure resources. Below are some examples:
Create a Virtual Machine
az vm create --resource-group MyResourceGroup --name MyVM --image UbuntuLTS --admin-username azureuser --generate-ssh-keys
Output:
{ "fqdns": "", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyVM", "location": "eastus", "macAddress": "00-0D-3A-1B-7C-9F", "powerState": "VM running", "privateIpAddress": "10.0.0.4", "publicIpAddress": "13.82.101.25", "resourceGroup": "MyResourceGroup", "zones": "" }
List Virtual Machines
az vm list --resource-group MyResourceGroup --output table
Output:
Name ResourceGroup Location Zones ------ --------------- ---------- ------ MyVM MyResourceGroup eastus
Advanced Features
Azure CLI also supports advanced features such as scripting and automation. Below is an example of how you can use a script to automate tasks:
Script to Create Multiple Resource Groups
#!/bin/bash for i in {1..5} do az group create --name MyResourceGroup$i --location eastus done
Save this script as create_groups.sh
and run it:
bash create_groups.sh
Conclusion
Azure CLI is a powerful tool that simplifies the management of Azure resources. Whether you are a developer or an administrator, mastering Azure CLI can significantly enhance your productivity and efficiency in managing Azure services.