Skip to main content

Azure CLI Basics

Authenticating

Before executing scripts, you must be authenticated. In a script this can be done by using:

Login to Azure
az login

This will open the browser where you can authenticate. After authantication this line is not needed for the time the session is valid.

Setting subscriptionID

To have the correct resources provisionied, you must define the subscriptionID to the session. It will take the default if not provided.

Set subscriptionID
az account set --subscription "[subscriptionID]"

Resources

Resourcegroup

Each resource in Azure requires a resourcegroup. A resourcegroup is a container that contains resources. Creating a resourcegroup requires:

  1. Location
  2. Name
Create resourcegroup
$location = "westeurope"
$resourceGroup = "tst-azure-web-apps"

az group create -l $location -n $resourceGroup

Webapps

Webapps are the most commonly used resources in Azure. It's the buildingblock to run webapps. To create a webapp , you'll need:

  1. Serviceplan
  2. Resourcegroup
  3. Name

Because you'll need a serviceplan to create a webapp, you must create a serviceplan first.

A serviceplan requires:

  1. Location
  2. Name
  3. Tier/SKU

Creating a serviceplan and webapp can be combined to minimize the amount of required code:

Create serviceplan and webapp
$appName = "calculator"
$location = "westeurope"
$resourceGroup = "tst-azure-web-apps"
$webapp = "{0}-{1}" -f $resourceGroup, $appName
$servicePlan = "{0}-plan" -f $webapp
$sku = "S1"

az appservice plan create -g $resourceGroup -n $servicePlan --location $location --sku $sku
az webapp create -g $resourceGroup -p $servicePlan -n $webapp

Additional configurations

When creating a webapp, it's important to configure the webapp as clean and secure as possible:

  1. Disable FTP access
  2. HTTPS only
  3. Disable Session affinity cookie
Webapp Configuration
az webapp update -g $resourceGroup -n $webapp --https-only true --client-affinity-enabled false
az webapp config set --ftps-state Disabled -g $resourceGroup -n $webapp