Skip to main content

Lab 027: Create VM using Azure CLI



Requirements

The Nautilus DevOps team is in the process of migrating some of their workloads to Azure. One of the tasks involves creating a new Virtual Machine (VM) using the Azure CLI. The team does not have access to the Azure portal but can manage Azure resources via the azure-client host (the landing host for this lab).

  1. Create a new Azure Virtual Machine named nautilus-vm using the Azure CLI.

  2. Use the Ubuntu2204 image and set the VM size to Standard_B2s.

  3. Make sure the admin username is set to azureuser and SSH keys are generated for secure access.

  4. Use Standard_LRS storage account, disk size must be 30GB and ensure the VM nautilus-vm is in the running state after creation.


Note

The original requirement statement is preserved. The procedure below uses one deterministic Azure CLI flow for VM creation and validation.

Prerequisites

  • Azure CLI is installed and authenticated on azure-client.
  • A target resource group exists and is discoverable via the kml filter.
  • SSH key generation is permitted in ~/.ssh.

Steps

For the Azure credentials, enter showcreds command. For further details, check out the Project Nautilus documentation.

# Fetch the resource group name that matches 'kml'
RG=$(az group list --query "[?contains(name, 'kml')].name" --output tsv)

# Fetch the location of the resource group
LOCATION=$(az group show --name $RG --query "location" --output tsv)

# Declare other variables
VM_NAME=nautilus-vm
IMAGE=Ubuntu2204
VM_SIZE=Standard_B2s
ADMIN_USERNAME=azureuser
SSH_KEY_PATH=~/.ssh/id_rsa.pub
OS_DISK_SIZE=30
STORAGE_SKU=Standard_LRS

# Log in to Azure CLI (if not already logged in)
#az login

# Generate SSH keys (if not already generated)
if [ ! -f ~/.ssh/id_rsa ]; then
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa
fi

# Create the virtual machine
az vm create \
--resource-group $RG \
--location $LOCATION \
--name $VM_NAME \
--image $IMAGE \
--size $VM_SIZE \
--admin-username $ADMIN_USERNAME \
--ssh-key-value $SSH_KEY_PATH \
--os-disk-size-gb $OS_DISK_SIZE \
--storage-sku $STORAGE_SKU

# Ensure the VM is running
az vm get-instance-view --resource-group $RG --name $VM_NAME --query instanceView.statuses[1] --output table

Verification

  • Confirm VM nautilus-vm exists and is in VM running state.
  • Confirm VM size is Standard_B2s and OS disk size is 30 GB.
az vm show --resource-group "$RG" --name "$VM_NAME" --query "{Name:name,Size:hardwareProfile.vmSize,PowerState:provisioningState,DiskSize:storageProfile.osDisk.diskSizeGb}" --output table
az vm get-instance-view --resource-group "$RG" --name "$VM_NAME" --query "instanceView.statuses[].displayStatus" --output table

Resources