Azure virtual machine auto shutdown

In this Azure tutorial, we will discuss the Azure virtual machine auto shutdown option.

We will also discuss the below topics

  • How to disable notification before auto-shutdown
  • Azure VM accelerated networking
  • How to check if the accelerated networking is enabled for your virtual machine
  • Azure VM Shutdown
  • Use Azure CLI to Shutdown Azure VM
  • Azure Automation Start/Stop VMs PowerShell
  • Azure VM Stopped (Deallocated Automatically)
  • Enable Auto Shutdown Azure VM PowerShell
  • Python Script To Stop Azure VM

We can set the Azure virtual machine auto shutdown option in Azure portal.

Azure virtual machine auto shutdown

Follow the below steps to enable the Azure virtual machine auto shutdown option in the Azure portal.

Step- 1:

Login to https://portal.azure.com/.

Step- 2:

Search for Virtual machines.

How to enable auto shutdown option for your Azure virtual machine

Step- 3:

You will see the list of VM created in your Azure subscription. It will show the VM name, Type, Status, ResourceGroup, Location, etc.

How to connect the virtual machine in Azure

Step- 4:

Now click on the VM name. “MyNewVM” in my case. You can able to see the details of your virtual machine like Public IP address, status, Computer name, Operating system, Size, etc.

azure virtual machine auto shutdown

Step- 5:

Now from the left menu, select the “Auto-shutdown” option under Operations section.

How to enable the Auto-shutdown option for your Azure VM.

Step- 6:

Now fill the below options

  • Enabled option as On
  • Scheduled shutdown: Mention the time when you want your VM should automatically shut down in the Scheduled shutdown option,
  • Time zone: Specify your time zone based on the country you belong to.
  • Send notification before auto-shutdown?: Make the option as Yes so that you will get notified in your email-id that your virtual machine is going to be shut down at the specified time.
  • Webhook URL: You can provide the URL If you want something to happen in sync with the machine being shut down.
  • Email address: You can specify the email id where you will get the notification regarding the shutdown of your virtual machine.
How to enable Auto-shutdown option in Azure VM

Step- 7:

Before 30 minutes you will get an email saying that your virtual machine is going to shut down.

Email notification for azure VM auto shutdown option

You can able to Postpone the shut down for 1 hour or 2 hour from the email itself by clicking on below two links as shown above

  • Postpone: 1 hour
  • Postpone: 2 hours

After you click on Postpone: 1 hour or Postpone: 2 hours link, a new notification will be sent 30 minutes prior to the new shutdown time.

You can also click on Skip it by clicking on Skip this instance link.

This is how we can enable Azure virtual machine auto shutdown option.

How to disable notification before auto-shutdown

If you want to disable the notification to be sent before auto-shutdown, From the below window you can select No for Send notification before auto-shutdown? option and then click on the Save button.

how to disable notification before auto-shutdown

This is how you can enable the Azure virtual machine auto shutdown option for your azure virtual machine(VM).

Azure virtual machine (VM) bandwidth

Azure provides different bandwidth depending upon the size of the virtual machine.

As we know already, the resources and hardware are being shared by the virtual machines from the host computer. Same way network bandwidth also being shared by all the virtual machines among them.

The NB(network bandwidth) of the virtual machine is measured in megabits per second (Mbps).

This is based on the virtual machine size you selected while creating the virtual machine.

The network bandwidth allocated to individual VM is metered based on the outbound traffic from the virtual machine

Bandwidth allocated to any of the virtual machines equals the sum of all outbound traffic across all network interfaces attached to that particular virtual machine.

The number of network connections present on a particular virtual machine at any given time can affect its network performance.

Azure vm accelerated networking

Accelerated networking is a new feature introduced to boost the network bandwidth of a virtual machine in Azure.

It helps to reduce utilization of the CPU, provides more capacity for processing the massive amounts of data, etc

It helps to push more data at once by providing more bandwidth to the network.

This feature is not supported by all virtual machines of any size. It is a specific virtual machine with some specific size.

How to check if the accelerated networking is enabled for your virtual machine

Follow the below steps to check if the accelerated networking is enabled for your virtual machine.

Step- 1: 

Login to https://portal.azure.com/

Step- 2: 

Search for Virtual machines there.

Azure virtual machine bandwidth

Step- 3: 

You will see the list of VM created in your Azure subscription. It will show the VM name, Type, Status, ResourceGroup, Location, etc.

What is Azure vm accelerated networking

Step- 4: 

Now click on the VM name. “MyNewVM” in my case. You can able to see the details of your virtual machine like Public IP address, status, Computer name, Operating system, Size etc.

Azure vm accelerated networking benefits

Step- 5:

Now click on the Networking option under Settings.

Azure vm accelerated networking

Step- 6:

In the blow window if you can able to see “Accelerated networking” is Disabled for me.

how to enable Azure vm accelerated networking

How to enable accelerated networking in your virtual machine

You can enable the accelerated networking while creating the virtual machine initially.

Check the below steps to enable this option.

There are two cases you can see here while enabling the accelerated networking option

Login to the portal and go to Create a virtual machine option. you can refer to my last blog How to Create Azure VM (Virtual Machine)

Case -1:

Fill out the details in the Basic tab and then click on the Networking tab.

Enable accelerated networking option for your virtual machine

If you will see above for this particular VM, we can’t enable the accelerated networking option. The selected VM size does not support this option.

You can notice here On option is disabled you can’t enable this option in this case.

Case-2:

In this case, the accelerated networking option supports for this particular virtual machine size.

Fill out the details in the Basic tab and select the Size of the VM as “Standard DS3 v2” .

How to enable accelerated networking option for your virtual machine

Now you click on the “Networking” tab. You can enable accelerated networking option here for this virtual machine

Select the “On” option for accelerated networking field to enable it.

How to disable accelerated networking option for your virtual machine

How to enable Accelerated Networking on existing VMs using Azure PowerShell.

You can enable the accelerated networking option on the existing virtual machine but the prerequisites are The virtual machine must be a supported size for accelerated networking.

Follow the below steps

Step- 1:

We must first stop or deallocate the VM for which you want to enable the accelerated networking option.

Stop-AzVM -ResourceGroup "newresgroup" -Name "MyNewVM"

Step- 2:

You can enable accelerated networking option on the NIC of your VM

$mynic = Get-AzNetworkInterface -ResourceGroupName "newresgroup" -Name "mynewvm843NIC"

$mynic.EnableAcceleratedNetworking = $true

$mynic | Set-AzNetworkInterface

Step- 3:

Now you can restart your virtual machine using the below command. Now if you will go and check for the networking tab, the accelerated networking option must be enabled.

Start-AzVM -ResourceGroup "newresgroup"  -Name "MyNewVM"

Azure VM Shutdown

In case you want, You can manually shutdown the Azure Virtual machine using the below steps.

  1. Log in to the Azure Portal.
  2. Search for Virtual Machines and click on the search result Virtual machines.
  3. Now, on the Virtual machines page, you can able to see the list of Virtual Machines created by you. Click on the specific virtual machine that you want to shut down manually.
Azure VM Shutdown option

4. On the specific Virtual Machine page, click on the Overview tab from the left side –> Click on the Stop button as highlighted below to shutdown your Azure VM.

Azure VM Shutdown

Use Azure CLI to Shutdown Azure VM

We can also use Azure CLI to shut down our Azure Virtual machine. Use the below script to shut down the Azure Virtual Machine.

# You can Stop the Azure VM using below cmdlet
az vm stop --name <Your vm name> --g <Your resource group name>
# You can Deallocate Azure VM using the below cmdlet
az vm deallocate --name <Your vm name> -g <Your resource group name>

Example:

az vm stop --name MyNewVM --g newresgroup

az vm deallocate --name MyNewVM --g newresgroup

Start VM Azure PowerShell

You can use the below Azure PowerShell cmdlet to start an Azure Virtual Machine.

Start-AzVM -ResourceGroup "Your resource Group Name" -Name "Virtual Machine Name"

Example: Below cmdlet will start the “TsInfoVM1” virtual machine under the “Demo123” resource group.

Start-AzVM -ResourceGroup "Demo123" -Name "TsInfoVM1"

Once you will execute the above Azure PowerShell cmdlet, you will get the output as below and it will start the Virtual machine.

OperationId : 403c32fd-53d0-43ee-ab0f-8bbd1aae06fb
Status      : Succeeded
StartTime   : 6/27/2022 12:05:26 PM
EndTime     : 6/27/2022 12:05:39 PM
Error       : 

Then you can check the status here

start vm azure powershell

Azure Automation Start/Stop VMs PowerShell

If you have a number of Virtual Machines and you want to automate the start and stop the process of the Azure Virtual Machines, the better idea here is to use your own PowerShell script using the PowerShell Workflow.

Make sure to set the credentials in the Automation account with the needed permission for the user.

The PowerShell script is as below

Note: The below script is to stop the virtual machines for starting the VMs, you need to make a slight change on the below script like instead of Stop-AzVm, use Start-AzVm, and then you will have to change the condition $status.

workflow Stop-VM
{
    $Cred = Get-AutomationPSCredential -Name "VMUserName"
    $uName = $Cred.UserName
    $pswd = $Cred.Password
    $PsCred = New-Object System.Management.Automation.PSCredential ($uName,$pswd)

    Connect-AzAccount -Credential $PsCred
        
    $myvms = Get-AzVM | Where-Object {$_.Tags.Keys -eq "NameofyourTag" -or $_.Tags.Keys -eq "Valueofyourtag"} | Select-Object Name, ResourceGroupName, Tags
        
    foreach -parallel ($vm in $myvms)
    { 
        $status = (Get-AzVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Status).Statuses[1].DisplayStatus
        if ($status -ne "VM deallocated")
        {
            Write-Output "Stopping $($vm.Name)";         
            Stop-AzVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force;
        } 
    } 
}

Azure VM Stopped (Deallocated Automatically)

In some scenarios, you might see that the Azure Virtual Machine has stopped and deallocated automatically. Basically, you need to make sure few key points here.

  1. Make sure you have enough Azure Credit to run the Virtual Machine and you are not out of shortage of Azure Credit.
  2. There might be a chance of any urgent update from Microsoft.
  3. Another important point is there might be a chance anyone has done this action manually. To make sure what has gone wrong, you can check out the below information.
  • Log in to the Azure Portal.
  • Search for Virtual Machines and click on the search result Virtual machines.
  • Now, on the Virtual machines page, you can able to see a list of Virtual Machines created by you. Click on the specific virtual machine where you want to check the activity.
  • Click on the “activity log” and then specify the time period when you want to look at it. This will give you a clear picture of what actions have been performed for the particular virtual machine.

Enable Auto Shutdown Azure VM PowerShell

Well, if you want to enable the auto-shutdown option in your Azure Virtual Machine using PowerShell, then note that there is no direct PowerShell cmdlet to enable the auto-shutdown

Azure VM Auto-Shutdown ARM Template

Possible to do it using a simple ARM template, where you can use the cmdlet New-AzureRmResourceGroupDeployment to import the template.

There is also one more way to achieve the same functionality i.e By using a script with the New-AzureRmResource cmdlet with the properties needed.

You can check out the complete PowerShell script and ARM template here.

Python Script To Stop Azure VM

Like PowerShell, you can also write a Python script to stop or start an Azure Virtual Machine. You can get the Python Script now. Or, You can also check out the Python Script to stop or start an Azure Virtual Machine.

Azure virtual machine auto shutdown – Video Tutorial

You may like the following Azure tutorials:

Conclusion

In this Azure tutorial, We discussed

  • Azure virtual machine auto shutdown
  • Virtual machine bandwidth in Azure
  • Azure VM accelerated networking
  • How to check if accelerated networking is enabled for your virtual machine
  • Enable Accelerated Networking on existing VMs using Azure power shell.
  • Azure VM Shutdown
  • Use Azure CLI to Shutdown Azure VM
  • Azure Automation Start/Stop VMs PowerShell
  • Azure VM Stopped (Deallocated Automatically)
  • Enable Auto Shutdown Azure VM PowerShell
  • Python Script To Stop Azure VM

Hope, you have enjoyed this article !!!