After I elaborated a bit on one of the base requirements to work with the Azure Service Management API at all which is enabling a valid certificate to authenticate your calls to the API I want today share some thoughts about how to automate tasks related with Azure deployments. As already pointed out in Chapter I you probably want to use Windows Powershell together with the Windows Azure Service Management CmdLets which will make your life in interacting with the Windows Azure Fabric from a shell environment much easier. The commandlets support most of the relevant tasks to do deployments and undeployments.
So in my example here I will show how those commandlets can be used in scripts to optimize the consumption of Windows Azure compute services. As you might know the compute service is billed based on service hours which basically means that as long as a service is deployed it will produce cost although the service may not be used at all during certain times. And as there may be quite some solutions where there are known time windows in which the service isn’t used (e.g. non busines hours) it may come in handy if there would be the possibility to undeploy and deploy the services based on their availability needs.
To have this work the only thing needed is two Windows Powershell scripts where on does the deployment and the other one the undeployment. The script for undeploying a service could look like the following:
After creating this script you then can create a scheduled job using the Windows Scheduler. When creating the command line for the scheduled task you have to be carefull about two things:
- Set the execution policy for powershell so that it allows to run scripts. Depending on your environment and security policies you should set the execution policy to either “AllSigned”, “RemoteSigned” or “Unrestricted”. For more information consult the powershell documentation. Example: Set-ExecutionPolicy Unrestricted
- If your path to the powershell script which you need to pass over to powershell as an argument does contain blanks you need to use double quotes surrounding the whole argument including the ampersand (which is the call operator in Windows Powershell) and inside enclose the path information with single quotes. So a correct statement would look like this: powershell.exe -command "& ‘C:\My Scripts\undeployAzureService.ps1’" …
To create the scheduled task using the wizard or use a XML template like the one below, insert your data and import it into the scheduler.
Then do the same for the deployment process. A respective Windows Powershell script could look like this:
And the template for the scheduler could look like this:
This is all to automate your deployment tasks. One thing however is to remember here. As the New-Deployment commandlet uploads your deployment package into the blob storage of the specified Azure Storage account you should check your blob storage from time and do a clean up otherwise this will also produce unnecessary cost.