Skip to main content

🎯 Advanced Guide to Creating a Virtual Machine in Azure

Creating a Virtual Machine (VM) in Microsoft Azure requires careful planning to ensure security, scalability, and performance optimization. This guide covers advanced options such as custom images, networking, storage, and automation.


🚀 Step 1: Plan Your Virtual Machine

Before creating an Azure VM, define:

  • 📦 Resource Group: Logical grouping for Azure resources.
  • 🌎 Region: Choose the region closest to your users or aligned with compliance needs.
  • 💻 Size: Select VM SKU based on CPU, RAM, and workload requirements.
  • 🖥️ OS Image: Choose a marketplace image or a custom image.
  • 🔑 Authentication: SSH Key for Linux or Password/RDP for Windows.

🖥️ Step 2: Create a Virtual Machine Using Azure CLI

📌 2.1. Create a Resource Group

az group create --name MyResourceGroup --location eastus

🌐 2.2. Create a Virtual Network and Subnet

az network vnet create \
--resource-group MyResourceGroup \
--name MyVNet \
--address-prefix 10.0.0.0/16 \
--subnet-name MySubnet \
--subnet-prefix 10.0.0.0/24

🔒 2.3. Create a Network Security Group (NSG)

az network nsg create \
--resource-group MyResourceGroup \
--name MyNSG

🌍 2.4. Create a Public IP Address

az network public-ip create \
--resource-group MyResourceGroup \
--name MyPublicIP \
--sku Standard

🖧 2.5. Create a Network Interface (NIC)

az network nic create \
--resource-group MyResourceGroup \
--name MyNIC \
--vnet-name MyVNet \
--subnet MySubnet \
--network-security-group MyNSG \
--public-ip-address MyPublicIP

2.6. Create the Virtual Machine

az vm create \
--resource-group MyResourceGroup \
--name MyVM \
--image Ubuntu2204 \
--size Standard_D2s_v3 \
--admin-username azureuser \
--ssh-key-values ~/.ssh/id_rsa.pub \
--nics MyNIC

💡 Replace Ubuntu2204 with your preferred OS image. For Windows, use --image Win2022Datacenter.


💾 Step 3: Attach and Configure Additional Storage

📀 3.1. Create a Managed Disk

az disk create \
--resource-group MyResourceGroup \
--name MyDataDisk \
--size-gb 50 \
--sku Premium_LRS

🔗 3.2. Attach the Disk to the VM

az vm disk attach \
--resource-group MyResourceGroup \
--vm-name MyVM \
--name MyDataDisk

📂 3.3. Format and Mount the Disk (Linux VM)

lsblk  # Identify the new disk (e.g., /dev/sdc)
sudo mkfs.ext4 /dev/sdc
sudo mkdir /mnt/datadisk
sudo mount /dev/sdc /mnt/datadisk
echo "/dev/sdc /mnt/datadisk ext4 defaults,nofail 0 2" | sudo tee -a /etc/fstab

⚖️ Step 4: Configure Auto-Scaling with Virtual Machine Scale Sets

az vmss create \
--resource-group MyResourceGroup \
--name MyScaleSet \
--image Ubuntu2204 \
--instance-count 2 \
--upgrade-policy-mode automatic \
--admin-username azureuser \
--ssh-key-values ~/.ssh/id_rsa.pub \
--vnet-name MyVNet \
--subnet MySubnet

🔐 Step 5: Enable Azure Bastion for Secure SSH/RDP Access

az network bastion create \
--resource-group MyResourceGroup \
--name MyBastion \
--vnet-name MyVNet \
--location eastus \
--public-ip-address MyBastionIP

📊 Step 6: Configure Monitoring and Alerts

📡 6.1. Enable Azure Monitor

az monitor metrics alert create \
--name CPUHighAlert \
--resource-group MyResourceGroup \
--scopes /subscriptions/{subscription-id}/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyVM \
--condition "avg Percentage CPU > 70" \
--window-size 5m \
--evaluation-frequency 1m \
--action-groups MyActionGroup

🎭 Step 7: Automate VM Creation with Terraform

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "rg" {
name = "MyResourceGroup"
location = "East US"
}

resource "azurerm_virtual_machine" "vm" {
name = "MyVM"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
vm_size = "Standard_D2s_v3"
network_interface_ids = [azurerm_network_interface.nic.id]

storage_os_disk {
name = "MyOSDisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
}

os_profile {
computer_name = "MyVM"
admin_username = "azureuser"
admin_password = "SecurePassword123!"
}
}

Run Terraform:

terraform init
terraform apply -auto-approve

🎯 Conclusion

This advanced guide provides a scalable, secure, and automated approach to VM creation in Azure. You can further enhance the setup by integrating Ansible, Kubernetes (AKS), or DevOps pipelines.

Would you like help with customizing this for your environment, such as integrating it into Jenkins or Azure DevOps? 🚀