Online Magazine
How to use Azure CLI

There are several ways to manage your resources in the Azure Cloud. A very practical one is the Azure Command-Line Interface (CLI), which also has certain advantages over the other options. However, you need to know a few tricks.
By Thomas Hafermalz

Anyone who wants to independently create or manage resources in their Azure account must know how to use the Azure Resource Manager. Apart from the Graphical User Interface (GUI, the Azure Portal), Infrastructure-as-Code templates (IaC) and PowerShell, there is another approach: the Azure Command-Line Interface (CLI).
In contrast to the approach with e.g. the cloud shell, which often entails a lot of copying and pasting, the work with Azure CLI is easier. It allows direct execution from the file as well as syntax highlighting and auto-completion. Furthermore, a series of commands can be executed together, as with a PowerShell script.
To do this, however, one must know how to properly save, execute and repeat scripts that have been created. I show this in the following two hacks.
Noob Hack
The following tools are required to use Azure CLI:
• Visual Studio Code
• Azure CLI
• CLI extension
– to save as a scrapbook file (.azcli)
Only through these tools and the subsequent saving as a scrapbook file (.azcli) can one work with autocompletion in addition to syntax highlighting. So when you enter your cli command, keywords are completed and, provided you are logged into your Azure account, additional names, e.g. for resource groups, are suggested and checked.
Pro Hack
Now let's take a closer look at the execution. With Visual Studio Code, there are two options: Execute the jobs per line or execute them all together.
- For a single line, simply place the cursor on the corresponding line and select <Run Line in Terminal> with the right mouse button – or by shortcut.
The results of this can be viewed in the terminal. - To illustrate how all commands within a file can be executed one after the other – but with only one call – I have created an .azcli file with a few example commands:
Create a resource group
Create a storage account
List the storage keys
Print the connection string
az group create --location northeurope --name az204blobdemo az storage account create --name stodemo08154711 --location northeurope --resource-group az204blobdemo --sku Standard_LRS az storage account keys list -g az204blobdemo -n stodemo08154711 az storage account show-connection-string -g az204blobdemo -n stodemo08154711
Now change to the path of the file in the terminal and simply enter the following: ./filename.azcli – the commands are then executed in the corresponding order:
Have fun coding with Azure CLI!
---
You can find more articles by Thomas Hafermalz and on the topic of the cloud here:
Monitoring in the Azure Cloud
More transparency for Cloud Security
Cost optimisation in the Cloud
