Getting Started with Azure CLI

A complete beginner guide to Azure CLI with examples of command usage.
Parveen
8 min read
May 25, 2021

Table of Content

Share this

Twitter
LinkedIn
Reddit

Azure CLI provided by Microsoft is a cross-platform command-line tool for managing Azure resources and automating cloud resource deployment. The tool enables you to query, create and manage the cloud resources using the terminal.

In this article, you’ll see how to authenticate using various methods and get familiar with some of the common Azure CLI commands for regular use like getting help and finding example commands in Azure CLI modules.

Prerequisites

This is the second article in the series of Azure CLI. If you haven’t installed the CLI tools yet, check out the first post in the series:

Before you go into the article, you should be familiar with the basic concept of

Authenticate to Azure CLI

There are several ways to authenticate to the Azure environment using Azure CLI. Once logged in, the CLI allows you to query and interact with the cloud resources. Cloud Shell is the easiest way to use Azure CLI without logging in using CLI but the Azure Portal. However, while doing complex scripting, it’s most likely that you need to authenticate to the cloud using your device or development environment.

In the following few sections, you will see multiple ways that you can use to log in to the Azure environment using Azure CLI. We’ll be covering CLI usage in the section after authentication for the sake of being logged into your account before you use any queries.

Authenticate using a Browser

While working on Windows or macOS devices, it’s convenient to log in to the terminal session without having to type in the username password again if you are already logged in to Azure Portal on a browser session.

Follow the instruction below to log in to Azure using Azure CLI:

  • Navigate to the Start menu on Windows and launch PowerShell or search for Terminal on macOS:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8ebfc989-7e40-4498-91fa-ace1394360e1/01-powershell-launch.png
  • Run the following command to initiate the login session:
az login

In this command, the login module for Azure CLI is used to interact with the Azure Environment. The command launches an authenticated session to the Azure portal for ongoing resource management. You don’t need to sign in for each action as the session is stored as an authentication refresh token locally.

  • The terminal will show the following text, displaying the further actions on the authentication process through the default browser:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6c5b7a2d-c79c-4fc9-bea5-fe289268282d/02-cli-redirect.png

Ignore the warning for the device code on the last line for now. We’ll be covering that in the next section.

  • Follow the instruction on your browser to login to the Azure Account:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/903f18dc-4aec-4098-87ba-d850283fc43a/03-cli-weblogin.png
  • Once the authentication is successful, you will see the confirmation on the browser:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/da79cc29-a25e-4e61-b69d-4ad943c2f493/04-cli-login.png
  • At this point, you can close the browser and return to the terminal. The output on the terminal will show the list of subscriptions you have access to in your account:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4fcc0d5f-2ca7-400b-b95b-3419712c4999/05-cli-output.png

Authenticate using a Device Code

There might be a case when you are using an SSH or WSL system that doesn’t have any GUI interaction or direct access to launch a browser or want to authenticate without letting Azure CLI launch the browser session.

Follow the steps below to authenticate to your Azure account using a device code that can authenticate your CLI session from either same or any device logged in to Azure:

  • Launch your terminal and type the following command:
az login --use-device-code

Using the --use-device-code flag tells the CLI to authenticate with the code and lets you decide the device of login and the account.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/62adad0a-3dc0-48b2-bc85-584a35144623/06-device-code-cli.png
  • You’ll be prompted to enter the device code. Use the device code that looks like EU5KMLTHK in this case, from your command line output and paste here:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/914423ef-0331-49b8-a6ee-8321eb9a8bcd/07-device-code.png
  • Once confirmed, you’ll see the output displaying a success message:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/eced1583-f707-479c-a155-27664c225c06/08-login-device.png
  • Navigate back to your terminal and review the output that displays your login session information along with tenant and subscription information:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/999b64fe-9cba-4953-a19a-6495e6cfc214/09-device-success.png

Authenticate using Username and Password

If opening a browser to authenticate or copy-pasting the device code is not feasible for your case, or if it’s too much hassle, you can use the username and password directly for your Azure account to log in.

It’s worth mentioning that this is not a recommended method since it’s the least secure method of all other login forms. I’d recommend using other methods over this where possible unless this is the last option to resort to for you.

Follow the steps below to login to Azure CLI using username and password:

  • Run the following command in your terminal:
az login -u <username> -p <password>

Replace the username and password with your Azure account email and password before pressing Enter.

  • The command will authenticate you directly and displays the subscription access for your account:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9aeca780-d743-496c-a92c-a5670a70367e/10-azlogin-username.png

The accounts with MFA or Two-factor authentication may not be able to log in using this method. Consider using device code, or the Service Principal method explained below to login instead.

Authenticate using a Service Principal

Service Principals are service accounts that are not real user account; however, they can have permissions or roles just like user accounts. Service principals have a time-bound secret as a password to log in, which is valid for six months or more and needs to be renewed once expired.

The method of service principal login is super helpful when running scripts in the background on a schedule where you don’t want someone to go and put the device code whenever the script needs to execute.

I won’t cover the service principal creation process in this article. However, you can check this article section on creating a service principal from the Azure portal.

Use the following steps to log in to Azure CLI using Service Principal:

Before your login, you need three pieces of information for your login session.

  • Service principal Application (client) ID
  • Azure Tenant Name (Tenant Domain)
  • Service principal secret (create one)

You can find the Application ID on the Overview section of the page of your service principal when you create it for the first time:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a3a2ca73-61fb-41b7-94af-6d0081dce170/11-SP-Overview.png

The name of the tenant can be your FQDN that email accounts use in your tenant. If you don’t have any custom domains configured, consider using the Microsoft provided .onmicrosoft.com domain.

  • Run the following command in your terminal:
az login --service-principal -u <applicationID> -p <secret> --tenant <tenant domain>

Update the applicationID, secret, and tenant domain with the relevant information to access your Azure tenant.

  • The results will confirm the successful login to your Azure account:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ff45f2af-45d6-4986-8ac3-2c0b8f3f2d33/12-SPlogin.png

Working with Azure CLI Commands

Azure CLI has three main components: (a) Group, (b) Subgroup, and (c) Commands. Let’s explore these more in-depth.

Each CLI command starts with az initially. The az is then followed by either a group or a subgroup or command.

Group

A Group is referred to as the base command of az. So, for example, if you refer to a virtual machine, the base command, in that case, would be az VM. So, in simple words, the CLI commands are organized as groups.

Subgroup

A subgroup is a second layer under the group which comes into effect when a resource has more than one type or service. A subgroup may have further subgroups creating another level of hierarchy.

For example, there is SQL Server in Azure and also a SQL database. Similarly, Azure offers web applications as PaaS offerings. If you were to modify a website configuration, it’d be a subgroup under az webapp that would allow you to interact with the configuration part of the resource.

Commands

The commands are the final action that you take on the cloud resource. The commands vary based on the resource you interact with. Refer to the commands available for a virtual machine resource that shows the possibility of action you can take on VM resource.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c7290b21-3ff1-40cd-8b33-c7fe889910e3/13-vm-commands.png

We’ll cover how to see this information in the next sections.

Common Set of Resources

In Azure, while working on day-to-day tasks, you might come across one of these core services at some point. In addition, these offerings are some of the most popular services offered by Azure. So it’s worth getting familiar with these resources.

Using Azure CLI Help with az help

If you’ve worked with any other programming or scripting frameworks, this may come off naturally to you. If not, that’s fine either. Almost all the frameworks or CLI tools come with a help command that gives you insights into using the tool and learning about basic usage examples.

While working with Azure CLI, the most helpful command is --help (help flag) while using any CLI command. This help flag displays the relevant information to either a group or a subgroup. In addition, it presents you with all the possible actions you can take to interact with any given resources.

For example, we’ll look at Virtual Machine resources in Azure.

  • Run the following command inside your terminal. You don’t need to be logged in to run this command.
az vm --help
  • The output will look like this:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d23aab05-8550-490f-80f2-ec5773798988/14-vm-help.png

The first thing to note here is the distribution of the output in Group, Subgroups, and Commands that we discussed earlier. Running the az vm with a help flag displays the possible action you can take on the VM resource overall.

  • Let’s take a look at subgroup command now. We’ll be using the extension subgroup in this example. Run the following command in your terminal:
az vm extension --help
  • The output will look like this:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bf23300c-e753-43e3-ad81-454dc781d27b/15-extension-help.png

It’s worth mentioning that the subgroup itself has a further subgroup image that will display a distinct set of commands from the current query. The commands section shows all the possible actions you can take on the VM extensions at this point. We’ll cover how to query at a later time.

  • Run the following query to check all the possible actions on the list command:
az vm extension list --help
  • The results will look like this:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/91e0dad7-bf1f-45e5-be03-46162c8d852c/16-extension-list.png

While getting the help for the commands, you will see an Argument and Global Arguments section that gives you the optional and required parameters to pass on while running the command.

For example, under the Arguments sections, you need to pass on the resource group’s information and the VM name to interact with the resource to list all the extensions.

Feel free to play with other services and adding a help flag at the end to see the results for those commands.

Using Azure CLI Find Module

While working with a rich set of groups, subgroups, and commands, it’s easier to get distracted and lose track of where to look for the information. Therefore, the Azure CLI offers another command that gives you the exact information on using any command with a set of examples.

The az find command displays a list of examples commands that you can run for any given service. Follow the steps below to explore Azure storage resource.

  • Run the following command in your terminal;
az find storage
  • The output displays the common way in which you can use the storage resources:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/aea36125-6222-4e6e-af5b-c0a5803d55c2/17-find-storage.png

It’s worth mentioning that almost every query to Azure resource will need a specific name and resource group name, excluding the queries against all the resources such as the az storage account list.

Using Azure CLI Query Module

The output of the Azure CLI command can get very lengthy based on the number of resources you query. That’s why there’s a better way to filter and display the necessary information only while working with CLI commands.

Follow the steps below to understand the different types of queries available on azure resources. It is recommended to deploy some resources inside your subscription to query against; otherwise, you’ll be working with empty results.

  • Run the following command to query all the resources in Azure:
az resource list
  • The output displays the list of resources as an array of JSON objects. In my case, the output looks like this:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e913a9dd-4775-4cbf-bfd3-18bdb2d4447d/18-resource-list.png
  • The resource displayed in the output is a storage account used for Cloud Shell. Run the following command to query and filter the resources in the location southeastasia:
az resource list --query "[?location=='southeastasia']"
  • The output again displays the same resource. However, the result is now much less than the number of entries earlier since you specified the location to query from in this case.
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5f3c0bb4-6a3b-4329-92a3-8fb2f93f7fef/19-resource-query.png

The query pattern ([?attribute==value]) executes a JMESPath Query on the result of commands. JMESPath is a query language for JSON with the ability to select and modify the data for displaying simpler output results.

  • You can also display a limited set of output fields to clear the chaos in the output. Run the following output to display the name, resource group and location fields of all the resources in the given location:
az resource list --query "[?location=='southeastasia'].{Name:name, Location:location, ResourceGroup: resourceGroup}"
  • The output of the query is shown below in this case:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c63c0d9c-3283-4f73-aef9-689dbc7fb7b1/20-resource-display.png

The output displays an array of JSON objects with the specific fields queried in the original command. You can use. (dot) with {...} to define the list of keys from the JSON object output. You can learn more about CLI Query here.

Working with Azure CLI Output Options

The result of Azure CLI is displayed in JSON format by convention. However, the CLI does support a variety of output formats that might be useful based on your use case. Below is a list of output that Azure CLI supports today:

The output format for the queries is defined by passing an output --output or -o flag at the end of the query.

  • Run the following command in your terminal to display the results from the previous command but with an output flag for table view:
az resource list --query "[?location=='southeastasia'].{Name:name, Location:location, ResourceGroup: resourceGroup}" -o table
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fe1cd573-00d9-4576-997f-7825e419aad7/21-output-table.png

The output display the data in a table format with the headings and data in rows.

  • Similarly, the following command displays the data as yaml output:
az resource list --query "[?location=='southeastasia'].{Name:name, Location:location, ResourceGroup: resourceGroup}" -o yaml
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d88cf930-b7cd-47b0-a380-f305ad506e1f/22-output-yaml.png

Setting up Azure CLI Default Configuration

While working with the CLI tool, you are always searching for consistency and ensuring your workspace stays the same any time you use, including any preferences you decide to configure. Luckily, Azure CLI allows you to configure logging, data collection, and default arguments for your terminal session using the az configure command.

Configure Azure CLI Logging and Data Collection

Following the instructions below to configure your terminal for Azure CLI basic logging and data collection permissions:

  • Run the following command inside your terminal and enter y to confirm the action:
az configure
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/020f611d-1352-4218-9a77-af01971003f8/23-azcli-configure.png
  • The default output for the CLI result is JSON. You can pick your desired output format that will be set to default for your session:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c88ccea3-f621-4fd9-8087-1ef9fee2c714/24-output-configure.png
  • Set the optional logging for your terminal session:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c4ce7c22-854e-43e9-9ce5-6c1dd8d29d6c/25-logging-configure.png
  • Finally, choose to enable data collection for Azure CLI usage, if desired:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bee08ddb-112a-4e77-9130-7f8e44f8273e/26-data-configure.png
  • Leave the last prompt for cache TTL as default and press Enter.

Configure CLI Default Configurations with az configure

While setting the configuration is pretty straightforward, you can also define your environment information for the terminal session and Azure CLI. Just as a reminder, you can always use the --help parameter to discover all the options for the az configure command.

The CLI configuration allows you to predefine some of the commonly used environment settings for Azure resources, including the following:

  • Resource Group (group): The default resource group to use for all commands
  • Location (location): The default location to use for all commands
  • Web (webapp): The default App Service resource name to use
  • VM (vm): The default VM name to use
  • VMSS (vmss): The default VM Scale Set name to use
  • ACR (acr): The default container registry name to use

The configuration command takes one argument --defaults along with a key=value pair separated by space. You can set the default using the following command:

az configure --defaults location="canadacentral" VM="Lab-VM"

You can find the full list of configuration values and environment variables on Microsoft Docs page.

Conclusion

You have configured your Azure CLI configuration and worked with various commands available to see how you can interact with Azure resources. In the next article, we’ll go through creating your first Azure resource using Azure CLI.

Reference

Check out the previous post in the Azure CLI series:

Stay wired with our newsletter!
1

Recommended Articles

Stay Up To Date with
Cloud News and Tutorials!

Subscribe to our weekly newsletter!

By entering your email address, you agree to our privacy policy.