CLI
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.
Installation
Section titled “Installation”The tool can be installed directly from the Azure website. The instructions can be found in the CLI Documentation.
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 loginThe 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
Section titled “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-001Scripting
Section titled “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 existsif 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 Serviceaz 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.shIf 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.
Next Steps
Section titled “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.