Using Azure CLI

There are two tools az (Azure CLI) and azd (Azure Developer CLI). Both serve different purposes but are complementary to each other.
Azure CLI Azure Developer CLI
Purpose General-purpose command-line tool High-level developer-focused CLI
Use Case Manages Azure resources and configurations. Manages end-to-end Azure workflows and applications.

1. Azure CLI

1.1 Install

Install it:
brew install azure-cli
Check it works:
az --version
It should show the installed version. As of 2025/01/01 my Azure CLI version is 2.67.0.

1.2 Login

To login do the following command. It will open a browser where you can login. When the login is done it will communicate the CLI.
az login
Since Azure CLI version 2.61.0, we are able to select the Subscription at login time. Just follow the instructions in the CLI and input the number that corresponds to the desired subcription. More in the official docs.

1.3 Check acccount/subscription information

If successfully logged in active account and subscription information can be retrieved
az account show --output table
or more specific
az account show --query "{Name:name, ID:id}" --output table
The former will show a table like this:
EnvironmentName HomeTenantId IsDefault Name State TenantDefaultDomain TenantDisplayName TenantId
AzureCloud ccfc0000-0000-0000-0000-000000000000 True Azure Subscription Basic Enabled meemaildomain.onmicrosoft.com Default Directory ccfc0000-0000-0000-0000-000000000000

1.4 Retrieve all subscriptions

All available accounts can be retrieved with
az account list --output table
or a more specific command
az account list --query "[].{Name:name, ID:id}" --output table
The former will show a table like this:
Name CloudName SubscriptionId TenantId State IsDefault
Azure Subscription Basic AzureCloud 00000-0000-0000.. ccfc0000-0000-0000-0000-000000000000 Enabled true

1.5 List Resource Groups

List resorces groups in a nice table. If you prefer the json format just omit --output table.
az group list --output table
or a more specific query:
az group list --output table

2. Azure Developer CLI

2.1 Install

Install it (More in the official docs.):
brew tap azure/azd && brew install azd
Check it works:
azd version

2.2 See help

There are various subcommands like init, up, etc. See a list of them in the help:
azd --help

2.3 Create a simple web

Official documentation shows various templates. I will use python and Flask with App Service because is the cheapest for small apps. Create the app from a template in local computer. We will be interactively asked for an environment name. It is optional.
azd init --template azure-flask-postgres-flexible-appservice 

2.4 Deploying the web app

If app was created with above template. It will contain a bunch of bicep files and .azure.yaml file which are enough to deploy with the follwing command. This command will deploy to the default environment. If it is the first time deploying in such environment then the subscription and region will be interactively asked too.
azd up
Resource group name and other services names will be derived from environment name (dev1).

(✓) Done: Resource group: dev1-rg (4.324s)
(✓) Done: Virtual Network: dev1-vnet (7.855s)
(✓) Done: Log Analytics workspace: dev1-AAAAAAAAAAAAA-loganalytics (17.576s)
(✓) Done: Application Insights: dev1-AAAAAAAAAAAAA-appinsights (5.623s)
(✓) Done: Portal dashboard: dev1-AAAAAAAAAAAAA-appinsights-dashboard (1.832s)
(✓) Done: Key Vault: dev1AAAAAAAAAAAAA-vault (24.42s)
(✓) Done: Private Endpoint: dev1-keyvault-pe (33.304s)
(✓) Done: Azure Database for PostgreSQL flexible server: dev1-AAAAAAAAAAAAA-postgresql (4m5.414s)
(✓) Done: App Service plan: dev1-AAAAAAAAAAAAA-appsvc-serviceplan (10.593s)
(✓) Done: App Service: dev1-AAAAAAAAAAAAA-appsvc-web (43.647s)

2.5 Deploy to another environment/region/subscription

Create a new environment and pass it to up command.
azd env new dev2
azd up --environment dev2  
Where is Environment information stored?
Information for each environment is stored in a separated file: .azure/[EnvironmentName]/.env .
  • Environment can be created at azd init --template [template-name] and azd env new [EnvironmentName] .
  • Environment name is stored in AZURE_ENV_NAME variable
  • Additionally, subscription plan name and location (region) is also stored in the same file.
  • Information gathered at deployment/provisioning time is also stored (i.e: BACKEND_URI, AZURE_KEY_VAULT_ENDPOINT, AZURE_KEY_VAULT_NAME, etc).
.azure/dev2/.env :
AZURE_ENV_NAME="dev2"
AZURE_LOCATION="eastasia"
AZURE_SUBSCRIPTION_ID="00000000-0000-0000-0000-000000000000"

2.6 Check environments


View current environment. The default environment will be used in azd up unless --environment flag is used.
azd env list
NAME DEFAULT LOCAL REMOTE
dev1 false true false
dev2 false true false
nacho true true false
View other related variables
azd env get-values

0 comments :

This work is licensed under BSD Zero Clause License | nacho4d ®