Back to Blog

Provision Azure VMs via CLI: images, SSH, disks, and networking

Complete tutorial about az vm create in Azure CLI. Learn image selection, size, SSH keys/password, OS/data disks, NIC and public IP.

Provision Azure VMs via CLI: images, SSH, disks, and networking

Provision Azure VMs via CLI: Images, SSH, Disks, and Networking

Introduction

Provisioning Azure Virtual Machines (VMs) via the Azure CLI is a fundamental task for developers and IT professionals who want to harness the power of cloud computing. The primary command for creating VMs is az vm create, which allows users to specify various parameters such as VM images, sizes, SSH keys, and networking configurations. This command matters as it enables users to automate and streamline the deployment of virtual machines, ensuring consistent environments for development, testing, and production workloads.

Azure VMs can run various operating systems, from Linux distributions to Windows Server, making them versatile for different use cases, including web hosting, application development, and more complex solutions like distributed applications and microservices. In this tutorial, we will explore how to create Azure VMs with a focus on image selection, SSH access, disk configurations, and networking setups.

Prerequisites

Before diving into creating Azure VMs, ensure you have the following:

  • Azure CLI: Install the latest version of Azure CLI on your machine. You can check your version using az --version.
  • Azure Subscription: You need an active Azure subscription to create resources.
  • Permissions: Ensure you have sufficient permissions to create resources in the Azure subscription. Typically, the Contributor role is required.
  • Authentication: Log in to your Azure account via the CLI using az login.

Fundamental Concepts

Understanding a few key terms will help you navigate the Azure VM creation process effectively:

  • Resource Group: A container that holds related resources for an Azure solution. A VM must be created within a resource group.
  • VM Image: The operating system and software configurations that the VM will use. Azure provides various pre-configured images in the Azure Marketplace.
  • Size: The specifications of the VM, including CPU and memory. Different sizes cater to various workloads.
  • SSH Keys: A secure method for accessing your VM without using passwords.
  • Networking: Configurations that define how your VM communicates with other resources and the internet, including Virtual Networks (VNet), Network Security Groups (NSG), and public IP addresses.

Command Syntax

The syntax for the az vm create command is as follows:

az vm create \
  --resource-group <resource-group-name> \
  --name <vm-name> \
  --image <image-name> \
  --size <vm-size> \
  --admin-username <username> \
  --ssh-key-value <ssh-public-key-file> \
  --public-ip-sku <public-ip-sku> \
  --data-disk-sizes-gb <disk-size> \
  --vnet-name <vnet-name> \
  --subnet <subnet-name>

Parameters Table

Parameter Description
--resource-group The name of the resource group.
--name The name of the VM.
--image The image to use (e.g., UbuntuLTS, win2019datacenter).
--size The size of the VM (e.g., Standard_DS1_v2).
--admin-username The username for the admin account.
--ssh-key-value The path to your public SSH key file.
--public-ip-sku The SKU for the public IP (e.g., Basic or Standard).
--data-disk-sizes-gb Size of the data disk in GB.
--vnet-name The name of the virtual network to associate with the VM.
--subnet The name of the subnet to use for the VM.

Practical Examples

Example 1: Create a Basic Linux VM

az vm create \
  --resource-group myResourceGroup \
  --name myLinuxVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

This command creates a basic Ubuntu VM with SSH keys automatically generated.

Example 2: Create a Windows VM with Password Authentication

az vm create \
  --resource-group myResourceGroup \
  --name myWindowsVM \
  --image win2019datacenter \
  --admin-username azureuser \
  --admin-password 'P@ssw0rd1234!'

This creates a Windows Server VM with password-based authentication.

Example 3: Create a VM with Specific Size

az vm create \
  --resource-group myResourceGroup \
  --name mySizedVM \
  --image UbuntuLTS \
  --size Standard_DS2_v2 \
  --admin-username azureuser \
  --generate-ssh-keys

Here, the VM is created with a specific size for better performance.

Example 4: Create a VM with Data Disks

az vm create \
  --resource-group myResourceGroup \
  --name myDiskVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys \
  --data-disk-sizes-gb 128

This command provisions a VM with a 128 GB data disk.

Example 5: Create a VM in a Specific VNet and Subnet

az vm create \
  --resource-group myResourceGroup \
  --name myVNetVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys \
  --vnet-name myVNet \
  --subnet mySubnet

This example connects the VM to a specific virtual network and subnet.

Example 6: Create a VM with a Static Public IP

az vm create \
  --resource-group myResourceGroup \
  --name myStaticIPVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys \
  --public-ip-sku Standard

This command assigns a static public IP address to the VM.

Example 7: Create Multiple VMs with Loop

for i in {1..3}; do
  az vm create \
    --resource-group myResourceGroup \
    --name myVM$i \
    --image UbuntuLTS \
    --admin-username azureuser \
    --generate-ssh-keys
done

This script creates three VMs in a loop.

Example 8: Create a VM with Network Security Group

az vm create \
  --resource-group myResourceGroup \
  --name mySecuredVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys \
  --nsg myNetworkSecurityGroup

This command associates the VM with a specified Network Security Group.

Real-World Use Cases

Scenario 1: Development Environment

A software company needs to set up a development environment for its team. Using the Azure CLI, they can quickly provision multiple VMs with the required specifications and configurations. Each developer can have their own VM for testing and development purposes without the overhead of physical machines.

Scenario 2: Web Hosting

A startup wants to host a web application on Azure. By using the az vm create command, they can deploy a VM with a public IP, install a web server, and configure the necessary security groups to manage traffic. This can be automated as part of the deployment pipeline.

Scenario 3: Testing and Staging

For continuous integration and delivery (CI/CD), an organization can create VMs dynamically for testing and staging environments. With Azure CLI scripts, they can spin up new VMs for each build and tear them down when testing is complete, optimizing resource usage and costs.

Best Practices

  • Use Managed Disks: They offer better reliability and performance.
  • Generate SSH Keys: Always use SSH keys for secure access rather than passwords.
  • Select Appropriate Sizes: Choose VM sizes that match your workload requirements to optimize costs.
  • Automate Deployments: Use scripts for creating VMs to ensure consistency and reduce manual errors.
  • Implement Network Security Groups: Control traffic flows to your VMs to enhance security.

Common Errors

  1. Insufficient Permissions:

    • Error Message: Operation failed with status: 'Forbidden'
    • Solution: Ensure you have the necessary roles assigned in the Azure portal.
  2. Invalid Image Name:

    • Error Message: Invalid image reference
    • Solution: Check the image name against available images in the Azure Marketplace.
  3. Quota Exceeded:

    • Error Message: QuotaExceeded
    • Solution: Request a quota increase through the Azure portal.
  4. Network Configuration Issues:

    • Error Message: Network security group does not exist
    • Solution: Ensure that the specified Network Security Group exists before deploying the VM.

Related Commands

Command Description
az vm list List all VMs in a resource group.
az vm start Start a stopped VM.
az vm stop Stop a running VM.
az vm delete Delete a specified VM.
az vm show Show details of a specified VM.

Automation Script

Here’s a complete Bash script for automating the VM creation process:

#!/bin/bash

# Variables
RESOURCE_GROUP="myResourceGroup"
LOCATION="eastus"
VM_NAME="myAutomatedVM"
IMAGE="UbuntuLTS"
ADMIN_USERNAME="azureuser"

# Create Resource Group
az group create --name $RESOURCE_GROUP --location $LOCATION

# Create VM
az vm create \
  --resource-group $RESOURCE_GROUP \
  --name $VM_NAME \
  --image $IMAGE \
  --admin-username $ADMIN_USERNAME \
  --generate-ssh-keys \
  --public-ip-sku Standard

# Output Public IP
az vm show --show-details --resource-group $RESOURCE_GROUP --name $VM_NAME --query publicIps -o tsv

Conclusion

In this tutorial, we explored how to provision Azure VMs using the Azure CLI, focusing on key aspects such as image selection, SSH configuration, disk setups, and networking. The az vm create command is a powerful tool that simplifies the VM deployment process, allowing for automation and consistency across environments. As you continue to work with Azure, consider leveraging these commands within scripts and CI/CD pipelines to enhance your workflows.

References