Creating and Configuring Virtual Machines in Azure with Azure CLI
Introduction
Creating and managing Virtual Machines (VMs) in Azure is a fundamental skill for anyone working with cloud infrastructure. Azure provides a robust CLI tool, az vm create, that enables users to create and configure VMs quickly and efficiently. Virtual Machines are essential for running applications, hosting services, and providing scalable computing resources in the cloud.
Azure VMs come in various sizes and configurations, allowing users to tailor their environments based on workload requirements. Additionally, Azure offers multiple disk options, networking capabilities, and security features, making it a versatile platform for deploying applications. In this tutorial, we will explore how to use the az vm create command along with practical examples to create and configure Virtual Machines in Azure.
Prerequisites
Before you begin, ensure you have the following:
- Azure CLI installed on your machine. (You can download it here).
- An active Azure subscription. You can create a free account if you don’t have one here.
- Sufficient permissions to create resources in your Azure subscription.
- Authentication to Azure CLI using
az login.
Fundamental Concepts
- Virtual Machine (VM): An emulation of a physical computer that runs an operating system and applications in the cloud.
- VM Size: The configuration of the virtual machine, including CPU, memory, and disk space.
- Managed Disks: Azure storage disks that are managed by Azure, simplifying the management of disk storage.
- Networking: The configuration of network interfaces, security groups, and public IP addresses associated with the VM.
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> --admin-username <username> --admin-password <password> [--size <size>] [--location <location>] [--public-ip-address <ip-name>] [--vnet-name <vnet-name>] [--subnet <subnet-name>]
Parameters Table
| Parameter | Description |
|---|---|
--resource-group -g |
The name of the resource group. |
--name -n |
The name of the virtual machine. |
--image -i |
The image or operating system to use (e.g., UbuntuLTS, Win2019Datacenter). |
--admin-username |
The username for the VM administrator account. |
--admin-password |
The password for the VM administrator account. |
--size |
The size of the VM (e.g., Standard_DS1_v2). |
--location |
The Azure region where the VM will be created. |
--public-ip-address |
The name of the public IP address to create. |
--vnet-name |
The name of the virtual network to attach the VM to. |
--subnet |
The name of the subnet within the virtual network. |
Practical Examples
Example 1: Create a Basic Virtual Machine
Create a simple Ubuntu VM in a specified resource group:
az vm create --resource-group myResourceGroup --name myUbuntuVM --image UbuntuLTS --admin-username azureuser --admin-password MyP@ssw0rd123
This command creates an Ubuntu VM named myUbuntuVM in the myResourceGroup resource group with specified admin credentials.
Example 2: Create a Windows Virtual Machine
Deploy a Windows Server VM:
az vm create --resource-group myResourceGroup --name myWindowsVM --image Win2019Datacenter --admin-username azureuser --admin-password MyP@ssw0rd123 --size Standard_DS2_v2
This sets up a Windows Server 2019 Datacenter VM with a specified size for better performance.
Example 3: Create a VM with a Public IP Address
Create a VM with a public IP address for external access:
az vm create --resource-group myResourceGroup --name myPublicVM --image UbuntuLTS --admin-username azureuser --admin-password MyP@ssw0rd123 --public-ip-address myPublicIP
This command creates a VM and allocates a public IP address, allowing external access.
Example 4: Create a VM in a Specific Location
Specify a location when creating a VM:
az vm create --resource-group myResourceGroup --name myLocationVM --image UbuntuLTS --admin-username azureuser --admin-password MyP@ssw0rd123 --location eastus
This creates an Ubuntu VM in the East US region.
Example 5: Create a VM with a Virtual Network and Subnet
Create a VM associated with a specific virtual network and subnet:
az network vnet create --name myVNet --resource-group myResourceGroup --subnet-name mySubnet
az vm create --resource-group myResourceGroup --name myVNetVM --image UbuntuLTS --admin-username azureuser --admin-password MyP@ssw0rd123 --vnet-name myVNet --subnet mySubnet
This first creates a virtual network and a subnet, then creates a VM within that virtual network.
Example 6: Create a VM with Managed Disks
Create a VM using managed disks for simplified storage management:
az vm create --resource-group myResourceGroup --name myManagedDiskVM --image UbuntuLTS --admin-username azureuser --admin-password MyP@ssw0rd123 --size Standard_DS1_v2 --storage-sku Standard_LRS
This command specifies the storage type as Standard_LRS, which is a commonly used managed disk option.
Example 7: Create a VM with Custom Data
Create a VM and pass custom data (cloud-init script):
az vm create --resource-group myResourceGroup --name myCustomDataVM --image UbuntuLTS --admin-username azureuser --admin-password MyP@ssw0rd123 --custom-data cloud-init.txt
This command allows you to pass a cloud-init script to configure the VM upon creation.
Example 8: Create a VM with SSH Key Authentication
Create a VM using SSH key authentication instead of a password:
az vm create --resource-group myResourceGroup --name mySSHVM --image UbuntuLTS --admin-username azureuser --ssh-key-value ~/.ssh/id_rsa.pub
This command uses an SSH public key for secure access without needing to specify a password.
Real-World Use Cases
Scenario 1: Development and Testing Environments
Developers can quickly create VMs to set up isolated environments for testing applications. By using different VM sizes and configurations, they can simulate production-like environments, helping to catch issues before deployment.
Scenario 2: Hosting Web Applications
Organizations can deploy web applications on Azure VMs. By configuring load balancers and scaling options, they can ensure high availability and performance, allowing for responsive user experiences.
Scenario 3: Running Legacy Applications
Companies can migrate legacy applications to Azure VMs, providing the necessary environment for applications that may not be compatible with modern cloud-native architectures. This approach allows for gradual modernization of IT infrastructure.
Best Practices
- Use Managed Disks: Managed disks simplify storage management and improve availability.
- Choose the Right VM Size: Select VM sizes based on workload requirements to optimize cost and performance.
- Use Availability Sets: Deploy VMs within availability sets to enhance reliability and ensure high availability.
- Implement Network Security Groups (NSGs): Use NSGs to control inbound and outbound traffic to your VMs, enhancing security.
- Automate Deployment: Utilize Azure Resource Manager (ARM) templates or Azure CLI scripts to automate VM creation and configuration.
Common Errors
Error: "Invalid image reference."
- Cause: The specified image name is incorrect or not available.
- Solution: Verify the image name and use
az vm image listto find valid options.
Error: "Resource group not found."
- Cause: The specified resource group does not exist.
- Solution: Create the resource group using
az group create.
Error: "Public IP address already exists."
- Cause: The specified public IP address name is already in use.
- Solution: Choose a different name for the public IP address.
Error: "Insufficient quota."
- Cause: The subscription has reached the maximum allowed quota for the selected VM size.
- Solution: Request an increase in quota through the Azure portal or select a different VM size.
Related Commands
| Command | Description |
|---|---|
az vm list |
List all VMs in a specified resource group. |
az vm show |
Show details of a specific VM. |
az vm delete |
Delete a specified VM from Azure. |
az vm start |
Start a stopped VM. |
az vm stop |
Stop a running VM. |
az vm resize |
Change the size of a running VM. |
Automation Script
Here’s a simple bash script to automate the creation of a Virtual Machine with a specific configuration:
#!/bin/bash
# Variables
RESOURCE_GROUP="myResourceGroup"
VM_NAME="myAutomatedVM"
LOCATION="eastus"
IMAGE="UbuntuLTS"
ADMIN_USER="azureuser"
ADMIN_PASSWORD="MyP@ssw0rd123"
VM_SIZE="Standard_DS1_v2"
# Create Resource Group
az group create --name $RESOURCE_GROUP --location $LOCATION
# Create Virtual Machine
az vm create --resource-group $RESOURCE_GROUP --name $VM_NAME --image $IMAGE --admin-username $ADMIN_USER --admin-password $ADMIN_PASSWORD --size $VM_SIZE
echo "Virtual Machine $VM_NAME created successfully in $RESOURCE_GROUP!"
Conclusion
Creating and managing Virtual Machines in Azure using the Azure CLI is a powerful way to leverage cloud computing resources. The az vm create command provides a flexible and efficient method for deploying VMs tailored to specific workload requirements. By mastering this command and understanding the associated concepts, you can effectively manage your cloud infrastructure.
Next Steps
- Explore configuring additional settings such as networking and security for your VMs.
- Investigate Azure Automation to streamline VM management tasks.
- Look into Azure Monitor to track the performance and health of your VMs.
