If you, just like me, have a MSDN subscription you are also provided with some serious Azure Credits. Because I am a Microsoft MVP I received the MSDN Enterprise subscription, allowing me to run 130 euro worth of Azure services.
Because I do a lot of demos and experiments I always have a couple of Virtual Machines running in Azure. But… If you have a number of Virtual Machines running, and the VM’s are a bit faster than the cheapest option, credits burn up pretty quick.
I have used the Azure Automation Accounts, but that included powershell scripts that I had to maintain somewhere, notification were hard to set up etc.
I then I had an Idea. In the fantastic new Build engine of Visual Studio Team Services, there are native tasks for managing Azure Resource Groups. Like creating and deleting them, but also Starting and Stopping Virtual Machines. All working out of the Box with security arranged with either credentials or a Service Principal. And the good thing is, they can be scheduled easier than ever.
So in this post I will walk you through the steps to set up a VSTS build that uses te hosted agent to stop your VM’s on Azure. This saves you money and time. You can use the same steps to start them just before work or use VSTS alerts to get notified when something went wrong.
Set up VSTS
So first we need to create a VSTS Team Project. I Created an Empty project for this, with a Git Repository. I added the Readme.MD file so the repo was not empty.
Then we need to hook up our Azure account to this Team Project. For that we go to the administration Tab, by clicking the gear icon on the right hand top, and click on Services
In here we need to pay attention. For Azure Resource Groups, we cannot use Certificates. We can choose credentials, this is the easiest way. But if you use a Microsoft Account for your Azure subscription and not a Work/Corporate account, you will get an error running the build stating that
-Credential parameter can only be used with Organization ID credentials.
I chose to use the Service Principal. To get a working Service Principal, You should follow either this manual here or the section [Create Service Principal] and [Grant Access] from the post of Donovan Brown.
In short you need to
- Add an application to your Azure Active Directory
- Get the subscription ID, TenantID, and ClientID (Service Principa ID) and Key from this Application Service Principal
- Grant Access to your Subscription to this application by adding it as a contributor . See the post of Donovan how to do this.
After this you have your Azure Subscription linked to your VSTS project
Now it is time to set up the build. Navigate to the build Tab, Create a New Build Definition and add the Build Task Azure Resource Group Deployment
On the properties of the task, choose the Subscription, Select the [Stop Virtual Machines] action and fill in the Resource Group Name
Last thing you need to do is add a schedule. Navigate to the Triggers Tab. I’ve set it up to shut down every night at 20:00 PM
Of course you can add as many tasks as you like!
Happy building !
Smart! I’ve talked to other Azure-VM-users who indeed have a hard time maintaining their scripts for stopping and starting their machines: this is simple but brilliant. 🙂