Penny Pinching in the Cloud: Automating everything with the Windows Azure Management Libraries and .NET
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:
C:\Users\scottha
λ azure vm list
info: Executing command vm list
+ Getting virtual machines
data: Name Status Location DNS Name
data: -------------------- ------------------ -------- -------------------------------
data: VSinTheSky StoppedDeallocated West US vsinthesky.cloudapp.net
data: hanselmandiscourse StoppedDeallocated West US discourse.cloudapp.net
data: hanselmansendy ReadyRole West US sendy.cloudapp.net
data: hanselmanlinuxfarm StoppedDeallocated West US linuxfarm.cloudapp.net
data: hanselmanlinuxfarm-2 StoppedDeallocated West US linuxfarm.cloudapp.net
data: hanselmanlinuxfarm-3 StoppedDeallocated West US linuxfarm.cloudapp.net
data: hanselmanmysql ReadyRole West US mysql.cloudapp.net
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:
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. https://www.nuget.org/packages/Microsoft.WindowsAzure.Management.Libraries
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
SmallestDotNet
Space: northcentraluswebspace
Hanselminutes
Space: northeuropewebspace
Space: westuswebspace
babysmash
nerddinnerofficial
ratchetandthegeek
speakinghacks
thisdeveloperslife
hanselmanlyncrelay
HanselmanGhost
anglebrackets
lostphonescreen
loggingtest
GetInvolved
keysleft
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.
- Penny Pinching in the Cloud: When do Azure Websites make sense?
- Penny Pinching in the Cloud: Enabling New Relic Performance Monitoring on Windows Azure Websites
- Penny Pinching Video: Moving my Website's Images to the Azure CDN (and using a custom domain)
- Penny Pinching Video: Moving an Azure Website between data centers
- Pinching pennies when scaling in The Cloud - lazy loading images and using the Azure CDN to save money
- Penny Pinching in the Cloud: How to run a two day Virtual Conference for $10
Also, I encourage you to check out Azure Friday, a new show I'm doing at http://friday.azure.com. 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.
About Newsletter
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.
Anyway, no biggie; I am happy that I had the alert set up or these holidays would have been quite bad.
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.
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
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.
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?
Comments are closed.
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?