Azure Header Background Image

Microsoft Azure – Setting Up the CLI and Launching a VM

Microsoft continues to deliver on their promise to embrace open source. With dozens of officially supported Linux distributions and open source services available on their Azure platform, anyone can deploy workloads to their cloud platform regardless of the operating system of choice. Today, we are going to show you how to set up the Microsoft Azure CLI and use it to launch a virtual machine in the cloud.

Installation of the CLI

Note that this article does assume that you have access to the Microsoft Azure Portal and you have signed up for either the free tier (consisting of 200 compute hours) or another account (corporate or Pay As You Go). This is important as we have to link the CLI to the associated account created during your registration.

The installation is relatively simple and is largely the same regardless of your Linux distribution. Be sure you have installed the npm package for your distribution (either “sudo yum install npm” or “sudo apt-get install npm” for most distributions). Once you have, you are going to use it to install the command line interface for Azure:

sudo npm install -g azure-cli

Once complete (and it will take a few minutes to install; like most Node.JS installations, there are a ton of dependencies that will install along with the actual packages we are after). We can test that the install was successful by executing:

azure --version

Which shows us the version installed. Perfect! Let’s move on to configuration.

Configure the CLI

A couple of quick items before we can use the CLI to launch a VM in our cloud infrastructure. First, we want to set up command line autocomplete. Just like pressing tab in Bash will autocomplete a directory name for us, we want to be able to type part of an Azure CLI command and see the available options that meet it (so typing “azure log[TAB]” would show us that “login” and “logout” are available options). We do that by doing the following:

azure --completion >> ~/azure.completion.sh
echo 'source ~/azure.completion.sh' >> ~/.bash_profile

Note that after adding this to your environment, you need to log out and back in (or just execute the source command above for the current session). Now we have to associate the CLI interface with the account you log in to the portal with. Again, at a command prompt, issue the following command:

azure login

Where you should see something like the following:

info: Executing command login
info: To sign in, use a web browser to open the page http://aka.ms/devicelogin. Enter the code XXXXXXXXX to authenticate.

You then need to open your browser to the indicated URL and enter the code you were provided. If you are not already logged in to the Microsoft Azure Portal with your account, you will be prompted to do so. The browser will accept your code, associate the account with the CLI tools, complete the login process and indicate you can close the browser window. Once you return to the CLI prompt, you will see “login command ok” if everything was done correctly. Finally, we need to indicate that this CLI is going to use the Microsoft Azure Resource Manager model for managing our resources (rather than the classic model). This is done with a simple:

azure config mode arm

That’s it, let’s launch our first virtual machine!

Launching a Simple VM

Most of the work is done now. Launching a VM in our environment is a simple command (note that there are a TON of options we could use here, but we are just exploring how we can launch a simple virtual machine using nothing but a single command now; we may do more advanced commands in later articles). Type the following to launch a VM and associate the current user’s SSH public key for login:

azure vm quick-create -M ~/.ssh/id_rsa.pub -Q RHEL

This asks you a few questions that look like the following block:

info: Executing command vm quick-create
Resource group name: Default
Virtual machine name: myrheltest
Location name: East US
Operating system Type [Windows, Linux]: Linux
User name: user
+ Listing virtual machine sizes available in the location "eastus" 
+ Looking up the VM "myrheltest" 
info: Verifying the public key SSH file: /home/user/.ssh/id_rsa.pub
info: Using the VM Size "Standard_DS1"
[more output here...]

You are asked for a resource group, a name for your virtual machine, the zone to run it in and the operating system type. If you answer as I did above, you then get a bunch of informational output as your instance is created. The whole process should take three to five minutes to complete and you will see a message once it is done with all the information you need to use it (the output below is lengthy):

data: ProvisioningState :Succeeded
data: Name :myrheltest
data: Location :eastus
data: Type :Microsoft.Compute/virtualMachines
data: 
data: Hardware Profile:
data: Size :Standard_DS1
data: 
data: Storage Profile:
data: Image reference:
data: Publisher :RedHat
data: Offer :RHEL
data: Sku :7.2
data: Version :latest
data: 
data: OS Disk:
data: OSType :Linux
data: Name :cli00002599248831d3-os-1476457820250
data: Caching :ReadWrite
data: CreateOption :FromImage
data: Vhd:
data: Uri :https://stoc4e8qlwgozg8.blob.core.windows.net/vhds/cli00002599248831d3-os-1476457820250.vhd
data: 
data: OS Profile:
data: Computer Name :myrheltest
data: User Name :user
data: Linux Configuration:
data: Disable Password Auth :true
data: 
data: Network Profile:
data: Network Interfaces:
data: Network Interface #1:
data: Primary :true
data: MAC Address :00-0D-3A-14-AA-4D
data: Provisioning State :Succeeded
data: Name :myrhe-eastu-mhflf3crqgd6-nic
data: Location :eastus
data: Public IP address :13.92.39.210
data: FQDN :myrhe-eastu-mhflf3crqgd6-pip.eastus.cloudapp.azure.com
data: 
data: Diagnostics Profile:
data: BootDiagnostics Enabled :true
data: BootDiagnostics StorageUri :https://clisto4148978356myrhelte.blob.core.windows.net/
data: 
data: Diagnostics Instance View:
info: vm quick-create command OK

 

You can now connect with either the Public IP or the FQDN as listed above. This whole process, start to finish, should take you less than fifteen minutes (including launching the VM). Be sure to check the Microsoft Azure documentation for more granular control over how your VM is configured, but right now, we have a full Red Hat VM available for use.

Terrence T. Cox

A veteran of twenty years in Information Technology in a variety of roles. He has worked in development, security and infrastructure well before they merged into what we now call DevOps. He provides training in Linux, VMWare, DevOps (Ansible, Jenkins, etc) as well as containers and AWS topics.

One thought on “Microsoft Azure – Setting Up the CLI and Launching a VM

Leave a Reply

Your email address will not be published. Required fields are marked *