Stretching Those Azure Dollars


Hello folks,image

Last week during another Microsoft Azure Camp someone asked, How do I ensure that i can stretch that “Free Trial”? As you may know, you can get a one month free trial of Azure.  That free trial is really $220 OR 30 days witch ever comes first.

So I thought I would look at how we can Automate the automatic shutdown and deallocation of the VMs in my environment.

If automation is something you are interested in, please start with the following MVA online course:

Before we start ensure that your environment is prepared to use Runbooks.  you can start with Step-by-Step: Get Ready to use Microsoft Azure Automation to prep your environment.  Once the environment is ready.

Go to the portal, Select Automation, and open the Automation Account that you already have created using the steps in the above mentioned article.

image

Select RUNBOOKS in the menu

image

In the RUNBOOK page, Select New, App Services, Automation, Runbook, Quick Create. and enter the required info to create the new Runbook.

image

And click Create

image

Once created, select it and click Author in the menu.

image

In the authoring windows paste the following script.  (Please be sure to change the red parts with the relevant info from your environment)

workflow deallocateVM
{
   
    # Grab the management certificate
    $cert = Get-AutomationCertificate –Name ‘aut-cert’
   
    # Specify Microsoft Auzre SUBSCRIPTION ID and Name      
    $subID = '<Enter your Subscription ID Here>'
    $subName = '<Enter your Subscription Name Here>'
 
    # Set and Select the Azure SUBSCRIPTION
    Set-AzureSubscription -SubscriptionName $subName -Certificate $cert -SubscriptionId $subID
    Select-AzureSubscription -SubscriptionName $subName
   
    # shutdown Lab VMs

    #list all VMs
    $vmList = ('VM1','VM2','VM3')
    $svcName = 'CloudServiceName'

    For ( $vmCount = 0; $vmCount -lt $vmList.Count; $vmCount++)
    {
   
        $vm = Get-AzureVM -ServiceName $svcName -Name $vmList[$vmCount]
   
        if ( $vm.InstanceStatus -eq 'ReadyRole' )
        {
       
            Stop-AzureVM `
               -ServiceName $vm.ServiceName `
               -Name $vm.Name `
              -Force
        }   
    }
}

Once this is done,  Click on Save and Publish

image

Now the script is ready to run, but we want to ensure that it runs every night in case i forgot to shutdown and deallocate my VM. (see Step-By-Step: Virtual Machine billing in Azure for more info).  To do so, click on Schedule in the menu and “Link to a new schedule”.

image

Fill in the info. (We want a daily schedule that will kick start the script at 8:00pm (20:00) ) and click the checkmark

imageimage

That’s it.  now every night at 20:00, the script will start and shutdown VM1, VM2 and VM3 in the CloudServiceName so that i don't waste my resources.

There are a lot more resources for automation of Microsoft Azure at the address below in the Automation section:

http://blogs.technet.com/b/privatecloud/ 

image

Cheers!

clip_image011

Pierre Roman | Technology Evangelist
Twitter | Facebook | LinkedIn

Comments (0)

Skip to main content