DEV Community

yaswanthteja
yaswanthteja

Posted on

What is the Azure CLI?

The Azure CLI is a command-line program to connect to Azure and execute administrative commands on Azure resources. It runs on Linux, macOS, and Windows and allows administrators and developers to execute their commands through a terminal or command-line prompt (or script!) instead of a web browser. For example, to restart a virtual machine (VM), you would use a command like the following:

az vm restart -g MyResourceGroup -n MyVm
Enter fullscreen mode Exit fullscreen mode

The Azure CLI provides cross-platform command-line tools for managing Azure resources, and can be installed locally on Linux, Mac, or Windows computers. The Azure CLI can also be used from a browser through the Azure Cloud Shell. In both cases, it can be used interactively or scripted. For interactive use, you first launch a shell such as cmd.exe on Windows or Bash on Linux or macOS and then issue the command at the shell prompt. To automate repetitive tasks, you assemble the CLI commands into a shell script using the script syntax of your chosen shell and then execute the script.

How to install the Azure CLI

On both Linux and macOS, you use a package manager to install the Azure CLI. The recommended package manager differs by OS and distribution:

  • Linux: apt-get on Ubuntu, yum on Red Hat, and zypper on OpenSUSE
  • Mac: Homebrew The Azure CLI is available in the Microsoft repository, so you'll first need to add that repository to your package manager.

On Windows, you install the Azure CLI by downloading and running an MSI file.

Using the Azure CLI in scripts

If you want to use the Azure CLI commands in scripts, you need to be aware of any issues around the "shell" or environment used for running the script. For example, in a bash shell, the following syntax is used when setting variables:

variable="value"
variable=integer
Enter fullscreen mode Exit fullscreen mode

If you use a PowerShell environment for running Azure CLI scripts, you'll need to use this syntax for variables:

$variable="value"
$variable=integer
Enter fullscreen mode Exit fullscreen mode

The Azure CLI must be installed before it can be used to manage Azure resources from a local computer. The installation steps vary for Windows, Linux, and macOS, but once installed, the commands are common across platforms.

Let's install the Azure CLI on your local machine, and then run a command to verify your installation. The method you use for installing the Azure CLI depends on the operating system of your computer. Choose the steps for your operating system.

Windows

Here you will install the Azure CLI on Windows using the MSI installer.

Go to https://aka.ms/installazurecliwindows, and in the browser security dialog box, click Run.
In the installer, accept the license terms, and then click Install.
In the User Account Control dialog, select Yes.

Running the Azure CLI

You run the Azure CLI by opening a bash shell (Linux and macOS), or from the command prompt or PowerShell (Windows).

Start the Azure CLI and verify your installation by running the version check.

az --version
Enter fullscreen mode Exit fullscreen mode

You set up your local machines to administer Azure resources with the Azure CLI. You can now use the Azure CLI locally to enter commands or execute scripts. The Azure CLI will forward your commands to the Azure datacenters where they will run inside your Azure subscription.

Linux

Here you will install the Azure CLI on Ubuntu Linux using the Advanced Packaging Tool (apt) and the Bash command line.

The commands listed below are for Ubuntu version 18.04. Other versions and distributions of Linux have different instructions. Follow the instructions in Install the Azure CLI if you are using a different Linux version or have any problems.

Modify your sources list so that the Microsoft repository is registered, and the package manager can locate the Azure CLI package

AZ_REPO=$(lsb_release -cs)
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | \
sudo tee /etc/apt/sources.list.d/azure-cli.list
Enter fullscreen mode Exit fullscreen mode

Import the encryption key for the Microsoft Ubuntu repository. This will allow the package manager to verify that the Azure CLI package you install comes from Microsoft.

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
Enter fullscreen mode Exit fullscreen mode

Install the Azure CLI.

sudo apt-get install apt-transport-https
sudo apt-get update && sudo apt-get install azure-cli

Enter fullscreen mode Exit fullscreen mode

Running the Azure CLI
You run the Azure CLI by opening a bash shell (Linux and macOS), or from the command prompt or PowerShell (Windows).

Start the Azure CLI and verify your installation by running the version check.

az --version
Enter fullscreen mode Exit fullscreen mode

macOS

Here you will install the Azure CLI on macOS using the Homebrew package manager.
Update your brew repository to make sure you get the latest Azure CLI package.

brew update
Enter fullscreen mode Exit fullscreen mode

Install the Azure CLI.

brew install azure-cli
Enter fullscreen mode Exit fullscreen mode

Running the Azure CLI
You run the Azure CLI by opening a bash shell (Linux and macOS), or from the command prompt or PowerShell (Windows).

Start the Azure CLI and verify your installation by running the version check.

az --version
Enter fullscreen mode Exit fullscreen mode

Work with the Azure CLI

The Azure CLI lets you type commands and execute them immediately from the command line. Recall that the overall goal in the software development example is to deploy new builds of a web app for testing. Let's talk about the sorts of tasks you can do with the Azure CLI.

*What Azure resources can be managed using the Azure CLI?
*

The Azure CLI lets you control nearly every aspect of every Azure resource. You can work with resource groups, storage, virtual machines, Azure Active Directory (Azure AD), containers, machine learning, and so on.

Commands in the CLI are structured in groups and subgroups. Each group represents a service provided by Azure, and the subgroups divide commands for these services into logical groupings. For example, the storage group contains subgroups including account, blob, and queue.

So, how do you find the particular commands you need? One way is to use az find, the AI robot that uses the Azure documentation to tell you more about commands, the CLI and more.

Example - find the most popular commands related to the word blob.

az find blob
Enter fullscreen mode Exit fullscreen mode

Example - Show me the most popular commands for an Azure CLI command group, such as az vm.

az find "az vm"
Enter fullscreen mode Exit fullscreen mode

Example - Show me the most popular parameters and subcommands for an Azure CLI command.

az find "az vm create"
Enter fullscreen mode Exit fullscreen mode

If you already know the name of the command you want, the --help argument for that command will get you more detailed information on the command, and for a command group, a list of the available subcommands. So, with our storage example, here's how you can get a list of the subgroups and commands for managing blob storage:

az storage blob --help
Enter fullscreen mode Exit fullscreen mode

How to create an Azure resource

When creating a new Azure resource, there are typically three steps: connect to your Azure subscription, create the resource, and verify that creation was successful. The following illustration shows a high-level overview of the process.

Image description

Each step corresponds to a different Azure CLI command.

Connect
Since you're working with a local install of the Azure CLI, you'll need to authenticate before you can execute Azure commands, by using the Azure CLI login command.

az login
Enter fullscreen mode Exit fullscreen mode

The Azure CLI will typically launch your default browser to open the Azure sign-in page. If this doesn't work, follow the command-line instructions and enter an authorization code at https://aka.ms/devicelogin

After a successful sign in, you'll be connected to your Azure subscription.

Create
You'll often need to create a new resource group before you create a new Azure service, so we'll use resource groups as an example to show how to create Azure resources from the CLI.

The Azure CLI group create command creates a resource group. You must specify a name and location. The name must be unique within your subscription. The location determines where the metadata for your resource group will be stored. You use strings like "West US", "North Europe", or "West India" to specify the location; alternatively, you can use single word equivalents, such as westus, northeurope, or westindia. The core syntax is:

az group create --name <name> --location <location>
Enter fullscreen mode Exit fullscreen mode

Verify
For many Azure resources, the Azure CLI provides a list subcommand to view resource details. For example, the Azure CLI group list command lists your Azure resource groups. This is useful here to verify whether creation of the resource group was successful:

az group list
Enter fullscreen mode Exit fullscreen mode

To get a more concise view, you can format the output as a simple table:

az group list --output table
Enter fullscreen mode Exit fullscreen mode

Top comments (0)