Introduction to the Azure CLI#

Overview

Learner Questions

  • How do I use the Cloud CLI?

  • How do I use az?

Learning Objectives

  • Find and use the “Cloud Shell” in the web console

  • Use basic cloud CLI commands (az)

  • Determine basic user/account settings.

  • Use environment variables for configuration - No hard coding

  • Understand the importance of using variables for configuration.

  • Introduce value of reproducibility and automation.

Resources

Cloud Automation#

Now that Drew can create a bucket in the web console they want to learn how to use tools to automate the process. Drew noticed that the web console could be used to upload and share objects but wishes to learn the command line so that they can automate and document the process (it is much easier to document commands then describing and showing mouse clicks).

Open the Cloud Shell#

The Cloud Shell is Debian running in a container with 5GB of no-charge persistent storage with most of the Azure tools and a lot of other useful tools (git, docker, jq, Docker, etc.) installed by default.

To use the Azure Cloud Shell:

  • Open up a Cloud Shell by clicking on the cloud shell icon (greater than sign followed by underscore) on the blue bar just to the right of search window. On first launch, Cloud Shell prompts to create a resource group, storage account, and Azure Files share on your behalf. This will open a cloud shell at the bottom of your browser window. You can choose a bash shell or Power shell.

  • If you want a new cloud shell in a different window, click the new cloud shell button (file icon with + in upper right corner) on the cloud shell menu bar. This will create a new cloud shell window tab. Note: you may need to allow popups or enable popups for this website depending on the browser you use. You can also pull up the tab to increase the shell screen size.

  • To quit cloud shell type exit at the prompt.

Exercise#

Take a couple of minutes to explore your new Cloud Shell environment.

  • How much RAM is available?

  • How much storage is available to you?

  • What is the internal IP address of the machine?

  • Install your favorite editor if it is not installed.

Basic Cloud Commands#

The cloud can also be controlled using a command line interface (CLI) or a programming language such as Python. Collectively these tools interact with the cloud over a Application Programming Interface (API) and this capability forms the basis of the cloud, the ability to control infrastructure programmatically.

For information about installing the Azure CLI for your OS see: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli. If you are on your own OS you will need to login via az login.

The az command is used to control most aspects of Azure Cloud.

Now let us look at your account information (your details will be different).

Update packages of the VM#

First make sure that the VM is up to date with the latest versions of pachages and security patches by running the following commands.

sudo apt-get update -y && sudo apt-get upgrade -y

Install the azure CLI by executing the the curl command below.

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

login into azure

az login

You will be prompted got to https://microsoft.com/devicelogin and enter the code (string of letters & numbers) listed. Open another tab in your browser, go to the URL above and paste in the code. Follow the steps to verify login.

  • Verify your account

  • Press Continue - Are you trying to sign in to CLI?.

  • Close the tab

Environment Variables#

When using the CLI you quickly notice that you have to enter many of the details, such as a Resource Group, many times. Not only is this tedious, it also makes moving to another project difficult making reusability and collaboration painful. We will demonstrate the use environment variables to address these issues. As a reminder, environment variables used to store configuration information that can be easily passed between programs and programming languages. Most cloud environments and tools rely heavily on this technique.

We now demonstrate the basic use environment variables.

TEST=1
echo $TEST

Although we do not always need to, we should always enclose shell variables in double quotes and enclose the variable name in curly braces (funny things may happen otherwise). In most cases it is easier to do this every time than try to figure out when it is possible to use the short form. We only use the short form here when it is used frequently and known to always work.

There are a number of environment variables already set that contain useful information, for example:

echo "${USER}"
echo "${HOSTNAME}"

We can also use the output of commands to set string values.

date
echo "Date: $(date)"

Define some environment variables from the resources created in previous lessons.

First, as with all Azure work we need to create a Resource Group (RG). A resource group is a logical container into which Azure resources are deployed and managed.

SUBSCRIPTION="<Subscription id>""
RG="<Resource Group>"
LOCATION="eastus"
STORAGE_ACCT="<Storage Account>"
BLOB_CONTAINER="<Blob Container>"
echo $RG

Now use the RG environment variable to get information about your resource group. This information is available at any time with the following command not just at creation.

az group show --resource-group $RG

Exercise#

FIXME:

  • What command shows the project number? (hint: we used it already)

  • What is the name of this variable? (hint: it is case sensitive)

  • What command shows just the Project Number (hint: use –format)

  • Set the PROJECT_NUMBER environment variable with the correct value. (hint: be careful with punctuation and matching punctuation)

Advanced Call-Out: Formating Output.#

The following example shows how to output the configuration as JSON.

az group list --output json