Azure Cli Scenarios
Azure CLI Scenariosπ
πΉ Exercise 1: Setup & Resource Groupsπ
Login to Azure CLI
Set your active subscription
Create a resource group
Verify it
πΉ Exercise 2: Storage Account + Blobπ
Create a storage account
az storage account create \
--name mystorage123xyz --resource-group MyRG \
--location eastus --sku Standard_LRS --kind StorageV2
Create a container
Upload a CSV file
az storage blob upload --account-name storageaccountfromclivb \
--container-name clidatacontainer --name sample.csv --file ./sample.csv
List blobs
az storage blob list \
--account-name storageaccountfromclivb --container-name clidatacontainer \
--output table
πΉ Exercise 3: SQL Databaseπ
Create a SQL server
az sql server create \
--name my-sql-server123 --resource-group MyRG \
--location eastus --admin-user myadmin --admin-password MyP@ssw0rd!
Create a database
az sql db create \
--resource-group MyRG --server my-sql-server123 \
--name mydb --service-objective S0
Show details
πΉ Exercise 4: Virtual Machineπ
Create a Linux VM
az vm create \
--resource-group MyRG --name MyVM \
--image UbuntuLTS --admin-username azureuser --generate-ssh-keys
Check running VMs
Stop the VM
Start it again
πΉ Exercise 5: Build a Mini Data Lake Pipelineπ
Goal: Create a Data Lake storage, upload raw data, then use queries to check it.
- Create a storage account with hierarchical namespace (Data Lake Gen2):
az storage account create \
--name mydatalake123 --resource-group MyRG \
--location eastus --sku Standard_LRS --kind StorageV2 \
--hierarchical-namespace true
Create a container for raw data:
Upload multiple files (CSV, JSON):
for file in ./data/*; do
az storage blob upload \
--account-name mydatalake123 \
--container-name raw \
--name $(basename $file) \
--file $file
done
basename $file
π
basename
is a Unix command.- It strips the directory path from a file and returns only the filename.
- Example:
Output:
--name $(basename $file)
π
- The
$(...)
syntax means command substitution: run the command inside and replace it with its output. - So
$(basename $file)
gets replaced by the filename (without path). - If
file="/home/user/docs/report.pdf"
, then:
becomes:
Why itβs usedπ
Often, scripts loop through files in directories. Instead of passing full paths, they just want the filename for naming a resource, argument, or output.
Example in Azure CLI:
for file in /path/to/files/*; do
az storage blob upload \
--account-name mystorage \
--container-name mycontainer \
--file $file \
--name $(basename $file)
done
Here:
--file $file
β full path to the file.--name $(basename $file)
β just the filename in the blob storage.
Verify using query:
az storage blob list \
--account-name mydatalake123 --container-name raw \
--query "[].{Name:name, Size:properties.contentLength}" --output table
That syntax is from the Azure CLI, and specifically it uses JMESPath (a JSON query language) to filter and reshape JSON output from Azure commands.
Letβs break it down:
The contextπ
Most az
CLI commands return JSON objects. For example, listing blobs:
Might return something like:
[
{
"name": "file1.txt",
"properties": {
"contentLength": 1234,
"contentType": "text/plain"
}
},
{
"name": "file2.csv",
"properties": {
"contentLength": 5678,
"contentType": "text/csv"
}
}
]
The query explainedπ
--query "[].{Name:name, Size:properties.contentLength}"
-
[]
-
Means "go through each item in the JSON array".
-
{Name:name, Size:properties.contentLength}
-
Reshapes each object to keep only:
Name
β mapped from the fieldname
.Size
β mapped from nested fieldproperties.contentLength
.
So the output would look like:
Why itβs usefulπ
- Avoids huge JSON output.
- Lets you extract just the fields you care about.
- Works with
--output table
for nice tabular summaries:
az storage blob list \
--account-name mystorage \
--container-name mycontainer \
--query "[].{Name:name, Size:properties.contentLength}" \
--output table
Gives: