Getting Started with the Azure Command Line Interface

Introduction

In this tutorial, we’ll get up and running with Command Line Interface (CLI) tools that allow us to work with Microsoft’s Azure cloud services. Using the CLI, we’ll be able to create resources in the cloud without having to leave the comfort of the shell.

ReQUIREMENTS

Before we get started, there are just a few things you’ll need to have in place:

  1. An Azure account with admin or power user privileges. Ideally, this should be a test or training account that is not associated with a production environment. We’ll be creating and deleting resources and it would be unfortunate to remove anything that should remain up and running!
  2. Access to a shell environment with an active internet connection.
  3. It will be handy to install `jq`, a command line application that helps parse JSON output.
Installing the Azure CLI Tool

Azure CLI is available for Linux, macOS, and Windows. The install is simple on each of these platforms; you’ll either run a curl command to run an install script or install a native binary from a package.

On Linux and macOS systems, you’ll need to have Python installed. You can check that by typing `which python` in your shell. Once you’ve confirmed that Python is installed, you can install the Azure CLI with the following curl command:

curl -L https://aka.ms/InstallAzureCli | bash

Note: On some Linux systems, GCC and development libraries also need to be installed to properly install Azure CLI. If you are installing on Linux and get the following error:

error: command 'gcc' failed with exit status 1

you can fix this on CentOS/RHEL system (for example), by running:

yum install -y gcc libffi-devel python-devel openssl-devel

To install Azure CLI for Windows, download the MSI from the following location and run the installation: https://aka.ms/InstallAzureCliWindows

Once Azure CLI is installed on your system, you can confirm the app is correctly installed by running `az`. You should see a welcome message along with a list of commands:

$ az
Welcome to Azure CLI!
---------------------

Use `az -h` to see available commands or go to https://aka.ms/cli.

Now that we have the Azure CLI tool installed, let’s get logged in.

Logging In

Once your environment is set up, log in using the `az login` command. Running the command returns a URL and a code:

$ az login
To sign in, use a web browser to open the page https://aka.ms/devicelogin and enter the code WXYZ12345 to authenticate.

Open the URL in a browser and enter the code. If you are not already logged in, you’ll be prompted to log into your Azure account. Once you are successfully logged in, you can go back to the terminal. You should see output that shows you are successfully logged in:

$ az login
To sign in, use a web browser to open the page https://aka.ms/devicelogin and enter the code WXYZ12345 to authenticate.
[
{
"cloudName": "AzureCloud",
"id": "2b97ffe3-a4ad-4f78-8d71-3494bf3c731d",
"isDefault": true,
"name": "Developer Program Benefit",
"state": "Enabled",
"tenantId": "abcdefghi-99aa-1234-5678-a123456789fff",
"user": {
"name": "your.user.name@your.domain.com",
"type": "user"
}
}
]

After successfully logging in, you can start using various `az` commands to manage resources in Azure. But first, we need to create a resource group.

Creating a Resource Group

A resource group is a container for resources that should be logically grouped together. One example is a flight of instances, their associated databases, and storage that all together make up an application or service. You can also consider a resource group to be like a project for a web application or something similar.

Resource groups also make it easy to manage a collection of items in terms of permissions, creation, and deletion. We’ll see later how removing a resource group automatically removes all of the VMs, databases, etc. that are contained within the group.

Creating a resource group requires passing an Azure region to the command. Presently, Azure is generally available in 34 regions around the world, with plans announced for 4 additional regions.

We can get a list of all the regions with the following command:

$ az account list-locations | jq '.[].name' | sort
"australiaeast"
"australiasoutheast"
"brazilsouth"
"canadacentral"
"canadaeast"
"centralindia"
"centralus"
"eastasia"
"eastus"
"eastus2"
"japaneast"
"japanwest"
"koreacentral"
"koreasouth"
"northcentralus"
"northeurope"
"southcentralus"
"southeastasia"
"southindia"
"uksouth"
"ukwest"
"westcentralus"
"westeurope"
"westindia"
"westus"
"westus2"

Note: If you are not piping the output of the az command to jq, you’ll see additional information for each region.

From the list of regions, pick the one that best suits the location for your resources. With that, we can finally create our resource group:

$ az group create --name my_resource_group --location westus2
{
"id": "/subscriptions/2b97ffe3-a4ad-4f78-8d71-3494bf3c731d/resourceGroups/my_resource_group",
"location": "westus2",
"managedBy": null,
"name": "my_resource_group",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null
}

To verify, run `az group list` and confirm that the output contains the group you just created.

$ az group list
[
{
"id": "/subscriptions/2b97ffe3-a4ad-4f78-8d71-3494bf3c731d/resourceGroups/my_resource_group",
"location": "westus2",
"managedBy": null,
"name": "my_resource_group",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null
}
]

Great! Now that we have a resource group, we can associate infrastructure with it. Let’s start by creating a VM.

Creating a Virtual Machine

To create our first virtual machine, we will need to call the `az vm create` command with the name of our resource group, the name of a VM image, and the name we chose for the VM.

We have the resource group from the previous step. Let’s get an image.

The image provides the basis for the type of VM we will create including the base operating system. It should be noted that not all images are available in all regions. The best way to find an image is to list them and grep for the keyword “urnAlias”.

$ az vm image list | grep urnAlias
You are viewing an offline list of images, use --all to retrieve an up-to-date list
"urnAlias": "Win2016Datacenter",
"urnAlias": "Win2012R2Datacenter",
"urnAlias": "Win2008R2SP1",
"urnAlias": "Win2012Datacenter",
"urnAlias": "UbuntuLTS",
"urnAlias": "CentOS",
"urnAlias": "openSUSE-Leap",
"urnAlias": "RHEL",
"urnAlias": "SLES",
"urnAlias": "Debian",
"urnAlias": "CoreOS",

Use `az vm image list –all` to get an extended list of available images. Running with `–all` may take several minutes to complete as the CLI tool will be making an extended query to get all available images. Also note that the extended listing does not include the “urnAlias” field. If you’re trying to parse out the image names, simply grep for “urn”.

For our exploration, using the default listing should suffice. Make a note of the “CentOS” urnAlias.

Now we can choose a name for our VM. There are a few things to note about VM naming conventions in Azure. Essentially, keep names short and alphanumeric. Linux host names cannot exceed 64 characters in length or contain the following characters: ` ~ ! @ # $ % ^ & * ( ) = + _ [ ] { } \\ | ; : ‘ \” , < > / ?

Note: Underscores are included in the list of bad characters for VM names!

With all the info we’ve gathered, let’s create a CentOS VM named ‘myserver1’ in the group ‘my_resource_group’. Here’s the command and the output of the successful creation:

$ az vm create --image CentOS --name myserver1 --resource-group my_resource_group
{
"fqdns": "",
"id": "/subscriptions/2b97ffe3-a4ad-4f78-8d71-3494bf3c731d/resourceGroups/my_resource_group/providers/Microsoft.Compute/virtualMachines/myserver1",
"location": "westus2",
"macAddress": "00-0D-3A-F7-F2-75",
"powerState": "VM running",
"privateIpAddress": "10.0.0.5",
"publicIpAddress": "52.xyz.xyz.xyz", # IP Address masked
"resourceGroup": "my_resource_group"
}

If the image is not available in your region, you may see an error like the following:

At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details. {
"error": {
"code": "SkuNotAvailable",
"message": "The requested tier for resource '/subscriptions/2b97ffe3-a4ad-4f78-8d71-3494bf3c731d/resourceGroups/development_group/providers/Microsoft.Compute/virtualMachines/myserver1' is currently not available in location 'westus' for subscription '2b97ffe3-a4ad-4f78-8d71-3494bf3c731d'. Please try another tier or deploy to a different location."
}
} Correlation ID: eb474e89-12e9-4c59-a749-4cb6148c6462

If that’s the case, check the output of `az vm list images` again and find a suitable image.

One of the cool things about creating Azure VMs from the command line is that SSH access is set up with your current SSH keys. If you are on a system with keys in ~/.ssh/id_rsa, VMs will be created with access using that key. You can also use the `–generate-keys` switch to have the `az vm create` command generate keys for you.

Another cool thing with creating Azure VMs from the command line is account creation. An account is created on the VM that matches the account running the `az vm create` command. That account will have admin privileges and will be able to run elevated commands with sudo as needed. So there’s no need to specify a user name (or a key if you have keys set up in ~/.ssh/id_rsa). Simply SSH to the IP address returned by the `az` command:

$ ssh 52.xyz.xyz.xyz
Warning: Permanently added '52.xyz.xyz.xyz' (ECDSA) to the list of known hosts.
[my.user.name@myserver1 ~]$ sudo yum update -y

Back on your local workstation, you can get more details on your VM with the `az vm show` command along with the VM and resource group name:

$ az vm show --name myserver1 --resource-group my_resource_group

You can find more information on the options for creating a vm by running `az vm create –help`. You’ll find details on sizing your VM, adding data disks, setting the admin password, and more. To get more information on managing VMs, run `az vm –help` for all commands related to working with VMs from the CLI.

For now, let’s clean up the resources we’ve created so far.

Removing Resources by Deleting a Resource Group

Before we clean up, let’s take a look at what’s actually inside our resource group. You can get complete details on your resource group with the `az group export` command:

$ az group export --name my_resource_group

The output from this command will include specifics on the group including networks, virtual machines, associated storage, and more.

As previously mentioned, all the resources inside a resource group can be managed through the group. This makes cleaning up a project extremely easy! We can remove all the resources associated with a group along with the group itself by using the `az group delete` command.

$ az group delete --name my_resource_group
Are you sure you want to perform this operation? (y/n): y

Thankfully, we’re prompted to confirm the delete operation. After indicating yes, the deletion proceeds and removes all associated resources and the group as well.

Once the command completes, we can confirm the group is gone by running `az group list`:

$ az group list
[]

As expected, the group list is empty.

Wrapping Up

This has been a quick look into getting started with the Azure command line interface. You learned how to create a resource group and the benefits of using one. You also learned how to find an alias for a machine image and use that image to add a VM to a resource group. Finally, you learned how to remove a resource group and all of the resources within it by deleting the resource group.

While this is a great start, there’s so much more to learn about using the Azure command line interface! Using the Azure CLI, you can build the infrastructure to host complete applications with just a few commands. Be on the lookout for more tutorials on using this very capable method for working with servers, networks, and more on Azure.

If you have feedback or questions on this tutorial, please leave them in the comments below. I also encourage you to share your successes as you explore and master using the Azure CLI.

Looking for team training?