Getting started with Azure Command Line Interface 2.0 (CLI)

Adding new Azure resources to your subscription can be done multiple ways. The most common way, of course, is adding resources by hand in the Azure Portal. But this is error prone and tedious. To manage your azure resources in a professional way, you need ‘better’ tooling.

A viable solution is making use of Azure Resource Manager (ARM) templates. This is very popular but it has a certain learning curve.

And it’s also possible to make use of Azure integration of power shell. Again, there is a certain learning curve.

Let’s look at another solution, the Azure Command Line Interface (CLI). What if you could manipulate resources using the command line in a dos box? What if you could add a resource group like this, in one line:

az group create -n cli-rg -l westeurope

This looks easy, doesn’t it?

The CLI is quite new, and it’s available on multiple platforms (Windows, Linux, MacOS, etc.). It’s built on top of Python so you have to install that tool first.

Let’s make this happen. Let’s install and run the CLI. In this example, I use a Windows machine but there are installation guides for other operating systems available too.

This is part one of a series of blogs regarding the Azure CLI:

  1. Getting started with Azure Command Line Interface 2.0
  2. Working with the IoT Hub using Azure Command Line Interface
  3. Azure CLI made easy. Using the shell

Installing Python

If not already available on your machine, download Python first. The regular version is good enough:

Before installing, be sure to check if the python installation is added to the environment path. This way you can access python everywhere on your file system:

I got this little warning regarding the maximum path length. Luckily, there is a solution for that:

After the installation, open a dos box (aka Command Prompt) and check if Python is available:

As you can see, I can run Python (version 3.6.0 at this moment).

Installing the CLI using PIP

With the installation of Python comes the availability of the Python Package Index (PIP). This is a package manager which makes it possible to download new libraries into your Python environment. And the Azure CLI is available in this index!

So run:

pip install --user azure-cli

This will download and install the CLI:

Note: if PIP is not available, reinstall Python and set the PIP option in the customize installation.

Let’s check if the installation of the Azure CLI using PIP has succeeded:

Although I get an error, I know for sure the installation is ok. But the location of the CLI installation is not added to my environment path yet. Therefore I get this error while accessing the ‘az’ command of the CLI.

So, add the path (in my case ‘C:\Users\[username]\AppData\Roaming\Python\Python36\Scripts’) and we can access the CLI:

Note: When you manipulate the path, please restart the DOSBox to make sure the update is available at the command prompt.

This ‘az’ will unlock Azure at the Dos Prompt!

Logging in into Azure

At this moment we have completed the installation of the CLI and it’s available at the Dos Prompt but we can not manipulate Azure resources yet. We have to log in first!

It can be done the normal way:

az login -u johndoe@contoso.com -p VerySecret

But this will expose your password in CLI scripts, it’s not recommended. Another way is using the interactive way:

az login

This will start an interactive dialog between the Dos Prompt and a browser. First, you get a code which you will have to enter into  ‘aka.ms/devicelogin’:

Fill in the code and then sign in with your azure credentials:

Finally, you are logged in:

This login will survive closing the dos box and even rebooting the system. It will time out eventually conform the OAuth connection that is made.

How about dealing with multiple Azure subscriptions?

Resources, like the resource group shown at the top of this page, will be created in only one subscription at a time. If you have multiple subscriptions, you will have to switch between them.

Let’s check out the subscription which is currently used:

az account show

This is the account the CLI will use to create resources in. If this is not the subscription you are looking for, you can select another subscription.

First, list the subscriptions you have:

az account list

Check out the accounts shown:

As you can see, The first account is the default account (isDefault is true). Now, fill in the name of the second account into:

az account set --subscription "[name of second account]"

Finally, now the right subscription is used for adding and changing resources.

Creating a resource group

Now it’s time to actually do some magic. Just create that resource group we have seen earlier:

az group create -n cli-rg -l westeurope

This will take a couple of seconds. CLI commands are blocking by default. This means that the prompt will not return until the action (creation, deletion, etc.) has completed. This way, you can ‘chain’ commands in a batch file. Creating multiple resources which depend on each other will succeed all because resources are available at the right time.

So finally, the creation will succeed:

If you check out the Azure Portal, you can find the resource group, created in good order:

How did I know the available settings?

The CLI is just another way of manipulating Azure. So you will have to learn all possible settings. Of course, there is a reference manual for this specific example.

You can also explore it using the command prompt:

A full command reference is available here.

Conclusion

Using the new command-line experience for managing Azure resources give you great power. And the list of options seems limitless. I can recommend this way of creating resources. It’s a real time saver once you have installed the CLI successfully.

Update

The collection of commands in the CLI is still increasing. To update your current CLI installation with the newest commands, use:

pip install --user azure-cli --upgrade
Advertenties