12  Week 10/11 (4/11-Dec-2025)


12.1 Use Azure Cli to manage Azure Resources

12.1.1 Download Azure Cli

  • Access the following site to download the tool for your operating system and install it.

12.1.2 Run the Azure client

  • Check the client version
az version
  • If you have already, just upgrade to the latest version.
az upgrade
  • Make sure you afre able to login to Azure.
  • Enter your username and password in the browser and return to the command line.
az login
  • Once you are authenticated, you can execute all other commands.
  • Try the following to list existing resources in your Account.
az resource list
  • Try the following to list existing VMs in your Account.
az vm list

12.1.3 Create the first template (empty template) file.

  • Open any code editor.
  • Create a new file named azuredeploy.json.
  • Add the following contents to it.
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": []
}

12.1.4 Deploy the template

  • Make sure you are logged in to Azure.
az login
  • If you have multiple Azure subscriptions, choose the subscription you want to use by running the following.
az account set --subscription SubscriptionName
  • When you deploy a template, you can specify a resource group to contain the resources. Before running the deployment command, create the resource group in a specific location (use Western Europe).
az group create --name myResourceGroup --location 'westeurope'
  • Deploy the template by using the resource group you just created. Give a name to the deployment so you can easily identify it in the deployment history.
  • For convenience, also create a variable that stores the path to the template file.
  • This variable makes it easier for you to run the deployment commands because you don’t have to retype the path every time you deploy.
  • Replace {provide-the-path-to-the-template-file} and the curly braces {} with the full path of your template fil created earlier.
templateFile="{provide-the-path-to-the-template-file}"
az deployment group create --name my-first-blanktemplate --resource-group myResourceGroup --template-file $templateFile
  • The deployment command returns results. Look for ProvisioningState to see whether the deployment succeeded.

  • Documentation for ARM templates can be found here.

12.1.5 Azure Quickstart Templates

  • Azure ARM Quickstart templates can be found here.

12.2 Exercises

12.2.1 Deploy a VM

  • Based on the Quickstart templates, deploy your first Linux VM to Azure.
  • Do so, by adding contents only in the "resources": [] section of your template file.

12.2.1.1 Solution

  • The following command requests several pieces of input from the user. These include:
    • Name of the Resource Group (resourceGroupName)
    • Location of the Azure datacenter that hosts the VM (location)
    • A name for resources related to the VM (projectName)
    • Username for the administrator user (username)
    • A public SSH key for accessing the VM’s terminal (key)
# You need to have generated a pair of public/private keys before in your machine.
# Execute the following in Linux or MacOS. For Windows, OpenSSH Client must be installed. 
ssh-keygen -t rsa -b 2048
read -r pubkey < ~/.ssh/id_rsa.pub

# This example uses variables as an input for the commands
echo "Enter the Resource Group name:" 
read resourceGroupName
echo "Enter the location (i.e. westeurope):" 
read location
echo "Enter the project name (used for generating resource names):" 
read projectName
echo "Enter the administrator username:" 
read username

# This prints all the variables

echo $pubkey
echo ""
echo $resourceGroupName
echo $location
echo $projectName
echo $username

# Get the content from this file and put into `azuredeploy.json` file
# https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/quickstarts/microsoft.compute/vm-sshkey/azuredeploy.json
# Change SKU to "Standard" and publicIPAllocationMethod to "Static" 

# This example uses variables as an input for the commands but you can hard-code the values instead.

az group create --name $resourceGroupName --location "$location" 

az deployment group create --resource-group $resourceGroupName --template-file ./azuredeploy.json --parameters projectName=$projectName adminUsername=$username adminPublicKey="$pubkey"

az vm show --resource-group $resourceGroupName --name "$projectName-vm" --show-details --query publicIps --output tsv
  • Connect to the virtual machine.
ssh <adminUsername>@<ipAddress>