Azure CLI: text commands for freedom and control

The Azure Command-Line Interface (CLI) is a tool that, through the use of text commands, allows users to create, configure and monitor cloud resources in an efficient and automated way, eliminating the need for graphical interfaces and facilitating integration with scripts and DevOps processes. Compatible with different operating systems, Azure CLI is dedicated to developers, system administrators and IT professionals who seek to optimize their work in the cloud and offers, in addition to great flexibility, also a learning curve accessible to the less experienced. In this article, we're going to take a closer look at how it works and what the essential commands are, and we'll also take a moment to talk about Developer CLI and the differences between Azure CLI and PowerShell.

What you'll find in this article

  • What is Azure CLI
  • Azure CLI: How does it work?
  • Azure CLI: How to install it?
  • Azure CLI Commands: examples of essential commands
  • Azure Developer CLI: the specialized interface for developers
  • Azure CLI vs. PowerShell: What are the differences?
Azure CLI: text commands for freedom and control

What is Azure CLI

If you are new to Microsoft Azure, your journey most likely started working within the Azure Portal, perhaps creating resources such as virtual machines and storage accounts using distribution wizards.

However, this process is tedious and doesn't scale when you need to create a lot of resources. To take the development of your software solutions to the next level, it is necessary to have a much higher degree of freedom and control, which software based on the use of a graphical interface often does not offer.

Azure CLI (short for Command-Line Interface) is a robust command-line tool provided by Microsoft for managing Azure resources. It offers a fast, flexible, scriptable way to interact with Azure services and resources, allowing users to efficiently automate tasks and manage infrastructure in the Azure cloud.

Users can use this platform to script a variety of operations in the Azure portal, such as creating or deleting Azure resources, including virtual machines or backing up SQL databases.

But how does it work specifically? Let's take a closer look at it.

Azure CLI: How does it work?

Before we begin our overview, let's take a moment to better understand what a CLI is and how it works.

A CLI, or Command-Line Interface (Command Line Interface), is a user interface that allows users to interact with a computer or software system by typing text commands. Unlike graphical interfaces (GUIs), which use visual elements such as windows, icons, and menus, a CLI requires the user to enter specific commands through a keyboard.

In the CLI, the interaction therefore takes place only textually, with the commands inserted sequentially one after the other. This type of interface allows more technical users to easily write scripts, or sequences of commands, to automate repetitive tasks.

A command line interface therefore offers those who know how to use it a high level of control and freedom and allows them to perform advanced and complex operations that are often not possible with GUIs and can be much faster and more efficient for performing complex operations.

Azure CLI does exactly that for Azure services and gives developers freedom and control to come up with more technically complex solutions for all Microsoft cloud platform services, including virtual machines, storage accounts, databases, virtual networks, and more. Users can create, manage, and monitor resources directly from the command line, without the need to interface with a GUI.

In order to use it, after installing it, you run it through a shell (a program that acts as an intermediary between the user and a computer's operating system), such as CMD on Windows or Bash on Linux or MacOS, and then you type a command at the shell prompt.

The Azure CLI can be installed locally on Linux, Mac, or Windows systems. It can also be accessed through a browser using the Azure Cloud Shell or within a Docker container.

Did you know that we help our customers manage their Azure tenants?

We have created the Infrastructure & Security team, focused on the Azure cloud, to better respond to the needs of our customers who involve us in technical and strategic decisions. In addition to configuring and managing the tenant, we also take care of:

  • optimization of resource costs
  • implementation of scaling and high availability procedures
  • creation of application deployments through DevOps pipelines
  • monitoring
  • and, above all, security!

With Dev4Side, you have a reliable partner that supports you across the entire Microsoft application ecosystem.

Azure CLI: How to install it?

To be able to use Azure CLI on your system, all you have to do is go through a simple installation process followed by a quick configuration to connect to your Azure account. Here we'll dive into the installation steps for different operating systems and the initial configuration steps to make sure you're ready to manage Microsoft Azure resources using the CLI in no time.

Windows

  • Access the official Azure CLI download page (here).

  • Download the MSI file available for Windows.
  • Run the MSI file and follow the installation wizard. The installer is designed to guide users through all the necessary steps, including choosing the installation directory.
  • Once installed, just open Command Prompt or PowerShell and type az to start using Azure CLI.

MacOS

  • The preferred method for installing the Azure CLI on macOS is to use Homebrew, a package manager for macOS.
  • If Homebrew isn't installed on your system, you can install it by pasting the following command into a macOS Terminal:
/bin/bash -c “$ (curl -fSSL https://raw.githubusercontent.com/Homebrew/
install/HEAD/install.sh)”
  • After installing Homebrew, simply install Azure CLI using the following command in the terminal:
brew update && brew install azure-cli

Linux

  • The installation process on Linux depends on the specific distribution. Azure CLI can be installed using package managers such as apt for Debian/Ubuntu-based distributions, yum for Red Hat-based distributions, or zypper for SUSE-based distributions.
  • For Debian/Ubuntu-based distributions, you can install the Azure CLI by running the following commands:
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
  • For Red Hat-based distros you can use:
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e “[azure-cli]\nname=Azure CLI\nbaseurl= https://packages.microsoft.com/
yumrepos/azure-cli\nenabled=1\ngpgcheck=1\ngpgkey= https://packages.
microsoft.com/ keys/microsoft.asc” > /etc/yum.repos.d/
azure-cli.rel'
sudo dnf install azure-cli
  • Each command downloads and runs a script that adds the Azure CLI software repository and installs it on the device.

Azure CLI: initial configuration

After installing the Azure CLI, the next step is to configure it by logging in to your Azure account:

  • Open a command line interface window that can be the Command Prompt or PowerShell on Windows, the Terminal on macOS, or any terminal emulator on Linux.
  • Type the following command and press Enter:
Log in
  • This command will open the system's default web browser and ask you to sign in with your Azure account credentials. If you are unable to use a web browser, you can log in using a device code following the instructions provided by the CLI.
  • Once you log in, the CLI will show information about your subscription. If you have multiple Azure subscriptions, you will need to specify which one you want to use. You can list all available subscriptions with the command:
az account list --output table
  • To set a default account to be used in CLI sessions, you can use this command:
<Subscription Name or ID>az account set --subscription "”

Azure CLI Commands: examples of essential commands

Azure CLI offers an incredible amount of commands that allow you to interact with the equally numerous Azure services. Given the size, it will not be possible to cover them in their entirety; therefore, we will limit ourselves to introducing only a few fundamental commands, essential for anyone starting to use Azure CLI.

These commands cover working with resource groups and obtaining information on various Azure services and will allow those who want to start using it to have a little clearer idea of how to do it.

Help

Before we begin, however, let's take a look at what is undoubtedly one of the most useful Azure CLI commands, namely 'help'.

The help command is a very useful tool for obtaining detailed information about the available commands, their topics and options, and its main function is to provide assistance and documentation directly on the command line, thus facilitating the use of the CLI.

When you use the 'az help' command, you get a general overview of the Azure CLI, including the main commands and extensions available, extremely useful for finding your way around and understanding which commands are available for use.

You can also obtain detailed information about a specific command by adding help after the command of interest. This provides details about the command topics, the options available, and often includes examples of use. Below is an example of this possible use:

AZ Group Help

To get details about a specific action of a command, you can further extend the use of help. This is useful for exploring all the possible actions and parameters of a given command. Let's take up the example above and see how the syntax changes:

AZ Group Create Help

You can also use the '—help' syntax with any command to get detailed information. This works identically to adding help at the end of the command. Below is another example:

Do vm create --help

Managing Resource Groups

Resource groups are containers that host related resources for an Azure solution. Here's how to quickly create and manage resource groups using the Azure CLI:

  • Create a Resource Group
<Insert Name>az group create --name --location <Insert Regione>

This command will create a new Resource Group. Remember to enter a name for the resource and its location in the command based on the Azure region of the account.

  • List Resource Groups: To see all the resource groups in your subscription (complete with locations and other properties) in a convenient table format, just write
az group list --output table
  • Delete a Resource Group: This command will delete the chosen resource group and all the resources contained within it.
az group delete --name --yes <InsertResourceName>--no-wait

The --yes flag confirms the operation, and --no-wait tells the CLI not to wait for the operation to complete before returning to the prompt.

View and manage Azure services and resources

As we already mentioned at the beginning of the section, Azure CLI can interact with practically all Azure services. Let's take a couple of examples to better understand how:

  • Make a list of all Virtual Machines (VMs) in a subscription:
Az vm list --output table

This command will list all the vms in the selected default subscription, showing their names, associated resource groups, and locations.

  • Make a list of all storage accounts:
az storage account list --output table

Similar to the previous command, it will show a table of all storage accounts showing their associated names, locations, and resource groups.

Working with Virtual Machines (VMs)

Azure Virtual Machines (VMs) are one of the most commonly used resources. They allow you to deploy and manage virtual servers in the cloud. Here's how you can manage VMs using Azure CLI:

  • Create a Virtual Machine: Creating a VM includes specifying details such as the associated resource group, VM name, image, and administrative credentials. The example below is to give you an idea of how to create a Linux VM
Do vm create\
 <InsertResourceName>--resource-group\
 --name MyVM\
 --image UbuntuLTS\
 <InsertUserName>--admin-username\
 --generate-ssh-keys
  • Starting/restarting a VM:
<InsertResourceName>az vm start --resource-group --name MyVM
  • Stop a VM: Interrupting a VM's work can help save resources and money when it's not in use
<InsertResourceName>az vm stop --resource-group --name MyVM

Deploying and managing Web Apps

Azure CLI can also be used to manage Apps created and distributed through the Azure App Service service. Here are also some examples of usable commands:

  • Create an App Service Plan: App Service Plans specify the location, size and characteristics of the web server farm that hosts your app. To create one, just use the following command
<InsertResourceName>az appservice plan create --name <InsertPlanName>--resource-group --sku F1 --location <InsertRegion>

(Please specify that this command specifically creates a free tier Service Plan).

  • Create a web app: Once our App Service Plan has been created, we can proceed to create the web app:
<InsertResourceName><InsertPlanName>az webapp create --resource-group --plan --name <InsertAppName>

Manage Azure Storage accounts

Azure CLI can also be used to manage and upload files to your Azure Storage accounts. Again, let's take a couple of more concrete examples to see how it works in this context:

  • Create a Storage Account:
az storage account create\
 <InsertStorageAccountName>--name\
 <InsertResourceName>--resource-group\
 <InsertRegion>--location\
 --SKU standard_LRS

With this specific command, we are going to create a storage account that uses “Standard_LRS” (Locally-redundant storage) as a redundancy option in the desired region.

  • Upload a file to a Blob container:
Do storage blob upload\
 <InsertStorageAccountName>--account-name\
 <InsertContainerName>--container-name\
 <InsertFileName>--name\
 --file <InsertPathToFile>

Before using the command, make sure that there is a container in your storage account.

Azure Developer CLI: the specialized interface for developers

The Azure Developer CLI is a primarily developer-oriented tool. This tool is designed to create and deploy Azure applications and infrastructure using just a few commands. It is like Azure CLI, an open-source tool and has two main objectives: to reduce development time and to help developers understand the fundamental concepts of developing on Azure.

Both allow you to distribute services and infrastructures on Azure but Azure CLI is a more general tool that allows you to manage and control Azure resources directly from the terminal, with a lot of freedom to modify, change and have more detailed control over each command executed.

The Azure Developer CLI, on the other hand, is a more developer-oriented tool, focused on accelerating distributions by having many aspects already connected for the user within a single application, giving developers the opportunity to have less concern about the distribution process.

A typical use case of the Azure Developer CLI is if you have a complete application consisting of multiple complex services that connect to each other. In this particular case it would be a very difficult task to configure them manually in the portal or through many commands through Azure CLI.

The handful of developer-friendly commands that can be used within this interface allows you to take care of all the key steps in a developer's workflow without having to get lost in the manual configuration of every single connection and service and can be particularly useful if you are working in a team and you want to be sure that everyone is using the same infrastructure and app code.

Overview of Azure Developer CLI

Azure CLI vs. Azure PowerShell: What are the differences?

Azure CLI and PowerShell are two of the main tools for managing and automating Azure services, but even if they appear to be the same tool, they couldn't be more different. In fact, many novice users may confuse them because both are command-line based, but the difference between Azure CLI and PowerShell is the equivalent of the difference between a program and an operating system. Let's take a better look at it.

Azure CLI, as we have already seen, is a tool designed specifically to work with Azure services. It is focused on interacting with the Azure platform through a series of commands with an intuitive and uniform syntax (facilitating the learning and use of the tool), regardless of the operating system in use and to be used it needs to be installed on a shell from which it will then be executed.

PowerShell, on the other hand, is a command-line shell and a scripting language created by Microsoft, initially to manage Windows systems but then extended to other environments, becoming compatible with Windows, macOS and Linux after the introduction of PowerShell Core, significantly expanding its reach.

The latter, unlike CLI, is a real command console (like Linux bash or the old Windows CMD) and offers a syntax based on commands and objects, which means that the results of operations are treated as objects and not as simple text and is often used for managing Windows environments and for the automation of more general activities.

Powershell integrates Azure-specific cmdlets, which offer greater depth of control and customization than the basic Azure CLI commands (albeit at the cost of greater complexity and difficulty in writing and reading) but its functionality is not limited to Azure alone, making it particularly suitable for complex scenarios where it is necessary to integrate Azure with other system operations and complex scripts.

In actual practice, many users use a combination of Azure CLI and PowerShell based on their specific needs and preferences, and these tools are complementary rather than exclusive to each other, and the choice often depends on the task at hand. When choosing which command-line tool is most suitable, you should therefore consider your experience and the type of environment in which you work to be able to exploit the two to their full potential.

Conclusions

The possibility of having greater freedom and control offered by command line interfaces is the reason why, after many more years, many experienced developers prefer them over solutions with a graphic interface that, although more user-friendly and easy to use, could be subject to uncomfortable limitations.

With its offer, Azure CLI has essentially made the work of developers who prefer the use of this type of more minimal interfaces much faster, more understandable and more concise, managing to guarantee them the same degree of control and freedom of action over their work with greater ease and less effort.

With its combination of versatility and intuitive, no-frills language, as well as the ability to use it on multiple platforms (and even browsers) without compatibility problems with different shells, the Azure command line interface has become over time one of the most popular solutions for developers working on Microsoft's cloud computing platform. So why not put it to the test and discover its potential for yourself?

FAQ on Azure CLI

What is Azure CLI?

Azure CLI is a command-line tool provided by Microsoft to manage Azure resources. It enables efficient automation and management of cloud infrastructure through text commands, offering more control compared to graphical interfaces.

What is Azure Developer CLI?

Azure Developer CLI is a specialized tool for developers, focusing on simplifying the development and deployment of Azure applications, offering streamlined commands for managing complex services and infrastructure.

How does Azure CLI work?

Azure CLI operates through text commands in a terminal or shell, allowing users to create, configure, and monitor Azure resources. It can be installed on Windows, macOS, or Linux, and used locally or via a browser through Azure Cloud Shell.

How do I install Azure CLI on Windows?

To install Azure CLI on Windows, download the MSI installer from the official Azure CLI download page, run it, and follow the installation wizard. Once installed, open Command Prompt or PowerShell and type az to start using Azure CLI.

Can I use Azure CLI on macOS?

Yes, you can install Azure CLI on macOS using Homebrew. First, install Homebrew, then use the command brew update && brew install azure-cli in Terminal to install Azure CLI.

What are some essential Azure CLI commands?

Some essential Azure CLI commands include:

  • az group create to create a resource group.
  • az vm list to list virtual machines.
  • az storage account list to list storage accounts.
What is the difference between Azure CLI and PowerShell?

Azure CLI is specifically designed for managing Azure services with a consistent command structure, while PowerShell is a more general-purpose shell and scripting language with deep integration into Windows environments.

Find out why to choose the team

Infra & Sec

The Infra & Security team focuses on the management and evolution of our customers' Microsoft Azure tenants. Besides configuring and managing these tenants, the team is responsible for creating application deployments through DevOps pipelines. It also monitors and manages all security aspects of the tenants and supports Security Operations Centers (SOC).