Creating and Managing Resources with Azure CLI

5.1 Creating Virtual Machines (VMs)

Virtual Machines (VMs) are one of the most common resources you'll deploy in Azure.


๐Ÿ› ๏ธ Create a Linux VM

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

--resource-group

Where the VM will be created

--name

Name of the VM

--image

OS image (Ubuntu, Windows, etc.)

--admin-username

Login username

--generate-ssh-keys

Automatically create SSH keys

โœ… This command will create:

  • VM

  • Public IP

  • NIC (Network Interface)

  • OS Disk

  • Virtual Network (if needed)


๐Ÿ–ฅ๏ธ Create a Windows VM

bashCopyEditaz vm create \
  --resource-group myResourceGroup \
  --name myWindowsVM \
  --image Win2019Datacenter \
  --admin-username azureuser \
  --admin-password YourP@ssword123

โš ๏ธ Tip: Passwords must meet Azureโ€™s complexity requirements!


๐Ÿ”Œ 5.2 Connect to the VM

Get the public IP address of the VM:

bashCopyEditaz vm list-ip-addresses --name myLinuxVM --output table

Connect to a Linux VM:

bashCopyEditssh azureuser@<public-ip-address>

Connect to a Windows VM (RDP):

  1. Open Remote Desktop Connection.

  2. Enter the public IP.

  3. Login with the username/password.

โœ… Now you're inside your VM!


๐Ÿ›‘ 5.3 Managing a VM (Start, Stop, Restart, Delete)

Action
Command

Stop a VM

az vm stop --name myVM --resource-group myResourceGroup

Start a VM

az vm start --name myVM --resource-group myResourceGroup

Restart a VM

az vm restart --name myVM --resource-group myResourceGroup

Delete a VM

az vm delete --name myVM --resource-group myResourceGroup

โœ… Important: Stopping a VM does not delete it โ€” it just stops billing for compute time (but storage costs remain).


๐Ÿ“ฆ 5.4 Creating Storage Accounts

Storage Accounts provide scalable, durable cloud storage for:

  • Blobs

  • Files

  • Tables

  • Queues


๐Ÿ› ๏ธ Create a Storage Account

bashCopyEditaz storage account create \
  --name mystorageaccount123 \
  --resource-group myResourceGroup \
  --location eastus \
  --sku Standard_LRS
Parameter
Purpose

--name

Globally unique name

--resource-group

Where the Storage Account will be created

--location

Azure region

--sku

Storage redundancy options (LRS, GRS)

โœ… Name must be globally unique (only lowercase letters and numbers).


List Storage Accounts

bashCopyEditaz storage account list --output table

Delete a Storage Account

bashCopyEditaz storage account delete --name mystorageaccount123 --resource-group myResourceGroup

๐ŸŒ 5.5 Creating Basic Networking Components


๐Ÿ› ๏ธ Create a Virtual Network (VNet)

bashCopyEditaz network vnet create \
  --resource-group myResourceGroup \
  --name myVNet \
  --address-prefix 10.0.0.0/16 \
  --subnet-name mySubnet \
  --subnet-prefix 10.0.1.0/24

โœ… This creates a VNet and a Subnet at the same time.


๐Ÿ›ก๏ธ Create a Network Security Group (NSG)

bashCopyEditaz network nsg create \
  --resource-group myResourceGroup \
  --name myNSG

NSG controls inbound/outbound traffic to resources like VMs.


๐Ÿ›ก๏ธ Create an NSG Rule to Allow SSH (Linux VM)

bashCopyEditaz network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNSG \
  --name allow-ssh \
  --protocol Tcp \
  --direction Inbound \
  --priority 1000 \
  --source-address-prefixes '*' \
  --source-port-ranges '*' \
  --destination-address-prefixes '*' \
  --destination-port-ranges 22 \
  --access Allow

โœ… This opens port 22 (SSH) inbound.


๐Ÿงน 5.6 Cleaning Up Resources

When you're done, delete the Resource Group to clean up everything inside it:

bashCopyEditaz group delete --name myResourceGroup

โœ… This deletes the VMs, Storage Accounts, VNets, everything inside the group.


๐Ÿ“ Module 5 Summary

Topic
Key Points

Create VMs

az vm create

Connect to VMs

SSH (Linux) or RDP (Windows)

Manage VMs

Start, stop, restart, delete

Create Storage Accounts

az storage account create

Create Networking Components

VNet, Subnet, NSG

Clean up Resources

az group delete

Last updated