First steps with Azure CLI

This guide uses Azure’s cross platform CLI to create and destroy a virtual machine.

First things first, lets define some basic properties about the machine such as the adminstrator credentials and its hostname, which will be given the suffix of cloudapp.net:

HOST=cccu-dev-adfs
USERNAME=djb
PASSWORD=Password1!

To make changes to your Azure account you’ll need to authenticate yourself using an X509 certificate. This can be generated by running the following command, which will launch your web browser:

azure account download

Once the certificate is downloaded you need to register it with the command line tool:

azure account import "~/Downloads/credentials.publishsettings"

Azure uses images (or templates) of operating systems with varying applications preinstalled. To create a new VM you need to pick an image, but these are identified by mixtures of random hexidecimal and textual description. To find the right identifier you can use another API call to search images by their human readable label. The following command downloads the list as json and uses the jq command line tool to find the right json object and return its identifier:

SELECTOR='.[] | select(.label | contains("Windows Server 2012 R2 Datacenter")) | .name'
IMAGE=`azure vm image list --json | jq -r -c $SELECTOR | tail -n 1`

You can now create and start your new server:

azure vm create $HOST $IMAGE $USERNAME $PASSWORD \
    --rdp \
    --location "West Europe" \
    --vm-size Medium
azure vm start $HOST

By default there won’t be any endpoints created on the Azure load balancer, save for 3389 used by RDP. To access a web server running on your new server use vm endpoint:

azure vm endpoint create $HOST 80

When you’re finished with the VM you’ll want to delete it to save a few pennies. Use the vm delete command and optionally also remove the VM’s disk storage:

azure vm delete $HOST --blob-delete --quiet