0
0
Azurecloud~5 mins

VM states (running, stopped, deallocated) in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Virtual machines (VMs) in Azure can be in different states that affect billing and availability. Understanding these states helps you manage costs and know when your VM is ready to use.
When you want to temporarily stop a VM but keep its resources reserved for quick restart.
When you want to stop a VM and avoid charges for compute resources by fully deallocating it.
When you need to check if a VM is running before connecting to it.
When you want to save costs by stopping VMs that are not in use.
When you want to restart a VM to apply updates or fix issues.
Commands
This command starts the VM named 'example-vm' in the resource group 'example-rg'. Starting a VM changes its state to running, making it ready to use.
Terminal
az vm start --resource-group example-rg --name example-vm
Expected OutputExpected
Starting VM 'example-vm'... Succeeded
--resource-group - Specifies the resource group where the VM exists
--name - Specifies the name of the VM to start
This command stops the VM but does not deallocate it. The VM state changes to stopped, but compute resources are still reserved and billed.
Terminal
az vm stop --resource-group example-rg --name example-vm
Expected OutputExpected
Stopping VM 'example-vm'... Succeeded
--resource-group - Specifies the resource group where the VM exists
--name - Specifies the name of the VM to stop
This command stops and deallocates the VM. The VM state changes to deallocated, releasing compute resources and stopping billing for them.
Terminal
az vm deallocate --resource-group example-rg --name example-vm
Expected OutputExpected
Deallocating VM 'example-vm'... Succeeded
--resource-group - Specifies the resource group where the VM exists
--name - Specifies the name of the VM to deallocate
This command retrieves the current state of the VM. The output shows if the VM is running, stopped, or deallocated.
Terminal
az vm get-instance-view --resource-group example-rg --name example-vm --query instanceView.statuses[1]
Expected OutputExpected
{ "code": "PowerState/running", "displayStatus": "VM running", "level": "Info", "time": null }
--resource-group - Specifies the resource group where the VM exists
--name - Specifies the name of the VM
--query - Filters the output to show only the VM power state
Key Concept

If you remember nothing else from this pattern, remember: stopping a VM keeps billing, deallocating a VM stops billing and frees resources.

Common Mistakes
Using 'az vm stop' expecting to stop billing for the VM.
Stopping a VM does not release compute resources, so billing continues.
Use 'az vm deallocate' to stop billing by releasing compute resources.
Trying to start a VM that is deallocated without specifying the resource group or name correctly.
The command fails because Azure cannot find the VM without correct identifiers.
Always specify the correct resource group and VM name with --resource-group and --name flags.
Assuming the VM is running without checking its state before connecting.
Connecting to a stopped or deallocated VM will fail because it is not available.
Use 'az vm get-instance-view' to check the VM state before connecting.
Summary
Use 'az vm start' to power on a VM and make it ready for use.
Use 'az vm stop' to stop a VM but keep billing active by reserving resources.
Use 'az vm deallocate' to stop billing by releasing the VM's compute resources.
Check VM state with 'az vm get-instance-view' to know if it is running, stopped, or deallocated.