Dokumentationen

CLI

With the Azure CLI, it is possible to create resources in Azure 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. See the instructions here.

Login

Just like in the portal, you must first log in using the CLI. To do this, open the command line and enter the following command:

az login

The login is performed interactively. Normally, after entering the command, a browser opens with the same login screen as in the portal.

Creating a Resource Group

A simple example of an administrative task is creating a Resource Group. After logging in, enter the following command:

az group create --location switzerlandnorth -n rg-testtutorial-switzerlandnorth-001

Scripting

While individual queries are useful for getting an overview, the CLI is especially suited for managing entire infrastructures. For this, scripts are ideal. See the following example in Bash to create 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'."

You can then run this script on the command line:

chmod +x script.sh
./script.sh

Note

Before running this example, you must log in with az login.

PIM

If you need to perform tasks via the CLI that require you to activate a role first (see Permissions or Tutorial: Activate Role), you can handle this just like in the portal. You need to activate the PIM group and can then work with elevated permissions via the CLI.

Important

If you first log in with az login and then activate the PIM group, the permissions may not be active yet. Therefore, you must first log out of Azure using az logout and then log in again with az login.

Next Steps

With the CLI, you can automate the setup of small environments and use scripts to automate this process. However, if you manage larger environments, the CLI is not ideal. Specialized Infrastructure as Code (IaC) tools are better suited for this. A well-known IaC tool is Terraform. The next article provides a brief introduction.

Further Information