Good bye Azure Portal, Welcome Azure CLI

Good bye Azure Portal, Welcome Azure CLI

Let's jump into Azure CLI !

ยท

10 min read

In this article about Azure CLI, we will talk about:

Azure CLI can be installed by following the instructions on this page.

1 - Managing Azure resources

Azure resources can be managed from different ways but the main ones are the Azure Portal, Azure PowerShell and Azure CLI. If you are new to Azure or if you are creating resources you are not familiar with, it is always nice to have a GUI to understand what you are doing and Azure Portal is the right way to go.

azurecli_portal_1.png

However, handling resources in the portal, moving from pane to pane to always do the same kind of operations can quickly become a little cumbersome. So if you want to be more productive or if you are more of a command line person, you will find in Azure CLI or in Azure PowerShell really good alternatives to Azure Portal. Both of them are cross platform tools (PowerShell Core runs on Windows, MacOS and Linux :+1:) and provide the same functionalities to manage Azure resources.

Until recently, when I was not using the Azure Portal I was more prompt to use Azure PowerShell than Azure CLI, probably because I was often finding samples of what I wanted to do written in PowerShell. But in fact, there are lots of commands in Azure PowerShell and even with autocomplete I am not really good at remembering them. So I couldn't stop returning to using Azure Portal ๐Ÿ˜•. And yet, lately I finally came back to the command line when I started to discover the power of Azure CLI: such a nice and above all easy tool to use and that's I will try to show you next.

2 - Azure CLI Syntax

The Azure CLI Syntax is simple to understand. For instance the command az webapp list will list the WebApps in the subscription you are currently logged in.

Actually an Azure CLI command has the following structure:

  • a command group which represents an Azure service and which can be the composition of subgroups
  • a command which is the action you want to do on the group / Azure service
  • arguments optionally which are a list of parameter names and values

To sign in to your Azure account in your terminal, you have to execute the Azure CLI command az login where az is the command group and login is the command relative to the group/service az (meaning Azure).

In our previous example, az webapp list :

  • az webapp was the command group composed of az and its subgroup webapp
  • list was the command
  • no arguments here

We could have added some arguments to this command, like the --resource-group argument (or -g) to list only the WebApps in a specific resource group and that would have become az webapp list --resource-group 'myResourceGroupName'

A few arguments are globally available arguments which means that can be used for every command. This is the case of the --help argument which displays help information about a command. The following example showing the use of the --help argument allows to better understand the structure of an Azure CLI command.

azurecli_console_1.png

Once used to the Azure CLI syntax, it is way more convenient than the Azure Portal and you don't have to search what Azure PowerShell command you need to use. You just have to look for the available subgroups and commands in the Azure service where you want to work (don't forget to use --help) and you will quickly get the job done.

3 - Deep dive in Azure CLI

Command output

azurecli_console_2.png

By default, the output format of Azure CLI commands is json but there are other formats you can use by specifying an output argument (--output or -o) like this: az group list -o table.

azurecli_console_3.png

๐Ÿ’Ž The default output format can be configured using the az configure command. This command also allows you to configure other settings like a default resource group for your commands for instance.

Using variables

Whether you choose to run your commands in Bash or in PowerShell, you can use variables with Azure CLI, only the syntax for creating variables will change depending on the command-line shell you use.

The samples in this article use the PowerShell syntax.

$rgName = 'TestingAzureCLI'

# Create a new resource group
az group create -n $rgName -l westeurope

# Create an App Service plan with the Free tier
az appservice plan create -n $webAppName -g $rgName --sku FREE

# Show the App Service plan just created 
az appservice plan show -n $webAppName -g $rgName 

# Store the result of the show query in a PowerShell variable
$servicePlan = az appservice plan show -n $webAppName -g $rgName 

JMESPath

If you don't know JMESPath, it is a query language for JSON that allow to extract and transform elements from a JSON document or from CLI output in the context of Azure CLI.

In order to use JMESPath you have to add in your CLI command the argument --query followed by your JMESPath query. Let's see that with some examples.

The query az group list executed on my subscription returns the following json :

[
  {
    "id": "/subscriptions/********-****-****-****-************/resourceGroups/CloudShellRG",
    "location": "westeurope",
    "managedBy": null,
    "name": "CloudShellRG",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": {
      "Environment": "Production"
    },
    "type": null
  },
  {
    "id": "/subscriptions/********-****-****-****-************/resourceGroups/MyApp1ResourceGroup",
    "location": "canadacentral",
    "managedBy": null,
    "name": "MyApp1ResourceGroup",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": {
      "Department": "RH",
      "Environment": "Dev"
    },
    "type": null
  },
  {
    "id": "/subscriptions/********-****-****-****-************/resourceGroups/TestingAzureCLI",
    "location": "westeurope",
    "managedBy": null,
    "name": "TestingAzureCLI",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": {
      "Department": "IT",
      "Environment": "Production"
    },
    "type": null
  }
]

Here are some CLI commands that query more precisely the resource groups:

  • Select only the resource groups names with az group list --query '[].name' azurecli_jmespath_2.png

  • Filter on resource groups in West Europe with az group list --query "[?location=='westeurope']" azurecli_jmespath_1.png

  • Filter on resource groups with an Environment tag with the value Production, select and rename name and tags properties with az group list --query "[?tags.Environment=='Production'].{RGName:name, RGTags:tags}"

azurecli_jmespath_3.png

๐Ÿ’Ž JMESPath is not a Azure CLI only thing, it is used in different other projects or tools like the AWS CLI.

Mixing output, variables and JMESPath

Once you have understood how to use variables, commands output and JMESPath with Azure CLI you can mix everything to manage Azure resources with CLI commands that use results of previous CLI commands, which can make powerful scripts :)

Let's presume that we want to create a Web App for the IT Department in an existing App Service Plan of our subscription located in West Europe. We can do the following:

# Retrieve resource group name of IT Department in West Europe
$itRgName = az group list --query "[?tags.Department=='IT' && location=='westeurope'].name" -o tsv

# Retrieve app service plan name in this resource group
$appServicePlan = az appservice plan list -g $itRgName --query "[0].name" -o tsv

# Create a new Web App on this app service plan
az webapp create -n "MyNewWebAp" -g $itRgName --plan $appServicePlan

# List all Web App in this resource group
az webapp list -g $itRgName --query "[].name"

This is just a quick example but you can easily imagine all the possibilities that Azure CLI offers you.

4- Where to use Azure CLI

All that is great but we haven't really talked about where we can use Azure CLI, let's do that now !

Bash vs PowerShell

We briefly mentioned that you can run Azure CLI in Bash or in PowerShell, and as a matter of fact you can also run it in Windows Command Prompt. Although Azure CLI is originally designed to be bash oriented, it works fine in PowerShell so it's a perfect valid choice too. What command line shell you choose is really up to you !

And don't think that you will be limited to using one or the other shell depending on the platform you are working on, because both PowerShell and Bash are available whatever your platform (PowerShell Core is cross-platform and Bash is available in Windows through Windows Subsystem for Linux). Therefore I would just suggest you to use Azure CLI with the shell you are most familiar with.

Just a quick note about autocompletion: at this time tab completion is only supported in bash and Microsoft currently does not plan to add support for PowerShell but is open to contributions from the community. That's not really a big deal but that's good to know, there is a GitHub issue on this matter. If you are not using bash and that tab completion is really important for you, Azure CLI interactive mode is what you are looking for (we will talk about it later in the article)!

Azure CLI in Visual Studio Code

As for most of Azure components, there is an extension in vs code for Azure CLI: Azure CLI Tools.

azurecli_vscode_1.png

With this extension you can create Azure CLI Scrapbooks which are files with an .azcli extension where you have intellisense on CLI commands you write.

azurecli_vscode_3.png

It also allows you to run commands in the integrated terminal or run them and show their output in a side-by-side editor as show in the screenshot below.

azurecli_vscode_2.png

Azure Cloud Shell

I did not talk about Azure CLI installation but you can find everything you need in Microsoft documentation. Actually Azure Cloud Shell provides you a way to use Azure CLI without installing anything.

If we quote the documentation:

Azure Cloud Shell is an interactive, browser-accessible shell for managing Azure resources

In concrete terms, you open a browser, go to https://shell.azure.com, log in to your Azure subscription and you have access to a shell (PowerShell or Bash) that can interact with all the resources of your subscription through Azure CLI.

azurecli_cloudshell_1.png

Cloud Shell in itself is free but requires an Azure file share to be mounted so you will have regular storage costs (which are really low).

Azure Cloud Shell is also available in the Azure Portal or directly in your vs code integrated terminal through the Azure Account extension.

5- Azure CLI Interactive mode

I mentioned Azure CLI interactive mode as a way to have auto-completion. In fact it is much more than that as it also provides you with command descriptions, examples, completion on resources names, JMESPath on the previous command...

To enter in interactive mode, you have to type az interactive in your terminal. Then you can type your CLI commands with autocompletion, description of the command you are using, and examples for it. Moreover while you are typing a command you will see help about the arguments for this command.

azurecli_interactive_1.png

One thing to notice is that commands you type are scoped to a command group. By default, it's scoped to az group so you don't have to type the az keyword before your commands. So by default it only saves you two characters as you will write group list instead of az group list. But you can set the scope to a any subgroup as you can see in the following example.

azurecli_interactive_2.png

Another really nice thing you can do with interactive mode is to query the results of your previous command using the JMESPath syntax. Imagine I just have executed the command group list in interactive mode. If I want to select only the names in the results of this command, I can just type "?? [*].name".

azurecli_interactive_3.png

As you can see in the example above, you can even use a JMESPath query on the previous command as an argument for your next command. Here we are listing all resources on the TestingAzureCLI group.

๐Ÿ’Ž It can sometimes be little slow to use Azure interactive mode on https://shell.azure.com/, so if it happens to you I suggest you to use the vs code integrated terminal (if you want to use Azure Cloud Shell) or any terminal you want (if you have the Azure CLI installed) instead of the browser.

๐Ÿ’Ž If you like interactive CLI, you can also have a look to AzBrowse which is a nice community project.

To conclude

Even if the title of this article suggests to say Good-bye to Azure Portal, when you start using Azure CLI you are not really going to stop completely using Azure Portal. You will always need a GUI for some tasks or to better visualize things. However you will probably use less often Azure Portal as you are going to find yourself more productive with Azure CLI and will enjoy be able to script everything โœจ.

There is still much to say about Azure CLI but that's already a long article so the best thing is that you try it by yourself !

ย