Azure Hands-On Labs - Use PowerShell to Resize an Azure Virtual Machine
Azure Learning Path for Cloud and DevOps Engineers
📝Introduction
This post demonstrates a hands-on lab, walk through how to find an easy solution to decrease the cost of Azure VMs in your subscription.
For example, your company's finance department explains that the cost of Azure virtual machines (VMs) is over budget, let's see how we can identify the VMs with low CPU utilization and change their size.
📌Note: I am using 2 VMs in this lab, one using Ubuntu 18.04 and the other using Windows Server 2016.
📝Log in to the Azure Management Console
Using your credentials, make sure you're using the right Region. In my case, I am using east-us
.
📌Note: You can also use the VSCode tool or from your local PowerShell to connect to Azure PowerShell
More information on how to set up it is on the link.
📝Prerequisites:
Update to PowerShell 5.1, if needed.
Install .NET Framework 4.7.2 or later.
VisualCode
Web Browser (Chrome, Edge)
📝Setting an Azure Storage Account to Load PowerShell
- Click the Cloud Shell icon
(>_)
at the top of the page.
- Click PowerShell.
- Click Show Advanced Settings. Use the combo box under Cloud Shell region to select the Region. Under Resource Group and Storage account (It's a globally unique name), enter a name for both. In the box under File Share, enter a name. Click ***Create storage (***if you don't have any yet).
📝Get the VM Size and CPU Metrics
Find the current size of the VMs, and get the VMs subscription name:
Get-AzVM
#Get the subscription information
Get-AzSubscription
Check VM CPU metrics, replacing <ID>
with the VM subscription name and <RESOURCE_GROUP_NAME>
with the group name you just copied:
az monitor metrics list --resource /subscriptions/<ID>/resourceGroups/<RESOURCE_GROUP_NAME>/providers/Microsoft.Compute/virtualMachines/<VM_NAME>
📝Resize VMs
Assign the value to the $vm
variable:
$vm = Get-AzVM -ResourceGroupName <RESOURCE_GROUP_NAME> -VMName <VM_NAME>
Perform the PowerShell commands to resize the VM(s):
$vm.HardwareProfile.VmSize = "Standard_B1s"
Update-AzVM -VM $vm -ResourceGroupName <RESOURCE_GROUP_NAME>
To view the results of the change, enter Get-AzVM
. We should see VMSize
for <VM_NAME>
changed to Standard_B1s
.
📌Note - At the end of each hands-on Lab, always clean up all the resources previously created to avoid being charged.
Congratulations — you have completed this hands-on lab covering the basics of Azure to Resize an Azure Virtual Machine with the PowerShell tool.
Thank you for reading. I hope you understood and learned something helpful from my blog.
Please follow me on CloudDevOpsToLearn and LinkedIn, franciscojblsouza