21_Azure_CLI_Using_Cloud_Shell - Nirvan-Pandey/Azure_DOC GitHub Wiki

21_1: Introduction

Azure CLI (Command-Line Interface) is a cross-platform tool used to create and manage Azure resources directly from the command line. It's ideal for developers, cloud engineers, and DevOps professionals who prefer scripting and automation over GUI-based resource management.

Key Benefits of Azure CLI:

  • Platform Independent: Runs on Windows, macOS, and Linux.

  • Automation Friendly: Perfect for CI/CD pipelines.

  • Scripting Capable: Supports Bash, PowerShell, and even Docker containers.

  • Lightweight & Fast: Easier and faster for repetitive tasks than the Azure Portal.

21_2: Setting Up Azure CLI

Azure CLI can be installed on various platforms. Below are the steps for installation:

1. On Windows

az --version

2. On macOS

  • Install using Homebrew:
brew update && brew install azure-cli
  • Verify the installation:
az --version

3. On Linux

  • Install using the package manager:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
  • Verify the installation:
az --version

4. On Docker

  • Run Azure CLI in a Docker container:
docker run -it mcr.microsoft.com/azure-cli

21_3: Accessing Azure CLI via Cloud Shell

Azure Cloud Shell is an interactive, browser-accessible shell for managing Azure resources without any local setup.

Steps to Open Azure Cloud Shell:

  • Login to the Azure Portal

  • Click the Cloud Shell icon on the top-right corner

image

  • Choose Bash or PowerShell

image

  • If prompted, create a storage account

image

image

This gives you instant CLI access with pre-installed tools and authentication.

21_4: Authentication and Subscription

Login to Azure

az login
az

image

Verify the version

az --version

image

Set Your Active Subscription

If you have multiple subscriptions, set a default subscription

az account set --subscription "<Your-Subscription-ID>"

21_5: Managing Resource Groups

Resource Groups are logical containers for Azure resources.

  • Create a Resource Group
az group create --name MyResourceGroup --location eastus
az group create --name Spoke_RG --location eastus

image

image

  • List Resource Groups
az group list --output table

image

  • Delete a Resource Group
az group delete --name MyResourceGroup --yes
az group delete --name Spoke_RG --yes

image

image

💡 Tip: Always tag your resource groups for better cost and usage tracking.

21_6: Virtual Machine Management

Virtual Machines are one of the core services in Azure IaaS.

  • Create a Linux VM
az vm create \
  --resource-group MyResourceGroup \
  --name MyVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys
  • Start the VM
az vm start --resource-group MyResourceGroup --name MyVM
  • Stop the VM
az vm stop --resource-group MyResourceGroup --name MyVM
  • Delete the VM
az vm delete --resource-group MyResourceGroup --name MyVM --yes

⚠️ Deleting a VM does not delete associated resources like disks and NICs. Clean them manually if needed.

21_7: Storage Account Management

Azure Storage provides scalable, secure, and durable cloud storage.

  • Create a Storage Account
az storage account create \
  --name mystorageacct2025 \
  --resource-group MyResourceGroup \
  --location eastus \
  --sku Standard_LRS
  • List Storage Accounts
az storage account list --output table

21_8: Azure Kubernetes Service (AKS)

AKS simplifies Kubernetes management by offloading operational overhead to Azure.

  • Create an AKS Cluster
az aks create \
  --resource-group MyResourceGroup \
  --name MyAKSCluster \
  --node-count 2 \
  --enable-addons monitoring \
  --generate-ssh-keys
  • Get AKS Credentials
az aks get-credentials --resource-group MyResourceGroup --name MyAKSCluster
  • Delete AKS Cluster
az aks delete --resource-group MyResourceGroup --name MyAKSCluster --yes

🔍 Azure CLI supports kubectl integration once AKS credentials are fetched.

21_9: Best Practices for Azure CLI

  • Use Output Formats: Add --output table/json/yaml to structure output.

  • Secure Your Scripts: Avoid storing credentials in plain text.

  • Automate: Combine commands in shell scripts for consistent deployments.

  • Tag Resources: Always use --tags to manage cost and governance.

21_10: Summary

Azure CLI is a robust tool for managing cloud resources with precision and speed. Whether provisioning infrastructure, deploying services, or automating processes, it empowers users to fully leverage the power of Azure from their terminal.

21_11: Next Steps

  • Explore advanced CLI use with networking and databases

  • Integrate Azure CLI into GitHub Actions or Azure Pipelines

  • Build reusable deployment scripts in Bash or PowerShell