With the Azure CLI, resources in Azure can be created via the command line. Below is an excerpt from Microsoft:
The Azure Command-Line Interface (Azure CLI) is a cross-platform command-line tool that allows you to connect to Azure and execute management commands for Azure resources. The tool enables you to run commands through a terminal, using interactive command prompts or a script.
Note
This topic is not required to use Azure. Everything described here can also be done via the portal.
Installation
The tool can be installed directly from the Azure website. The instructions can be found in the CLI Documentation.
Login
Just like in the portal, a login must first be performed using the CLI. To do this, the command line can be opened and the following command can be entered:
az login
The login is performed interactively. Normally, after entering the command, a browser opens with the same login screen as displayed in the portal.
Creating a Resource Group
A simple example of an administrative task is creating a Resource Group. After logging in, the following command can be entered:
az group create --location switzerlandnorth -n rg-testtutorial-switzerlandnorth-001
Scripting
While individual queries are practical for getting an overview, the CLI is especially suited for managing entire infrastructures. For this, scripts are ideal. The following example in Bash shows the creation of a WebApp:
#!/usr/bin/env bash
resourceGroupName="rg-testwebapp-switzerlandnorth-001"
appServiceName="app-testwebapp-switzerlandnorth-001"
planName="plan-testwebapp-switzerlandnorth-001"
location="switzerlandnorth"
servicePlanSku="F1"
if az group show --name $resourceGroupName --query "name" --output tsv 2>/dev/null; then
echo "Resource group '$resourceGroupName' already exists."
else
# Create the resource group
az group create --name $resourceGroupName --location "$location"
echo "Resource group '$resourceGroupName' created."
fi
# Check if the service plan already exists
if az appservice plan show --name $planName --resource-group $resourceGroupName --query "name" --output tsv 2>/dev/null; then
echo "App Service Plan '$planName' already exists."
else
# Create the App Service Plan
az appservice plan create --name $planName --resource-group $resourceGroupName --sku $servicePlanSku
echo "App Service Plan '$planName' created."
fi
# Create the App Service
az webapp create --name $appServiceName --resource-group $resourceGroupName --plan $planName
echo "App Service '$appServiceName' created in resource group '$resourceGroupName' in location '$location'."
This script can then be executed on the command line:
chmod +x script.sh
./script.sh
Note
Before running this example, a login with az login must be performed.
PIM
If tasks need to be performed via the CLI that require a role to be activated first (see Permissions or Tutorial: Activate Role), this can be handled just like in the portal. The PIM group must be activated and then work can be performed with elevated permissions via the CLI.
Important
If a login is first performed with az login and then the PIM group is activated, the permissions may not be active yet. Therefore, a logout from Azure must first be performed using az logout and then a new login must be performed with az login.
Next Steps
With the CLI, smaller environments can be built automatically and this process can be automated using scripts. However, if larger environments are managed, the CLI is not ideal. Specialized Infrastructure as Code (IaC) tools are better suited for this. A well-known IaC tool is Terraform. A brief introduction can be found in the next article Terraform.