2019-03-30: How to set up a Azure VM with ARM Template and Azure CLI

Goal

  • Use Azure Resource Manager (ARM) Template and Azure CLI (az) scripts to set up Azure VM
  • Use Azure CLI to set up networking instead of using Powershell or ARM templates

Workspace Source code

Requirements

Development Environment

For the best Dev experience:

  • Use Visual Studio Code to edit the code
    • In the ".vscode" folder, there is a "settings.json" file.
    • If you want to run Python scripts on Windows Host: And If you have more than one Python installation, create a "PYTHON_INSTALL_LOC" environment variable on the Windows Host and point it to your selected Python installation.
  • Run "docker-compose.up.build.bat" to build and start the "Workplace" Container.
  • Work from inside a Container.
    • This Container will run identical everywhere and you avoid bloating your Windows Host with config and files. Running stuff in Container also makes it easier to handle different version of applications.
    • The source code is mounted into the Container. This makes it possible to edit locally in Visual Studio Code and then just run the scripts in the Container without the need to build and restart the Container.

Starting up

  • Run "docker-compose.up.build.bat" to build and start the "Workplace" Container.

This start a Container and attach a terminal to the running container.

cd azure-template.001.win2019
dos2unix *
bash deploy.sh

This will try to login into Azure.

az login


This will login you in and show all the Azure accounts available:


Select one and put into the "subscriptionId" parameter shown below.

  • Open "deploy.sh" and edit the "subscriptionId" variable:

declare subscriptionId="<>"
declare vmName="myazurevm001"
declare resourceGroupName="$vmName-rg"
declare deploymentName="setup_azure_vm_arm_cli_$vmName"
# Use "account list-locations" to list all possible locations
declare resourceGroupLocation="westeurope"


Run the script again:

cd azure-template.001.win2019
dos2unix *
bash deploy.sh


This script will create:

  • a resource group with a Windows Server 2019 VM
  • add a DNS name to VM using the Azure CLI.

The DNS name is added by this code:

echo "Adding DNS name"
az network public-ip update -g "$resourceGroupName" -n "$vmName-ip" --set "dnsSettings.domainNameLabel=$vmName-geircode"  --debug

Using a combination of Azure Resource Manager Templates and Azure CLI is much more developer friendly and faster to learn than to only use Powershell and ARM Templates.

Azure CLI has the power to do everything!

Rejoice.



Delete everything after use by running the script: "resourcegroups_delete.sh"

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

az group delete --name myazurevm001-rg --no-wait