Skip to content

Commit 8009277

Browse files
committed
Merge branch 'main' of https://github.com/MicrosoftDocs/azure-docs-pr into ip-move
2 parents 20d2f91 + f9ff51e commit 8009277

File tree

1 file changed

+156
-106
lines changed

1 file changed

+156
-106
lines changed
Lines changed: 156 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: Resize a virtual machine
33
description: Change the VM size used for an Azure virtual machine.
4-
author: cynthn
4+
author: ericd-mst-github
55
ms.service: virtual-machines
66
ms.workload: infrastructure
77
ms.topic: how-to
8-
ms.date: 2/21/2023
8+
ms.date: 09/15/2023
99
ms.author: cynthn
10-
ms.custom: devx-track-azurepowershell
10+
ms.custom: compute-cost-fy24
1111

1212
---
1313
# Change the size of a virtual machine
@@ -42,87 +42,114 @@ If your VM is still running and you don't see the size you want in the list, sto
4242

4343
### [CLI](#tab/cli)
4444

45-
To resize a VM, you need the latest [Azure CLI](/cli/azure/install-az-cli2) installed and logged in to an Azure account using [az login](/cli/azure/reference-index).
45+
To resize a VM, you need the latest [Azure CLI](/cli/azure/install-az-cli2) installed and logged in to an Azure account using [az sign-in](/cli/azure/reference-index).
4646

47-
1. View the list of available VM sizes on the current hardware cluster using [az vm list-vm-resize-options](/cli/azure/vm). The following example lists VM sizes for the VM named `myVM` in the resource group `myResourceGroup` region:
47+
The below script checks if the desired VM size is available before resizing. If the desired size isn't available, the script exits with an error message. If the desired size is available, the script deallocates the VM, resizes it, and starts it again. You can replace the values of `resourceGroup`, `vm`, and `size` with your own.
4848

49-
```azurecli-interactive
50-
az vm list-vm-resize-options \
51-
--resource-group myResourceGroup \
52-
--name myVM --output table
53-
```
54-
55-
2. If you find the desired VM size listed, resize the VM with [az vm resize](/cli/azure/vm). The following example resizes the VM named `myVM` to the `Standard_DS3_v2` size:
56-
57-
```azurecli-interactive
58-
az vm resize \
59-
--resource-group myResourceGroup \
60-
--name myVM \
61-
--size Standard_DS3_v2
62-
```
49+
```azurecli-interactive
50+
# Set variables
51+
resourceGroup=myResourceGroup
52+
vm=myVM
53+
size=Standard_DS3_v2
54+
55+
# Check if the desired VM size is available
56+
if ! az vm list-vm-resize-options --resource-group $resourceGroup --name $vm --query "[].name" | grep -q $size; then
57+
echo "The desired VM size is not available."
58+
exit 1
59+
fi
60+
61+
# Deallocate the VM
62+
az vm deallocate --resource-group $resourceGroup --name $vm
63+
64+
# Resize the VM
65+
az vm resize --resource-group $resourceGroup --name $vm --size $size
66+
67+
# Start the VM
68+
az vm start --resource-group $resourceGroup --name $vm
69+
```
6370

64-
The VM restarts during this process. After the restart, your VM will keep existing OS and data disks. Anything on the temporary disk will be lost.
71+
> [!WARNING]
72+
> Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.
73+
>
74+
> If you are resizing a production VM, consider using [Azure Capacity Reservations](capacity-reservation-overview.md) to reserve Compute capacity in the region.
6575
66-
3. If you don't see the desired VM size, deallocate the VM with [az vm deallocate](/cli/azure/vm). This process allows you to resize the VM to any size available that the region supports. The following steps deallocate, resize, and then start the VM named `myVM` in the resource group named `myResourceGroup`:
67-
68-
```azurecli-interactive
69-
# Variables will make this easier. Replace the values with your own.
70-
resourceGroup=myResourceGroup
71-
vm=myVM
72-
size=Standard_DS3_v2
76+
**Use Azure CLI to resize a VM in an availability set.**
7377

74-
az vm deallocate \
78+
The below script sets the variables `resourceGroup`, `vm`, and `size`. It then checks if the desired VM size is available by using `az vm list-vm-resize-options` and checking if the output contains the desired size. If the desired size isn't available, the script exits with an error message. If the desired size is available, the script deallocates the VM, resizes it, and starts it again.
79+
80+
81+
```azurecli-interactive
82+
# Set variables
83+
resourceGroup="myResourceGroup"
84+
vmName="myVM"
85+
newVmSize="<newVmSize>"
86+
availabilitySetName="<availabilitySetName>"
87+
88+
# Check if the desired VM size is available
89+
availableSizes=$(az vm list-vm-resize-options \
90+
--resource-group $resourceGroup \
91+
--name $vmName \
92+
--query "[].name" \
93+
--output tsv)
94+
if [[ ! $availableSizes =~ $newVmSize ]]; then
95+
# Deallocate all VMs in the availability set
96+
vmIds=$(az vmss list-instances \
7597
--resource-group $resourceGroup \
76-
--name myVM
77-
az vm resize \
98+
--name $availabilitySetName \
99+
--query "[].instanceId" \
100+
--output tsv)
101+
az vm deallocate \
102+
--ids $vmIds \
103+
--no-wait
104+
105+
# Resize and restart the VMs in the availability set
106+
az vmss update \
78107
--resource-group $resourceGroup \
79-
--name $vm \
80-
--size $size
81-
az vm start \
108+
--name $availabilitySetName \
109+
--set virtualMachineProfile.hardwareProfile.vmSize=$newVmSize
110+
az vmss start \
82111
--resource-group $resourceGroup \
83-
--name $vm
84-
```
85-
86-
> [!WARNING]
87-
> Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.
88-
>
89-
> If you are resizing a production VM, consider using [Azure Capacity Reservations](capacity-reservation-overview.md) to reserve Compute capacity in the region.
112+
--name $availabilitySetName \
113+
--instance-ids $vmIds
114+
exit
115+
fi
116+
117+
# Resize the VM
118+
az vm resize \
119+
--resource-group $resourceGroup \
120+
--name $vmName \
121+
--size $newVmSize
122+
```
90123

91124
### [PowerShell](#tab/powershell)
92125

93126
**Use PowerShell to resize a VM not in an availability set.**
94127

95-
Set some variables. Replace the values with your own information.
128+
This script sets the variables `$resourceGroup`, `$vm`, and `$size`. It then checks if the desired VM size is available by using `az vm list-vm-resize-options` and checking if the output contains the desired size. If the desired size isn't available, the script exits with an error message. If the desired size is available, the script deallocates the VM, resizes it, and starts it again.
96129

97130
```azurepowershell-interactive
131+
# Set variables
98132
$resourceGroup = "myResourceGroup"
99-
$vmName = "myVM"
100-
```
133+
$vm = "myVM"
134+
$size = "Standard_DS3_v2"
101135
102-
List the VM sizes that are available in the region where you hosted the VM.
103-
104-
```azurepowershell-interactive
105-
Get-AzVMSize -ResourceGroupName $resourceGroup -VMName $vmName
106-
```
136+
# Check if the desired VM size is available
137+
if ((az vm list-vm-resize-options --resource-group $resourceGroup --name $vm --query "[].name" | ConvertFrom-Json) -notcontains $size) {
138+
Write-Host "The desired VM size is not available."
139+
exit 1
140+
}
107141
108-
If you see the size you want listed, run the following commands to resize the VM. If you don't see the desired size, go on to step 3.
109-
110-
```azurepowershell-interactive
111-
$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName
112-
$vm.HardwareProfile.VmSize = "<newVMsize>"
113-
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup
114-
```
142+
# Deallocate the VM
143+
az vm deallocate --resource-group $resourceGroup --name $vm
115144
116-
If you don't see the size you want listed, run the following commands to deallocate the VM, resize it, and restart the VM. Replace **\<newVMsize>** with the size you want.
117-
118-
```azurepowershell-interactive
119-
Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force
120-
$vm = Get-AzVM -ResourceGroupName $resourceGroup -VMName $vmName
121-
$vm.HardwareProfile.VmSize = "<newVMSize>"
122-
Update-AzVM -VM $vm -ResourceGroupName $resourceGroup
123-
Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName
145+
# Resize the VM
146+
az vm resize --resource-group $resourceGroup --name $vm --size $size
147+
148+
# Start the VM
149+
az vm start --resource-group $resourceGroup --name $vm
124150
```
125151

152+
126153
> [!WARNING]
127154
> Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.
128155
>
@@ -131,59 +158,78 @@ Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName
131158

132159
**Use PowerShell to resize a VM in an availability set**
133160

134-
If the new size for a VM in an availability set isn't available on the hardware cluster currently hosting the VM, then you will need to deallocate all VMs in the availability set to resize the VM. You also might need to update the size of other VMs in the availability set after one VM has been resized. To resize a VM in an availability set, perform the following steps.
161+
If the new size for a VM in an availability set isn't available on the hardware cluster currently hosting the VM, then you need to deallocate all VMs in the availability set to resize the VM. You also might need to update the size of other VMs in the availability set after one VM has been resized. To resize a VM in an availability set, run the below script. You can replace the values of `$resourceGroup`, `$vmName`, `$newVmSize`, and `$availabilitySetName` with your own.
135162

136163
```azurepowershell-interactive
164+
# Set variables
137165
$resourceGroup = "myResourceGroup"
138166
$vmName = "myVM"
139-
```
140-
141-
List the VM sizes that are available on the hardware cluster where you hosted the VM.
142-
143-
```azurepowershell-interactive
144-
Get-AzVMSize `
145-
-ResourceGroupName $resourceGroup `
146-
-VMName $vmName
147-
```
167+
$newVmSize = "<newVmSize>"
168+
$availabilitySetName = "<availabilitySetName>"
148169
149-
If you see the size you want listed, run the following commands to resize the VM. If you don't see it listed, go to the next section.
150-
151-
```azurepowershell-interactive
170+
# Check if the desired VM size is available
171+
$availableSizes = Get-AzVMSize `
172+
-ResourceGroupName $resourceGroup `
173+
-VMName $vmName |
174+
Select-Object -ExpandProperty Name
175+
if ($availableSizes -notcontains $newVmSize) {
176+
# Deallocate all VMs in the availability set
177+
$as = Get-AzAvailabilitySet `
178+
-ResourceGroupName $resourceGroup `
179+
-Name $availabilitySetName
180+
$virtualMachines = $as.VirtualMachinesReferences | Get-AzResource | Get-AzVM
181+
$virtualMachines | Stop-AzVM -Force -NoWait
182+
183+
# Resize and restart the VMs in the availability set
184+
$virtualMachines | Foreach-Object { $_.HardwareProfile.VmSize = $newVmSize }
185+
$virtualMachines | Update-AzVM
186+
$virtualMachines | Start-AzVM
187+
exit
188+
}
189+
190+
# Resize the VM
152191
$vm = Get-AzVM `
153-
-ResourceGroupName $resourceGroup `
154-
-VMName $vmName
155-
$vm.HardwareProfile.VmSize = "<newVmSize>"
192+
-ResourceGroupName $resourceGroup `
193+
-VMName $vmName
194+
$vm.HardwareProfile.VmSize = $newVmSize
156195
Update-AzVM `
157-
-VM $vm `
158-
-ResourceGroupName $resourceGroup
196+
-VM $vm `
197+
-ResourceGroupName $resourceGroup
159198
```
160-
161-
If you don't see the size you want listed, continue with the following steps to deallocate all VMs in the availability set, resize VMs, and restart them.
162199

163-
Stop all VMs in the availability set.
164-
165-
```azurepowershell-interactive
166-
$availabilitySetName = "<availabilitySetName>"
167-
$as = Get-AzAvailabilitySet `
168-
-ResourceGroupName $resourceGroup `
169-
-Name $availabilitySetName
170-
$virtualMachines = $as.VirtualMachinesReferences | Get-AzResource | Get-AzVM
171-
$virtualMachines | Stop-AzVM -Force -NoWait
172-
```
200+
This script sets the variables `$resourceGroup`, `$vmName`, `$newVmSize`, and `$availabilitySetName`. It then checks if the desired VM size is available by using `Get-AzVMSize` and checking if the output contains the desired size. If the desired size isn't available, the script deallocates all VMs in the availability set, resizes them, and starts them again. If the desired size is available, the script resizes the VM.
173201

174-
Resize and restart the VMs in the availability set.
175-
176-
```azurepowershell-interactive
177-
$availabilitySetName = "<availabilitySetName>"
178-
$newSize = "<newVmSize>"
179-
$as = Get-AzAvailabilitySet -ResourceGroupName $resourceGroup -Name $availabilitySetName
180-
$virtualMachines = $as.VirtualMachinesReferences | Get-AzResource | Get-AzVM
181-
$virtualMachines | Foreach-Object { $_.HardwareProfile.VmSize = $newSize }
182-
$virtualMachines | Update-AzVM
183-
$virtualMachines | Start-AzVM
184-
```
202+
### [Terraform](#tab/terraform)
203+
204+
To resize your VM in Terraform code, you modify the `size` parameter in the `azurerm_linux_virtual_machine` or `azurerm_windows_virtual_machine` resource blocks to the desired size and run `terraform plan -out main.tfplan` to see the VM size change that will be made. Then run `terraform apply main.tfplan` to apply the changes to resize the VM.
205+
206+
> [!IMPORTANT]
207+
> The below Terraform example modifies the size of an existing virtual machine when you're using the state file that created the original virtual machine. For the full Terraform code, see the [Windows Terraform quickstart](./windows/quick-create-terraform.md).
208+
209+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-windows-vm-with-iis-server/main.tf" range="91-117" highlight="8":::
210+
211+
> [!WARNING]
212+
> Deallocating the VM also releases any dynamic IP addresses assigned to the VM. The OS and data disks are not affected.
213+
>
214+
> If you are resizing a production VM, consider using [Azure Capacity Reservations](capacity-reservation-overview.md) to reserve Compute capacity in the region.
185215
186216
---
217+
218+
## Choose the right SKU
219+
220+
When resizing a VM, it's important to choose the right SKU based on the signals from the VM to determine whether you need more CPU, memory, or storage capacity:
221+
222+
- If the VM is running a CPU-intensive workload, such as a database server or a web server with high traffic, you may need to choose a SKU with more CPU cores.
223+
- If the VM is running a memory-intensive workload, such as a machine learning model or a big data application, you may need to choose a SKU with more memory.
224+
- If the VM is running out of storage capacity, you may need to choose a SKU with more storage.
225+
226+
227+
For more information on choosing the right SKU, you can use the following resources:
228+
- [Sizes for VMs in Azure](sizes.md): This article lists all the VM sizes available in Azure.
229+
- [Azure VM Selector](https://azure.microsoft.com/pricing/vm-selector/): This tool helps you find the right VM SKU based on your workload type, OS and software, and deployment region.
230+
231+
232+
187233
## Limitations
188234

189235
You can't resize a VM size that has a local temp disk to a VM size with no local temp disk and vice versa.
@@ -193,9 +239,13 @@ The only combinations allowed for resizing are:
193239
- VM (with local temp disk) -> VM (with local temp disk); and
194240
- VM (with no local temp disk) -> VM (with no local temp disk).
195241

196-
For a work-around, see [How do I migrate from a VM size with local temp disk to a VM size with no local temp disk? ](azure-vms-no-temp-disk.yml#how-do-i-migrate-from-a-vm-size-with-local-temp-disk-to-a-vm-size-with-no-local-temp-disk---). The work-around can be used to resize a VM with no local temp disk to VM with a local temp disk. You will create a snapshot of the VM with no local temp disk > create a disk from the snapshot > create VM from the disk with appropriate [VM size](sizes.md) that supports VMs with a local temp disk.
242+
For a work-around, see [How do I migrate from a VM size with local temp disk to a VM size with no local temp disk? ](azure-vms-no-temp-disk.yml#how-do-i-migrate-from-a-vm-size-with-local-temp-disk-to-a-vm-size-with-no-local-temp-disk---). The work-around can be used to resize a VM with no local temp disk to VM with a local temp disk. You create a snapshot of the VM with no local temp disk > create a disk from the snapshot > create VM from the disk with appropriate [VM size](sizes.md) that supports VMs with a local temp disk.
197243

198244

199245
## Next steps
200246

201-
For more scalability, run multiple VM instances and scale out. For more information, see [Automatically scale machines in a Virtual Machine Scale Set](../virtual-machine-scale-sets/tutorial-autoscale-powershell.md).
247+
- For more scalability, run multiple VM instances and scale out.
248+
- For more SKU selection information, see [Sizes for virtual machines in Azure](sizes.md).
249+
- To determine VM sizes by workload type, OS and software, or deployment region, see [Azure VM Selector](https://azure.microsoft.com/pricing/vm-selector/).
250+
- For more information on Virtual Machine Scale Sets (VMSS) sizes, see [Automatically scale machines in a VMSS](../virtual-machine-scale-sets/tutorial-autoscale-powershell.md).
251+
- For more cost management planning information, see the [Plan and manage your Azure costs](/training/modules/plan-manage-azure-costs/1-introduction) module.

0 commit comments

Comments
 (0)