AK_
AK_

Reputation: 2069

Stop and deallocate an Azure VM within the guest OS

The VM info:

How to shutdown (or Stop) and de-allocate the VM in Azure without having to go to the azure portal?

Bearing in mind in Azure if you execute shutdown -h 0; it will stop the VM but won't deallocate it.

I'm asking this because I don't have a direct connection to the Azure portal nor do I have logins to it. I want to deallocate the VM using the guest OS only.

Upvotes: 3

Views: 4007

Answers (5)

Kerem Atasen
Kerem Atasen

Reputation: 89

It is possible to do that using terraform:

resource "null_resource" "deallocate_vm" {
  provisioner "local-exec" {
    command = "az vm deallocate --resource-group 
    ${azurerm_linux_virtual_machine.XXX.resource_group_name} --name 
    ${azurerm_linux_virtual_machine.XXX.name}"
  }

  depends_on = [azurerm_linux_virtual_machine.XXX]
}

Upvotes: 0

The Bahree
The Bahree

Reputation: 73

An alternative you can consider is creating a runbook in Azure Automation and adding PowerShell code to deallocate the VM from there.

Next, you can create a Webhook to this runbook from within Azure Automation, which you can call externally through an HTTP Post request from anywhere (like from Postman, or using CURL, or anywhere you can issues an HTTP Post from) to trigger the VM deallocation.

Now anytime you want to deallocate the VM, you can use the Webhook URL from say within Postman tool, Issue an HTTP Post request, and the runbook will deallocate the VM. I have already done this for few scenarios as part of a complex workflow, and it works perfectly.

HTH

Upvotes: 0

user8470717
user8470717

Reputation:

I understand what's your scenario. You can SSH to the VM only, but cannot access the Azure. For Example you don't know the credential of Azure account.

For this scenario, you cannot deallocate the Azure VM. Because that action must need been done with Azure Account and it is completed in Azure, not just VM. Deallocate the Azure VM also links to other resources, such as Public IP address, Disk, Network interface and etc.

If you still want to deallocate the Azure VM, I suggest you call your owner of this Azure VM who can access azure portal.

Another hack around this problem is to use Azure CLI to login within the guest VM then control the VM. You will still need to login to Azure, but this is required only once.

  1. Download and install Azure CLI as guided here
  2. Generate a an authentication code using the command:

    az login

  3. This will generate an authentication code that can be used to log you in via the Azure owner (only required once). Use that code to authenticate.

  4. After login in you can issue this command to stop and deallocate your VM:

    az vm deallocate --resource-group myResourceGroup --name myVM

list of available azure CLI commands can be found here

Upvotes: 2

Dan Patrick
Dan Patrick

Reputation: 11

Do you have a login to the subscription? If so you could use the azure CLI to deallocate. The VM always has access to the Azure Fabric even if you don't.

To login without access to portal:

az login -u [email protected] -p VerySecret

To Deallocate:

az vm deallocate --resource-group myResourceGroup --name myVM

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72171

You can use any means of automation, Powershell\CLI\different SDK to shutdown the vm. you can créate a script and to just execute it. Example for Azure Powershell.

Stop-AzureRmVM -ResourceGroupName "ResourceGroup11" -Name "VirtualMachine07"

Upvotes: 0

Related Questions