Skip to main content

Using Azure CLI and Cloud Shell for Automation

Learn to automate repetitive Azure tasks with the CLI and Cloud Shell instead of clicking through the Portal one resource at a time.

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.

◈ DIAGRAM
+------------------------+ +------------------------------+
| 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

  1. Open Cloud Shell from the Portal's top navigation bar and select Bash if prompted to choose a shell.

  2. Confirm your active subscription and account:

Bash
az account show --output table
  1. Create a Resource Group to hold everything this lab creates:
Bash
az group create --name rg-cli-lab-mumbai --location centralindia
  1. Write a small script that creates three storage accounts in a loop, rather than running the same command three times by hand:
Bash
for i in 1 2 3; do
az storage account create \
--name stclilabrahul$i \
--resource-group rg-cli-lab-mumbai \
--location centralindia \
--sku Standard_LRS
done
  1. List all three storage accounts to confirm the loop worked identically each time:
Bash
az storage account list \
--resource-group rg-cli-lab-mumbai \
--output table
  1. Use --query to extract only the specific field you need, instead of scanning full JSON output manually:
Bash
# Returns just the names, one per line
az storage account list \
--resource-group rg-cli-lab-mumbai \
--query "[].name" --output tsv
Note

--query uses 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.

  1. Clean up everything created in this lab:
Bash
az group delete --name rg-cli-lab-mumbai --yes --no-wait

Production Best Practices & Common Pitfalls

Common Mistake

Using 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.

Tip

Save 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 table while exploring, --output json or --query when 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

Explore More in Azure Fundamentals and Governance

All 6 Topics

Frequently Asked Questions

Is Using Azure CLI and Cloud Shell for Automation free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Using Azure CLI and Cloud Shell for Automation topic cover?

Learn to automate repetitive Azure tasks with the CLI and Cloud Shell instead of clicking through the Portal one resource at a time.