2

I am trying to stop and Deallocate a linux Azure VM from the script that is running on the VM by

sudo halt

This terminates the session (ssh if interactive), stops the VM, but does not deallocate the resources. Is this somehow possible by e.g. some configuration like AllowShutdownFromGuest? There is an SO answer considering shutdown from windows guests, but not linux. There are mentions about azure-cli accessible from the linux images, but I am not able to find any documentation, does anyone know something about this?

JaKu
  • 165
  • 1
  • 7
  • How would you normally shutdown a Linux machine? Do that in your Azure Linux VM. – joeqwerty Jan 29 '19 at 12:36
  • That's the `sudo halt` which does not deallocate the machine. It stops running, but I am still paying for it. There should be a possibility to execute `curl` requests from [Azure REST api](https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/deallocate). But thought there should be less verbose linux api around that. – JaKu Jan 29 '19 at 12:40
  • Your question states that you want to stop the VM. Stopping and Deallocating a VM are 2 different things. So you want to deallocate the VM so as to not incur charges? – joeqwerty Jan 29 '19 at 13:19
  • Yes, the confusion originates in the fact, that msft uses both verbs in different places. REST endpoint is called [deallocate](https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/deallocate), in Azure cloud shell it is `az vm deallocate`, in Azure portal it is the `Stop` button and in Azure RM powershell it is [`Stop-AzureRmVM`](https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/stop-azurermvm?view=azurermps-6.13.0). To my understanding, they all do the same thing. – JaKu Jan 29 '19 at 13:28
  • `Stop-AzureRmVm` will either stop a VM or stop and deallocate a VM, depending on whether you specified `-StayProvisioned`. These are semantically two different operations. Unlike most cloud providers, Azure charges the same for a VM whether it is running or stopped, until you expressly deallocate it. So most people don't ever stop them, and if they do, it's usually for the express purpose of deallocating them. But they are still two different operations. – Michael Hampton Jan 29 '19 at 14:30
  • Yes, I am aware of the parameter. But the default setting is execution without this parameter. Then `Stop-AzureRmVm` means Deallocate and `Stop-AzureRmVm -StayProvisioned` means Stop and do not Deallocate. – JaKu Jan 29 '19 at 14:34
  • The confusing bit is that most other cloud providers make no distinction here. They always "deallocate" a stopped VM. I can't find any way to have Azure automatically deallocate a VM when it stops itself. Perhaps someone else will come up with a good idea. – Michael Hampton Jan 29 '19 at 14:37
  • I've modifed the question to maybe better distinguish between stopping and deallocation. – JaKu Jan 29 '19 at 14:43

1 Answers1

5

You can run either the Azure CLI or the newer AZ PowerShell core cmdlets on a Linux VM, both are cross platform and would allow you to do this.

CLI:

az vm deallocate -g MyResourceGroup -n MyVm

AZ Powershell:

Stop-AzVM -name myvm -resourcegroup MyResourceGroup

Sam Cogan
  • 38,736
  • 6
  • 78
  • 114