Scott Hanselman

Penny Pinching in the Cloud: Automating everything with the Windows Azure Management Libraries and .NET

December 16, 2013 Comment on this post [10] Posted in Azure
Sponsored By

Even though I work for the Azure and ASP.NET team, I still pay for my own Azure account. Frankly, if I did get free cloud time not only would I likely abuse it, but I also wouldn't be using the cloud in the way it's meant to be used! From my perspective, a good cloud is a cheap cloud. I use CDNs, caching, and focus on a balance of cheap resources and compute that work for me when I need them, and are asleep (and not costing me money) when I don't.

There's things like auto-scale for websites, for example, based on either CPU or a schedule, but the one thing that gets me into trouble (sometimes) is leaving big virtual machines on. I manage most of my Azure assets from the command line, so I can just "azure vm list" to see what's running and I'll see this:

λ azure vm list
info: Executing command vm list
+ Getting virtual machines
data: Name Status Location DNS Name
data: -------------------- ------------------ -------- -------------------------------
data: VSinTheSky StoppedDeallocated West US
data: hanselmandiscourse StoppedDeallocated West US
data: hanselmansendy ReadyRole West US
data: hanselmanlinuxfarm StoppedDeallocated West US
data: hanselmanlinuxfarm-2 StoppedDeallocated West US
data: hanselmanlinuxfarm-3 StoppedDeallocated West US
data: hanselmanmysql ReadyRole West US
info: vm list command OK

If I want to shut one down I can use the command line, the Azure Portal, or even manage VMs within Visual Studio itself:

Managing Azure within Visual Studio

But sometimes I have custom apps to manage Azure resources, perhaps WinForms or WPF apps, MSBuild Tasks, or I want to add cloud management to an existing process. I'll want to not just turn off VMs, but also manage WebSites, create resources, upload things to storage and more.

I just learned from Brady Gaster that there's now Windows Azure Management Libraries for .NET as part of the Azure SDK. Basically this means that you can call the same backend REST APIs that the Azure Portal calls now, except now with a simple wrapper around them that makes it rather a bit easier in .NET.

Brady has a lot of info on his blog about this libraries. Here's some of the best highlights:

  • Supports Portable Class Libraries (PCL)
  • Ships as a set of focused NuGet packages with minimal dependencies to simplify versioning
  • Supports async/await-based task asynchrony (with easy synchronous overloads)
  • Has a shared infrastructure for common error handling, tracing, configuration, and HTTP pipeline manipulation
  • Is factored for easy testability and mocking
  • Is built on top of popular libraries such as HttpClient and Json.NET
  • Is all open source and on GitHub.

They are on NuGet as a group of packages but you can get them separately if you want to just manage VMs, for example. Here's the version I'm using from NuGet. Note it's prerelease as of the date of this writing. 


Azure authentication and authorization is based on X509 Certificates, so you'll use those to initially talk to your Azure instance. You can download your certificates from an authenticated session here and your certificates and subscription id are inside.

I can list out, for example, all my running websites in all their locations (web spaces):

using (var client = new WebSiteManagementClient(creds)) {
var spaces = client.WebSpaces.List();
foreach (var space in spaces) {
Console.WriteLine("Space: {0}", space.Name);

var sites = client.WebSpaces.ListWebSites(space.Name, new WebSiteListParameters{ PropertiesToInclude = { "Name" }});
foreach (var site in sites) {
Console.WriteLine(" " + site.Name);

This little app gives me this output:

Space: eastuswebspace
Space: northcentraluswebspace
Space: northeuropewebspace
Space: westuswebspace

I can go and update sites, disable (stop) them, make web farms, scale them, create, configure and generally do everything I can from the command line. Very slick. Now I can manage Azure stuff from PowerShell, from .NET, from node and the cross platform command line, Visual Studio, and the Azure Portal.

Go read all about Windows Azure Management Libraries on Brady's blog, get them on NuGet, or read the code and enter issues on GitHub.

if you link your MSDN and Azure accounts and you can get up to $150 a month in Azure credits, so up to two free VMs running all day for a month.

I've done a few posts on "Penny Pinching in the Cloud" that you may enjoy.

Also, I encourage you to check out Azure Friday, a new show I'm doing at Azure Friday also on iTunes as a downloadable podcast in HD!

Sponsor: Thanks to Aspose for sponsoring the blog feed this week! Aspose.Total for .NET has all the APIs you need to create, manipulate and convert Microsoft Office documents and a host of other file formats in your applications. Curious? Start a free trial today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
December 16, 2013 14:12
I love your articles, truly. I am also beginning to hate Azure with a fiery passion :)

First time I tried to use it? Free trial, here's an example you can play with. Didn't work. (I'm talking about a Microsoft tutorial or something like that. It just wouldn't run correctly.) Got billed because of the data that got transferred. (Not much, and the bill was cancelled, but I still was annoyed.)

Second time I tried? Recently, after your previous article. I created a nice tiny VM, well about 3 times 'cause it kept not being what I wanted :) but anyway. I uploaded a web app to it. Or so I thought. Fortunately, I set up cost monitoring - four days later, I was informed that I went past my 15 euros a month quota. It seems that uploading the web app created a "cloud service", whatever that is, which somehow was distinct from my VM. (Even though I told it to upload it to my VM.)

Oh well. Now I deleted the cloud service and the VM but I cannot delete the container... because it has a disk used by a VM on it. (Even though the VM is gone.)

So - you and "the Gu" are my favorite Microsoft employees but Azure is still less than awe-inspiring. Maybe next year?
December 16, 2013 16:33
Maybe you follow the wrong tutorial?
December 16, 2013 18:10

Basically, everything end up running in a VM somewhere. What you actually did was to create and provision a website or a web role.

If I remember correctly, VM can't have automatic deployment like web role or website. You manage them all by yourself.

My recommendation is to always go through the management portal after deployment to make sure that things are what you expect. Could save you a lot of money.
December 16, 2013 18:49
@Maxime: if I recall correctly, I right-clicked the VM in Visual Studio and chose deploy or something like that. It created a "cloud service" (again, I am sorry I haven't saved a screenshot or something, but I just wanted to get rid of the whole thing). There was no mention of a website or web role.

Anyway, no biggie; I am happy that I had the alert set up or these holidays would have been quite bad.
December 16, 2013 19:47
I've switched to doing about 1/2 my development work from an Azure VM. I would love the ability to "Turn Off/On at H:MM, unless I flip a switch to make an exception for the day."

One of those things I've been meaning to write, but a) haven't had time and b) expect the Azure Feature Machine to crank out any cycle now.
December 22, 2013 11:58
Acceleration is a fast growing start-up which is having their ERP Web solutions for Education, Finance & Hospitality domain. Recently they have started with training modules for Dot Net / JAVA / PHP technologies & Android / Windows 8 Framework application development. Good opportunity for 6 Week / Internship program for all Engineering & MCA.
They are Industry professionals to train & get you prepared for IT Job Interviews. The Good part is they have seperately added a interview preparatory technical programs to get you qualify in any IT interview.

Add-Ones for training profile: -

--> Technical Leaders & Managers from Industry to train
--> Live project development Exposure
--> Valid Pvt ltd company experience
--> 100% Job Assistance
--> Personality Development Sessions + Resume Writing Tips
--> Free Mock Tests for written exams for Interview Cracking
December 23, 2013 23:53
wow. Too much stuff. Not for my little brain. I know I do good and build good stuff. I know I can find an easier way to use cloud computing. Too much busy work on this one, not enough business. all due respect.
December 31, 2013 6:38
I've switched to doing about 1/2 my development work from an Azure VM. I would love the ability to "Turn Off/On at H:MM, unless I flip a switch to make an exception for the day."

One of those things I've been meaning to write, but a) haven't had time and b) expect the Azure Feature Machine to crank out any cycle now.
January 22, 2014 23:23
I've been trying to use the Azure Management Library, and I used your code above, but I get an error "String not recognized as valid boolean" on this line:

var sites = client.WebSpaces.ListWebSites(space.Name, new WebSiteListParameters{ PropertiesToInclude = { "Name" }});

It pulls in the space name just fine, but I get this error every time I try to access the websites. I've tried using client.WebSites.Get(space.Name, "sitename", new WebSiteGetParameters()) and get the same error. I'm not sure why. I do have two subscriptions, but made sure I'm using the correct subscriptionid for the credentials. I updated to the latest version of the library, and that didn't help. Any thoughts?
January 27, 2014 22:25
Thanks to Brady Gaster who helped me resolve the error I was getting. Read all about it in his blog post here:

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.