You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Change the VM size used for an Azure virtual machine.
4
-
author: cynthn
4
+
author: ericd-mst-github
5
5
ms.service: virtual-machines
6
6
ms.workload: infrastructure
7
7
ms.topic: how-to
8
-
ms.date: 2/21/2023
8
+
ms.date: 09/15/2023
9
9
ms.author: cynthn
10
-
ms.custom: devx-track-azurepowershell
10
+
ms.custom: compute-cost-fy24
11
11
12
12
---
13
13
# 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
42
42
43
43
### [CLI](#tab/cli)
44
44
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).
46
46
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.
48
48
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
+
```
63
70
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.
65
75
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.**
73
77
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 \
75
97
--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
> 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
+
```
90
123
91
124
### [PowerShell](#tab/powershell)
92
125
93
126
**Use PowerShell to resize a VM not in an availability set.**
94
127
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.
96
129
97
130
```azurepowershell-interactive
131
+
# Set variables
98
132
$resourceGroup = "myResourceGroup"
99
-
$vmName = "myVM"
100
-
```
133
+
$vm = "myVM"
134
+
$size = "Standard_DS3_v2"
101
135
102
-
List the VM sizes that are available in the region where you hosted the VM.
az vm deallocate --resource-group $resourceGroup --name $vm
115
144
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.
**Use PowerShell to resize a VM in an availability set**
133
160
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.
135
162
136
163
```azurepowershell-interactive
164
+
# Set variables
137
165
$resourceGroup = "myResourceGroup"
138
166
$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>"
148
169
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.
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.
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.
173
201
174
-
Resize and restart the VMs in the availability set.
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).
> 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.
185
215
186
216
---
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
+
187
233
## Limitations
188
234
189
235
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:
193
239
- VM (with local temp disk) -> VM (with local temp disk); and
194
240
- VM (with no local temp disk) -> VM (with no local temp disk).
195
241
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.
197
243
198
244
199
245
## Next steps
200
246
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