2

The VM info:

  • Basic A1 Serie VM with Ubuntu 16 on it

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.

AK_
  • 1,879
  • 4
  • 21
  • 30
  • 2
    This seems like a question specifically about Azure. Why are you also including AWS in the question? On AWS if you set the `instance-initiated-shutdown-behavior` on the instance to `terminate`, then `shutdown -h 0` will terminate (deallocate) the instance. However that doesn't seem to answer you question since you appear to be using Azure, so again, why are you including AWS in your question? – Mark B Dec 09 '17 at 15:00
  • @MarkB because I use both Azure and AWS. It seems that AWS is different than azure in this. I removed AWS in my last edit to make things clear – AK_ Dec 09 '17 at 22:05
  • @markB - for my info, Is there something similar in Azure? – AK_ Dec 10 '17 at 13:02

4 Answers4

2

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

AK_
  • 1,879
  • 4
  • 21
  • 30
  • Yes indeed - I was wondering if there is somekind of Azure guest tool to allow inter-connection between guest and host which will eventually allow me to deallocate the VM. – AK_ Dec 09 '17 at 14:10
  • just for future references; this indeed seems to be impossible if you don't have credentials to azure, if you do however, you can use Azure CLI as explained in this answer: https://stackoverflow.com/a/44648207/1442789 – AK_ Dec 09 '17 at 22:13
  • 1
    Hi,@AK_. Just for clarify. The CLI cmdlets in that answer also needs the action for logging Azure . Such as that it needs to run in Azure automation. As Berklogic said, you cannot deallocate the Azure VM without Azure credentials. – Wayne Yang Dec 11 '17 at 01:45
1

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 johndoe@contoso.com -p VerySecret

To Deallocate:

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

0

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"

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
0

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

The Bahree
  • 53
  • 1
  • 10
  • can you give a detailed example? – AK_ Dec 15 '17 at 16:54
  • You need to create an Azure Automation account first. Then create a new runbook there. Inside the runbook add PS code for deallocating the VM. In the runbook, first log into Azure through Automation account registered service principal. Next, select the option to create Webhook on your runbook from within the portal. You will get a Uri to Invoke the webhook. Use this Uri to remotely trigger the runbook anytime from anywhere, by issuing an HTTP post request, like using Postman. HTH. – The Bahree Dec 19 '17 at 12:08
  • Pls mark my response as answer if it helps your purpose. – The Bahree Dec 19 '17 at 12:09