Skip to main content

Azure Development Environment - Control when an Azure VM runs

Azure Development Environment Series:
In the last post we talk about how we can reduce the cost of Azure development environment of a small team by using an Azure VM to store all the databases that are needed by developers.
In this post we will see how we can optimize the cost of this Azure VM, by running it only when developers are active.

Context
Usually the development team works from 08:00 AM to 07:00 PM. Don't imagine that they stay for 11 hours at work. We have colleagues that comes at 08:00 AM and leave at 04:30 PM, others are coming at 10:00 AM and will stay until 07:00 PM.
Based on this information we observe that between 07:00 PM to 08:00 AM nobody is working. This means that the Azure VM is not required to run in this time interval.
There are 12h (plus 1h buffer) when nobody is working, when we could stop our VM. This means that we can reduce the costs for this service with 50%. At 07:00 PM we could shut down the VM and at 07:30 AM to start the VM again.

Manual
One solution is to talk with the team and the last person that leave the office should shut down the VM and in the morning the first guy that arrives at the office should start the VM. It will work, but why we should do this manually.

Azure Dev-Test Labs
A clean and nice solution for our problem is Azure Dev-Test Labs. This allow us to specify a time when the VM should shut down automatically. This can be done by defining a policy related to that specific VM (https://azure.microsoft.com/en-us/documentation/articles/devtest-lab-set-lab-policy/#set-auto-shutdown). Unfortunately, it doesn't allow us to start the VM automatically. In the morning we will still need somebody to start the VM.
  

Azure Automation
Another approach to automate this tasks is to use Azure Automation. This service enable us to run any kind of power-shell script directly from Azure. On top of this, we can specify when a task to be run.
We need to create two different runbooks. One that will run every morning and will start the VM and another in the evening that will shut down the Azure VM. 
To be able to access and control your subscription, you will need to add your credentials to the Azure Automation. The most simple way is to create a new Automation account and use your certificate for credentials. 
Once you done this, you can create the runbook for starting the VM. Don't forget to link the new runbook to the automation account that you created a step before. After this, you can add the power-shell script that start the VM (script can be found at the end of the post). 
The last step is to link the runbook to scheduler (a new one or to an existing one). This can be done from the Schedule tab of the runbook.

A scheduler can run only one time (at a time specific by us), hourly or daily. In the last two cases we can specify recurrence time (hours/days) and an expiration time. We cannot specify the day of the week when we want our runbook to run, but from the script we can check if is working day or not (see in the scripts).
A nice feature of Azure Automation is the price. First 500 minutes are free. In our case, the start operation takes around 10 minutes and the stop operation takes around 5 minutes. It means that we are consuming 450 minutes. Don't bother for price per minute because is very cheap -  0.0017e per minute (0.102e per hour). 

Conclusion
We identify that we need an Azure VM that is used by development team only only for 12h per day. We looked at 3 different ways to control when the Azure VM should run (manual, Azure Dev-Test Labs, Azure Automation). In this moment Azure Automation is the one that offers us all the capabilities that we need to automate this with minimal effort.


Start the VM:
workflow start-devsql-vm
{ 
    Param (  
        [parameter(Mandatory=$true)] 
        [String] 
        $VirtualMachineName,
        
        [parameter(Mandatory=$true)] 
        [String]
        $ResourceGroupName 
    )  
    
    $day = (Get-Date).DayOfWeek
    if ($day -eq 'Saturday' -or $day -eq 'Sunday'){
        exit
    }
        
    $SubscriptionName = Get-AutomationVariable -Name "SubscriptionName" 
    $SubscriptionId = Get-AutomationVariable -Name "SubscriptionID" 
    $CertificateThumbprint = Get-AutomationVariable -Name "CertificateThumbprint" 
    
    $TenantId = Get-AutomationVariable -Name "TenantId"
  $CertificateThumbprint = Get-AutomationVariable -Name "CertificateThumbprint"
    $ADApplicationId = Get-AutomationVariable -Name "ADApplicationId"
    
    $certificateName = Get-AutomationVariable -Name "CertificateName" 
    $certificate = Get-AutomationCertificate -Name $certificateName  
    
    # Set subscription profile
    Set-AzureSubscription -SubscriptionName $SubscriptionName -SubscriptionId $SubscriptionId -Certificate $certificate

    # Select subscription as the current context
    Select-AzureSubscription -SubscriptionName $SubscriptionName
    
    # Login to Azure Resource Manager
    Add-AzureRmAccount -TenantId $TenantId -ServicePrincipal -CertificateThumbprint $CertificateThumbprint -ApplicationId $ADApplicationId  
    
    $vm =  Get-AzureRmVM -Name $VirtualMachineName -ResourceGroupName $ResourceGroupName -Status  
    
    # Starting the virtual machine  
    Start-AzureRmVM -Name $Virt$VirtualMachineName -ResourceGroupName $ResourceGroupName 
}

Stop the VM:
workflow stop-devsql-vm
{ 
    Param (  
        [parameter(Mandatory=$true)] 
        [String] 
        $VirtualMachineName,
        
        [parameter(Mandatory=$true)] 
        [String]
        $ResourceGroupName 
    )  
    
    $day = (Get-Date).DayOfWeek
    if ($day -eq 'Saturday' -or $day -eq 'Sunday'){
        exit
    }
        
    $SubscriptionName = Get-AutomationVariable -Name "SubscriptionName" 
    $SubscriptionId = Get-AutomationVariable -Name "SubscriptionID" 
    $CertificateThumbprint = Get-AutomationVariable -Name "CertificateThumbprint" 
    
    $TenantId = Get-AutomationVariable -Name "TenantId"
  $CertificateThumbprint = Get-AutomationVariable -Name "CertificateThumbprint"
    $ADApplicationId = Get-AutomationVariable -Name "ADApplicationId"
    
    $certificateName = Get-AutomationVariable -Name "CertificateName" 
    $certificate = Get-AutomationCertificate -Name $certificateName  
    
    # Set subscription profile
    Set-AzureSubscription -SubscriptionName $SubscriptionName -SubscriptionId $SubscriptionId -Certificate $certificate

    # Select subscription as the current context
    Select-AzureSubscription -SubscriptionName $SubscriptionName
    
    # Login to Azure Resource Manager
    Add-AzureRmAccount -TenantId $TenantId -ServicePrincipal -CertificateThumbprint $CertificateThumbprint -ApplicationId $ADApplicationId  
    
    $vm =  Get-AzureRmVM -Name $VirtualMachineName -ResourceGroupName $ResourceGroupName -Status  
    
    # Stopping the virtual machine  
    Stop-AzureRmVM -Name $Virt$VirtualMachineName -ResourceGroupName $ResourceGroupName -Force
}

Comments

Popular posts from this blog

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

[Post-Event] Codecamp Conference Cluj-Napoca - Nov 19, 2016

Last day I was invited to another Codecamp Conference, that took place in Cluj-Napoca. Like other Codecamp Conferences, the event was very big, with more than 1.000 participants and 70 sessions. There were 10 tracks in parallel, so it was pretty hard to decide at  what session you want to join.
It was great to join this conference and I hope that you discovered something new during the conference.
At this event I talked about Azure IoT Hub and how we can use it to connect devices from the field. I had a lot of demos using Raspberry PI 3 and Simplelink SensorTag. Most of the samples were written in C++ and Node.JS and people were impressed that even if we are using Microsoft technologies, we are not limited to C# and .NET. World and Microsoft are changing so fast. Just looking and Azure IoT Hub and new features that were launched and I'm pressed (Jobs, Methods, Device Twin).
On backend my demos covered Stream Analytics, Event Hub, Azure Object Storage and DocumentDB.

Title:
What abo…