Azure CLI Setup: Authenticate and Configure Defaults
You will install the Azure CLI, authenticate, select the right subscription, and set defaults so later commands are short and safe. By the end you will have a reproducible az workflow ready for every other Azure lab.
Learning Objectives
- Authenticate interactively and view your subscriptions.
- Set the active subscription and default resource group/location.
- Create a service principal for automation.
- Time: ~1 hour · Difficulty: Beginner · Prereqs: an Azure account.
Architecture Overview
graph LR
You[You] -->|az login| AAD[Microsoft Entra ID]
AAD -->|token| CLI[Azure CLI]
CLI -->|set subscription| Sub[Subscription]
CLI -->|create SP| SP[Service Principal for automation]
Environment Setup
You will need: the Azure CLI (install docs) and an Azure account.
Before you begin: know which subscription you should deploy into to avoid surprise billing.
Step-by-Step Execution
01
Authenticate
az loginOpens a browser to sign in to Microsoft Entra ID.
02
Select the subscription
$ az account list -o table && az account set --subscription "Pay-As-You-Go"
Name IsDefault
Pay-As-You-Go True
03
Set defaults to shorten commands
az configure --defaults group=tyf-rg location=eastus04
Create a service principal for automation
A service principal lets pipelines authenticate without your personal account.
az ad sp create-for-rbac --name tyf-ci --role Contributor --scopes /subscriptions/$(az account show --query id -o tsv)Progress So Far
graph LR
A[01 Login] -->|done| B[02 Subscription]
B -->|done| C[03 Defaults]
C -->|done| D[04 Service principal]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
style D fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
az account show -o table && az group create -n tyf-rg -l eastusYou should see your active subscription and a created resource group using your defaults. If so, your CLI is ready.
Troubleshooting
- az: command not found: the CLI is not on PATH; reinstall or restart your shell.
- Wrong subscription used: always confirm with
az account showbefore deploying. - SP secret printed once: store it immediately in a secret manager; it is not retrievable later.
Extension Ideas
- Deploy your first network in Virtual Network Deployment.
- Use the service principal in GitHub Actions.
- Codify everything with Infrastructure as Code.
Key Results
- Authenticated and confirmed the correct subscription.
- Set defaults that shorten and de-risk every later command.
- Created a least-privilege service principal for automation.
- Validated by creating a resource group from defaults.