Getting started with Azure CLI

Getting started with Azure CLI

The Azure CLI is a cross-platform, command-line tool built-in Python that creates, updates, and removes nearly all Azure resources. It provides you a fairly easy-to-understand CLI that uses the various Azure Rest APIs behind the scenes.

Photo by Wes Hicks on Unsplash

Azure CLI via Azure Cloud Shell

If you’re using a Azure Cloud shell to use Azure CLI, you don’t need it in your local machine, One of the advantage of using Cloud Shell is it prevents you from having to authenticate with the Azure CLI. Since you’ve already authenticated via the Azure portal, Cloud Shell “passes on” that authentication to the Azure CLI, so everything just works.

Azure CLI Locally

If you choose to install and run the Azure CLI locally, you can install via local this and then you can authenticate the Azure CLI using the command az login. Once you authenticate, be sure to set the default subscription using az account set. If you don’t set the default subscription, you’ll have to specify the subscription for every command you issue.

Run the following command to set the default subscription changing the [subscription_name] placeholder with the name of your subscription.

az account set --subscription '[subscription_name]'

Commands:

The Azure CLI is built around the premise of commands. A command is an executable program that performs some action. The Azure CLI all stems from a single command called az. All commands you’ll run with the Azure CLI start with the az command.

az is the starting point that kicks off the Azure CLI, which you then specify a command group.

Finding Commands:

One of the best ways to discover what’s possible to use with the Azure CLI and the syntax is using the help system.

Every Azure CLI command group and command has a universal --help argument. The --help command shows you all options related to a command or group.

Notice that at the root level, the command group is actually the az command itself. All subgroups then spider off of az.

For example, if you are unsure of what you do not know at this point, then type the following command:

az --help

You will see that az returns all of the available groups and subgroups.

You can now drill down into any of the subgroups to explore more. Let’s say that you’d like to manage resource groups. Scrolling down a bit, you’ll see a subgroup called group. Provide that group as an argument to az with --help and see what happens. The command will be:

az group --help

Continue using --help after any command or command group to inspect what’s possible.

Getting Examples:

Examples are great ways to learn. Luckily, we have the az find command to discover actual examples of using various commands and command groups.

Once you’ve found the command group or the command you’d like to use, use az find to provide examples of how that group or command is used. For example, maybe you need to perform some action on a storage account. Try the following command to see some helpful examples.

az find "az storage"

Maybe you need to find some examples to create a resource group. Using --help, you’ve discovered that az group is the command group you need to use. Once you know the command group or command, use az findto drill down into various examples.

You can see that by providing the command group to az find first, you can find which commands to use (create). From there, you can provide that command to az find to show examples related to that command. For example, try the following commands:

az find "az group"  
az find "az group create"

Azure Scripting:

Even though the Azure CLI isn’t a scripting language like PowerShell, you can still build some handy scripts.

Since the Azure CLI is simply executable commands that are not depending on any one language, you can create shell scripts just like you would any other command. A Bash shell script is common to create Azure CLI scripts, but you can also combine Azure CLI commands in a PowerShell script or even a Windows batch file.

If you’re going to create scripts based on Azure CLI commands, you should use VS Code and install the Azure CLI Tools VS Code extension. This extension provides syntax coloring, various Intellisense features, and more in Azure CLI scripts.

Creating an Azure CLI script isn’t much different than running the commands on their own. You will still type up the commands to run, but do so in a script and save the script with an AZCLI extension. The AZCLIextension tells VS Code that the file is an Azure CLI script and to recognize it as such.

Azure CLI provides bunch of commands to perform tasks, easy to put in your Devops cycle or automation task. we have scratched the tip of iceberg, you can refer the docs in the reference section

References:

Azure REST API reference documentation
Welcome to the Azure REST API reference documentation. Representational State Transfer (REST) APIs are service…
docs.microsoft.com

Get started with Azure Command-Line Interface (CLI)
Welcome to the Azure Command-Line Interface (CLI)! This article introduces the CLI and helps you complete common tasks…
docs.microsoft.com