Overview and What You Will Learn
In this lab, you will use Cloud Shell to run Azure CLI commands, write a small script that creates several resources in a loop, and see directly why automation beats manual clicking the moment a task needs to be repeated more than once.
Why This Matters in Production
At a company like Hotstar preparing infrastructure for a major live event, spinning up twenty identical VMs by clicking through the Portal's wizard twenty times invites mistakes and wastes hours. The same task as a CLI script with a loop takes seconds, produces identical results every time, and can be reviewed and reused for the next event.
Core Principles
Every action available in the Portal, CLI, PowerShell, or the underlying REST API ultimately goes through the same engine - Azure Resource Manager. This is why results stay consistent no matter which tool you use.
+------------------------+ +------------------------------+| You (CLI, Portal, | | Azure Resource Manager || PowerShell, or API) | -------> | Authenticates + authorizes |+------------------------+ | then forwards to the service | +------------------------------+Cloud Shell is a browser-based terminal built into the Portal with the CLI and PowerShell pre-installed and already authenticated - no local setup needed to start scripting immediately.
Detailed Step-by-Step Practical Lab
Open Cloud Shell from the Portal's top navigation bar and select Bash if prompted to choose a shell.
Confirm your active subscription and account:
az account show --output table- Create a Resource Group to hold everything this lab creates:
az group create --name rg-cli-lab-mumbai --location centralindia- Write a small script that creates three storage accounts in a loop, rather than running the same command three times by hand:
for i in 1 2 3; do az storage account create \ --name stclilabrahul$i \ --resource-group rg-cli-lab-mumbai \ --location centralindia \ --sku Standard_LRSdone- List all three storage accounts to confirm the loop worked identically each time:
az storage account list \ --resource-group rg-cli-lab-mumbai \ --output table- Use
--queryto extract only the specific field you need, instead of scanning full JSON output manually:
# Returns just the names, one per lineaz storage account list \ --resource-group rg-cli-lab-mumbai \ --query "[].name" --output tsvNote
--queryuses JMESPath syntax to filter and reshape the JSON response before it's even printed. This becomes essential once you're scripting against dozens or hundreds of resources and don't want to manually parse full output.
- Clean up everything created in this lab:
az group delete --name rg-cli-lab-mumbai --yes --no-waitProduction Best Practices & Common Pitfalls
Common MistakeUsing the Portal's wizard to manually create many similar resources, one at a time, "since it's just a few." Manual repetition is exactly where inconsistent naming, forgotten tags, and copy-paste mistakes creep in - a script produces the same correct result every single time.
TipSave frequently used CLI commands as small shell scripts in your Cloud Shell's persistent storage. Cloud Shell keeps a small file share between sessions specifically so your scripts and tools survive even after you close the browser tab.
- Use
--output tablewhile exploring,--output jsonor--querywhen scripting. Table output is easier for a human to scan; JSON or a JMESPath query is what a script should actually parse. - Test destructive commands on a throwaway Resource Group first. A loop that deletes resources is exactly as fast and exactly as repeatable as a loop that creates them - verify the logic somewhere safe before running it against anything real.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
az account show |
Show the currently active subscription |
az account list |
List all subscriptions available to your account |
az <service> list --query "[].name" |
Extract just names from a resource list |
az group delete --yes --no-wait |
Delete a group without waiting for confirmation |