Use Visual Studio Team Services Build to manage your Azure Resource Groups

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.

image

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.

image

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.

image

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

image

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

image

 

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

image

On the properties of the task, choose the Subscription, Select the [Stop Virtual Machines] action and fill in the Resource Group Name

image

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

image

Of course you can add as many tasks as you like!

Happy building !

Advertisements

One Response to “Use Visual Studio Team Services Build to manage your Azure Resource Groups”

  1. 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. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: