Getting Started with the Azure CLI

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Microsoft Azure is a cloud computing service that provides customers with scalable and secure solutions to run their applications.
This tutorial will get you started using the Microsoft Azure Command Line Interface (CLI), which allows your application code to connect to its services from any tool or programming language on any operating system. The CLI has comprehensive documentation available for developers, administrators and end users who are new or experienced in this technology.

The “azure cli commands” is a command line interface for the Azure cloud platform. The CLI offers commands that allow users to create, manage and deploy resources in Microsoft’s cloud.

Getting Started with the Azure CLI

You’re losing out if you have a Microsft Azure account and are still managing resources via the Azure Portal. Using the Azure CLI is one of the greatest methods to decrease clicks and enhance productivity.

The Azure CLI is a Python-based cross-platform tool that lets you control almost all of your Azure resources from the command line. The Azure CLI delivers a uniform experience across all platforms, regardless of whatever operating system you’re using.

You’ll learn how to get started with the Azure CLI in this lesson. Using different Azure CLI commands, you’ll learn how to do a basic setup and add and delete some popular resources.

Prerequisites

This page will serve as a guide. You’ll need at least one of the following items to follow along step-by-step:

  • This tutorial will use Azure CLI v2.20.0 on a Docker Container, however the commands will work on other platforms as well.

Related: Azure CLI Installation (Windows, Linux, macOS, Azure Shell)

  • Permissions to construct Azure resources using a Microsoft account and maybe a service principal.

Related:How to Create (And Understand) Azure Service Principals

Obtaining az assistance

Learn how to utilize the az help command before running any Azure CLI commands. The Azure CLI provides a wealth of information and assistance. Because this course will not be able to cover all instructions, understanding how to utilize az help is essential.

Let’s get started by exploring the help system and seeing where we can obtain more information on certain commands.

Using a Standard Help Example

The greatest learning resource is examples. Let’s look at how to utilize the Azure CLI help system by looking at how to locate the command to establish a resource group in Azure.

Open your terminal and type:

1. Type az help at the command prompt. There are two sorts of output from this command: groups and subgroups.

Commands are grouped together. Groups are often referred to as “commands” in tutorials (and even documentation). Each group represents an Azure service and is organized in a hierarchical manner.

az help's outputaz help’s output

2. Scroll down till you come across the group subgroup. As you can see from the description below, this subgroup is in charge of resource groups management. The alphabetical arrangement of the groups.

scrolling to locate the groupscrolling to locate the group

3. Once you’ve located the command you want, execute it using the —help flag. The —help argument has no effect; it just returns all of the help material for a group or command.

The group group and a single subgroup (lock) with different commands may be seen below.

support from the az groupsupport from the az group

4. The create command seems to be the correct method for Putting Together a Resource Group. Continue to narrow your search by attaching a more precise command to the initial az group reference and looking for assistance.

To access the help documentation for any command, use the —help argument.

The —help flag now provides assistance for the create command. As seen below, commands have parameters. You now have the inputs needed to define a resource group and necessary location in this example.

parameter assistanceparameter assistance

Examples of Syntax with az find

If you can’t easily find the group or command you’re looking for with the –parameter assistance, take a look at the az find command. This command searches for syntax examples using group/command references as shown below.

For example, instead of using —help to navigate the help system, use az find to do the same work in a single line.

az group create “az group find”

find command outputfind command output

Using az configure to provide a default configuration

There are several commands and functionalities available with the Azure CLI. Various options, such as logging and data collecting choices, are referenced by each of these features. When you first start using the Azure CLI, one of the first things you should do is execute the az configure command.

The az configure command leads you through three distinct settings in one command:

  • Format of Output — The Azure CLI delivers output in seven distinct formats.
  • All Azure CLI activities may be logged to a file.
  • To activate or stop providing information to Microsoft about how you use Azure CLI, go to Telemetry.

Assuming you already have the Azure CLI installed, here’s how to get started:

1. Switch to a terminal (Bash or PowerShell).

2. Type az configure at the command prompt. A basic menu will appear, informing you that all settings will be saved in the.azure/config file. To continue, press Y.

azconfigure's outputazconfigure’s output

3. The program will ask you for the default Format of Output you choose. Choose your preferred output here. After performing a command or getting an error message, the default Format of Output determines how your output will look on the screen.

If you're unsure, below you'll find some examples.If you’re not sure, look at the samples below.

The output parameter is used in the examples following. You may always change the default output using the output argument, regardless of the format you choose as the default.

JSON (Java Object Notation)

output in jsonoutput in json

JSONC (Java Object Notation with commentscolorization)

output jsoncoutput jsonc

Table

output from tableoutput from table

TSV (Tab-Separated Values)

output tsvoutput tsv

YAML stands for “You Ain’t Markup Language.”

output in yamloutput in yaml

YAMLC (YAML with commentscolorization)

1647492813_401_Getting-Started-with-the-Azure-CLIproduction of yamlc

None – Only errors or warnings will be returned by the Azure CLI.

4. The Azure CLI will ask you to activate logging once you’ve configured the Format of Output. To allow logging of all commands and output to a file, choose Y.

Format of OutputFormat of Output

If you choose Y, a log will be created with all of the commands and output gathered while using the CLI. In Linux and macOS, this log may be found in the $HOME/.azure/logs directory, while in Windows, it can be found in the percent USERPROFILE percent.azurelogs directory.

Below is an illustration of how the log may appear.

After running az login, record the contents of the file.After running az login, record the contents of the file.

5. The Azure CLI will then prompt you to transmit Microsoft Data from Telemetry. If you choose Y, Microsoft will get anonymous information about how you use the Azure CLI.

Data from TelemetryData from Telemetry

Refer to Microsoft’s privacy information on collecting anonymous data from Azure CLI for further information on the data it gathers.

Finally, az configure will prompt you to specify the time-to-live value for the CLI object cache. This should be left at the default value of 10.

cli object cache configuration cli object cache configuration

When debugging an Azure CLI problem, only alter the CLI object TTL if Microsoft recommends it.

Values for Default Parameters

Various commands in the Azure CLI have the same arguments, such as location and group. You may establish default parameter values rather than typing in the values for these parameters for each command.

Use the —defaults argument on az configure to establish parameter defaults. You may use this parameter to specify a parameter and its default value. Perhaps you’ve grown bored of putting in a location and group for numerous actions. No worries. Create a few default settings.

The default area (location) is westus2, and the resource group (group) is MyResourceGroup in the following example. When you execute any Azure CLI command with a location or group argument, Azure CLI will now send values to those parameters automatically.

—defaults az configure group=MyResourceGroup location=westus2

By defining the defaults in each container instance, you may use individual containers to manage particular regions, subscriptions, or resource groups if you’re utilizing containers. Using the command above, you may create four containers in each container and define a default region and default resource group.

Using a Browser for Authentication

You’ve studied the fundamentals of using the Azure CLI, but you haven’t done anything with it yet. Let’s get started on producing some materials today! However, you must first connect in to Azure using the command az login.

If you have multiple Azure subscriptions, run the az account list command to find all subscriptions. When you find the subscription, run az account set –subscription <subscription name> to set the subscription to use for the session. You can also use the global subscription parameter to explicitly specify a subscription.

1. Run az login from a terminal. This command launches a browser window that prompts you for your login and password.

in a Powershell session, az loginin a Powershell session, az login

2. As indicated below, enter your Azure account credentials or choose an existing user account.

Microsoft authenticationMicrosoft authentication

3. Close your web browser and return to your Azure CLI session after Azure has verified your account. The az login command confirms that you now have an authentication token by returning information about your subscription.

Once authenticated, output from az loginOnce authenticated, output from az login

Using a Device Code for Authentication

Use the —use-device-code flag if you’re using a system without a browser or if you wish to download an authentication token to another machine. This option enables a user to authenticate using a code provided by another device.

To use a device code to authenticate, open a terminal and type:

Use the —use-device-code flag to run az login. This time, the az login command will not launch a browser. Instead, it will give you a code to enter on another computer’s device login page.

—use-device-code az login

az-login —use-device-code outputaz-login —use-device-code output

2. Go to https://microsoft.com/devicelogin on a separate computer, paste in the code, and click Next.

To authenticate, enter the code.To authenticate, enter the code.

3. Now, to login to Azure, input your credentials and, if necessary, shut your browser.

selecting an account to use for authenticationselecting an account to use for authentication

az login will return your subscription details when you return to the Azure CLI session.

verification Your azure session is active.verification Your azure session is active.

Output Options for Azure CLI

As previously stated, the Azure CLI may deliver output in a variety of formats. With the output option, you may set the default output and alter it on the fly. However, the output parameter is not the only one that affects output behavior.

Use the following settings to vary the verbosity, filter output, or modify error output behavior.

  • query — Filters output for all commands using the JMESPath query language. To understand more, go to this article.
  • verbose — Returns detailed information on Azure resources as well as additional information about those resources.
  • debug — Returns a minimal amount of information regarding debugging CLI activities.
  • only-display-errors Show warnings and hide mistakes.

Putting Together a Resource Group

Now that you’re authenticated to Azure, you’re free to manage all of the resources you have permission to. Let’s first run through a common example of Putting Together a Resource Group.

Assuming you’re using a machine that has the Azure CLI installed and you’ve already authenticated to Azure with the chevalier chevalier chevalier chevalier chevalier chevalier chevalier chevalier chevalier chevalier

As demonstrated below, use the az group create command to establish a resource group. This command creates the TESTGrp resource group in the North Europe location and outputs full verbose output.

—location northeurope —resource-group TESTGrp —verbose az group create

produce az group's outputproduce az group’s output

2. Verify that Azure has generated the resource. list of az groups

You’ll see that the TESTGrp resource group has been formed.

az group list outputaz group list output

3. You may have a large number of resource groupings to sort through. Use the query parameter to filter down the list instead of scrolling. Using JMESPath, the command below only returns resource groups in the North Europe location.

—query “[?location==’northeurope’]” az group list

a JMESPath query's outputa JMESPath query’s output

Creating A Virtual Linux Machine

Learn how to use the Azure CLI to construct an Azure VM in this last example. It’s a good idea to understand how to create virtual machines and how to configure them. Creating Azure virtual machines is a frequent process that you may encounter many times throughout your cloud journey.

Create an Azure Linux VM with default settings and deploy it to the resource group you established in the last part in this exercise.

A disk image, virtual network adapter, virtual machine, and other Azure resources all work together to create an Azure VM.

To use the Azure CLI to build an Azure VM, follow these steps:

To discover all available VM images for the VM, use the az vm image list command. This command displays all cached and offline photos. vm image list az

Choose the UbuntuLTS image as shown below for this demonstration.

Available picture list cached Available picture list cached

The Azure CLI keeps a cached list of all virtual machine images. Use the all option to receive an updated list if the UbuntuLTS image does not appear.

2. Create the VM using az vm create once you know which disk image to use. The code below creates a VM named ATAVM1 in the resource group TESTGrp using the UbuntuLTS image. Azure will produce SSH keys (generate-ssh-keys) to connect to the VM later and deliver output in JSON when the VM is created (output).

az vm create —resource-group TESTGrp —name ATAVM1 ## virtual machine hostname —image UbuntuLTS ## image name acquired using az vm image list —generate-ssh-keys ## Auto creates SSH keys for use with VM —output json ## override for format output to return JSON —verbose ## additional output

Using a trailing backslash, split a single lengthier Azure CLI command into several lines.

There are many more options for configuring a VM using the az vm create command. Remember, the assistance system is your ally! —help az vm create

If the command was properly executed, you should see something like the image below.

Creating a virtual machine Creating a virtual machine

You may see the exact REST API method and arguments used in green if you use the verbose setting.

3. When the deployment is complete, the result will include information about your VM in the final part. Copy the public IP address, especially for this demonstration. This is required to connect to the VM through the Internet.

the result of vm creationthe result of vm creation

Connect to the Ubuntu VM through SSH using the VM’s public IP address, which may be discovered above. Open your chosen SSH client and login to the VM as azureuser, as seen below. The az vm create command creates a local user named azureuser by default.

Setting up SSH on Linux is related.

linked to the virtual machine linked to the virtual machine

Because you utilized the generate-ssh-keys argument when constructing the VM, you were able to SSH into it without a password. This uploads the public key to the VM and downloads the private key to your /.ssh directory, enabling you to authenticate.

5. Now it’s time to clear out the VM and resource group.

—name TestGRP az group remove

Steps to Follow

You should now be able to use the Azure CLI. You’re all set to handle some assets! So, what comes next?

The “azure cloud shell” is a command-line tool that allows users to interact with Azure’s services. This article will help you get started with the Azure CLI.

Related Tags

  • install azure cli
  • azure cli commands list
  • how to use azure cli
  • azure cli linux
  • azure cli windows

Table of Content