☁️ Azure CLI – Full Practical Tutorial¶
1️⃣ What is Azure CLI?¶
Azure CLI (az) is a cross-platform command-line tool to:
- Create and manage Azure resources
- Automate infrastructure
- Use in CI/CD (GitHub Actions, Azure DevOps)
- Avoid portal clicking
✔ Works on Linux, macOS, Windows ✔ Used heavily in DevOps
2️⃣ Install Azure CLI¶
Linux (Ubuntu / Debian)¶
Verify¶
3️⃣ Login & Account Basics¶
Login¶
Browser opens → authenticate.
List Subscriptions¶
Set Default Subscription¶
Show Current Context¶
4️⃣ Resource Groups (Foundation)¶
Create Resource Group¶
List Resource Groups¶
Delete Resource Group¶
5️⃣ App Service (Backend) via Azure CLI¶
Create App Service Plan (Linux)¶
az appservice plan create \
--name asp-pay2chat-dev \
--resource-group rg-pay2chat-dev \
--sku B1 \
--is-linux
Create Web App (Node.js)¶
az webapp create \
--name pay2chat-backend-dev \
--resource-group rg-pay2chat-dev \
--plan asp-pay2chat-dev \
--runtime "NODE|18-lts"
View Web App¶
6️⃣ App Settings (Environment Variables)¶
Add App Settings¶
az webapp config appsettings set \
--resource-group rg-pay2chat-dev \
--name pay2chat-backend-dev \
--settings \
NODE_ENV=production \
PORT=8080
List App Settings¶
az webapp config appsettings list \
--name pay2chat-backend-dev \
--resource-group rg-pay2chat-dev \
--output table
7️⃣ Enable Managed Identity (Very Important)¶
Verify¶
8️⃣ Azure Key Vault (CLI-Only Workflow)¶
Create Key Vault¶
Add Secret¶
az keyvault secret set \
--vault-name kv-pay2chat-dev \
--name DB-CONNECTION-STRING \
--value "postgres://user:pass@host:5432/db"
Get Secret (manual)¶
9️⃣ Grant Key Vault Access to App Service¶
Get Managed Identity Object ID¶
az webapp identity show \
--name pay2chat-backend-dev \
--resource-group rg-pay2chat-dev \
--query principalId \
--output tsv
Assign Key Vault Policy¶
az keyvault set-policy \
--name kv-pay2chat-dev \
--object-id <PRINCIPAL_ID> \
--secret-permissions get list
🔟 Use Key Vault Reference in App Settings¶
az webapp config appsettings set \
--resource-group rg-pay2chat-dev \
--name pay2chat-backend-dev \
--settings \
DB_URL="@Microsoft.KeyVault(SecretUri=https://kv-pay2chat-dev.vault.azure.net/secrets/DB-CONNECTION-STRING/)"
✔ No SDK ✔ No secrets in code ✔ Auto rotation
1️⃣1️⃣ Deployment via Azure CLI (Zip Deploy)¶
az webapp deploy \
--resource-group rg-pay2chat-dev \
--name pay2chat-backend-dev \
--src-path app.zip \
--type zip
1️⃣2️⃣ Logs & Monitoring¶
Stream Logs¶
Enable Logs¶
az webapp log config \
--name pay2chat-backend-dev \
--resource-group rg-pay2chat-dev \
--application-logging filesystem \
--level information
1️⃣3️⃣ Static Web Apps (CLI)¶
az staticwebapp create \
--name raiden-frontend-dev \
--resource-group rg-pay2chat-dev \
--source https://github.com/username/repo \
--branch Development \
--app-location "/" \
--output-location "build"
1️⃣4️⃣ Role-Based Access Control (RBAC)¶
Assign Role¶
az role assignment create \
--assignee user@domain.com \
--role "Contributor" \
--resource-group rg-pay2chat-dev
List Roles¶
1️⃣5️⃣ Clean-up (Very Important)¶
1️⃣6️⃣ Azure CLI in GitHub Actions¶
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Create Resource Group
run: |
az group create --name rg-ci-dev --location eastus
1️⃣7️⃣ Must-Know Azure CLI Commands (Cheat Sheet)¶
az help
az find "webapp"
az configure --defaults location=eastus
az resource list --output table
az upgrade
1️⃣8️⃣ Real-World Best Practices ⭐¶
✔ Use CLI for automation
✔ Use Bicep/Terraform for infra-as-code
✔ Never hardcode secrets
✔ Prefer Managed Identity
✔ Use --output table for readability
✔ Use --query for scripting
What You Should Practice Next¶
- Deploy App Service only using CLI
- Key Vault + Managed Identity via CLI
- CI/CD with Azure CLI
- Automate cleanup scripts
- Combine CLI + Terraform/Bicep