Monday, January 30, 2017

Azure Kudu Deployment Version Number

I recently blogged about generating a version.txt file on every build via Visual Studio. This solution is fine if you are building from you local machine and using Web Deploy to deploy to Azure, but if you have a github webhook and are using kudu, the VS Post Build Events are not executed.

The easiest way I found to generate a version.txt file for every deployment is to add a post deployment action to your Web App/App Service.

Create a file within the /site/deployments/tools/PostDeploymentActions/ folder of your App Service (you may need to create that folder). You can name the file whatever you'd like, I chose `generategitversionfile.cmd`. The file should contain at least the following line:

git rev-parse HEAD > "..\wwwroot\version.txt"

This command will be executed within the context of the `/site/repository` directory, that's why we need a more fully qualified path for where to write the file.


Now, after every kudu deploy, a new version.txt file will be created that contains the current git comment hash.


Hope this helps,
Aaron

Thursday, January 26, 2017

Stop Azure WebJob from WebSite

I recently had the need to toggle the status of an Azure WebJob from the WebSite in the same Azure App Service. I found a couple half-complete answers online, but I thought I'd put together this simple guide to have a complete example all in one place.

To accomplish this, we'll be using the kudu REST API for WebJobs.

(note: I also looked into toggling the WEBJOBS_STOPPED environment variable from the WebSite, however I was not able to verify that solution was going to work. I confirmed I was able to set the environment variable from the website, however the value did not update in the "Application Settings" section of the Azure Portal and I was concerned the WebJobs may read a separate environment variable than the WebSite [process scoped vs machine envar for example])

The first thing you'll need to do is download the PublishSettings file (aka "Publish Profile") associated with the App Service (you need the username/password from this file to satisfy the basic auth requirement to the kudu API)



In the "Web Deploy" publish profile section of this file, find the "userName" and "userPWD" values, we'll need these in the code linked below.


The full documentation describing all the various REST endpoints is available here. I have created a simple gist of how to authenticate using basic auth and call a few WebJob methods to start and stop services here: https://gist.github.com/aaronhoffman/212dd858ff5d4275369f5fde01d9fd5d


Hope this helps!
Aaron



Visual Studio Version Build by Git Commit Hash

Semantic versioning is great, but more often than not, when I'm looking at a folder of deployed binaries, I want to know the git commit hash of the source code that was used to generate them (a versioned container might be better in some cases, but that's not applicable in every situation at the moment).

A simple way to accomplish this is to generate a "version.txt" file at build time that will eventually be deployed with the code. Visual Studio makes it pretty easy to automatically generate this version.txt file with it's built in "build events". Simply add the following line to the "Post-build event command line:" textbox, and a version.txt file will appear in your build output directory with the current commit hash after a successful build.

"C:\Program Files\Git\bin\git.exe" rev-parse HEAD > "$(ProjectDir)$(OutDir)version.txt"



Hope this helps!
Aaron





Wednesday, January 25, 2017

Replace Azure Scheduler with Azure Functions

I have written before about the shortcomings of the Azure Scheduler (doesn't integrate with classic [non-RM] Azure Storage accounts, message body pushed to Azure Storage Queue is wrapped in XML, costs $15/mo for basic scheduling tasks...) but the good news is, now with Azure Functions, your Scheduler Tasks can mostly be replaced with Functions.


Here are a couple tables to break down the differences in Triggers and Actions.


Scheduler Triggers compared to Function Input Bindings



Scheduler
Functions
Manual
X
X
Timer
X
X
Http

X
Storage Queue

X
Service Bus Queue

X
Service Bus Topic

X
Blob

X
Event Hub

X


Scheduler Actions compared to Built-in Function Output Bindings 



Scheduler
Functions
Http
X
X
Storage Queue
X
X
Service Bus Queue
X
X
Service Bus Topic
X
X
Notification Hub

X
Event Hub

X
Table Storage

X
DocumentDB

X
Blob

X
Bot Framework

X
SendGrid

X
Twilio

X
[custom code]

X


You lose some of the nice built-in Azure Scheduler Retry functionality, and Error Action functionality, however for most of my use cases, those could easily be replaced with some custom code in the Azure Function itself.


Another great benefit of Azure Functions, is that they are essentially free. As with AWS Lambdas, the first 1 Million Executions, and 400,000 GB-s of Execution Time, are 100% free, and the costs are very reasonable if you go beyond that (1M Executions for $0.20, and $16 for 1M GB-s of Execution Time)


Below, I'll demonstrate replacing a Timer Trigger Azure Scheduler Task that drops a message into a Storage Queue, with an Azure Function that performs the same task.


On the Azure Scheduler side, this setup is very easy, everything is driven through the Azure Portal UI. On the Azure Functions side, everything can still be accomplished through the Portal UI, but there are some differences. After you have added an Azure Function App to your subscription, open the app and select the "New Function" option.

You'll be greeted with the Choose a Template UI:


Select the "TimerTrigger-CSharp" template, name your new function, and configure the Time schedule. The schedule uses a cron scheduler expression, here are some examples:


"* * * * * *" - run every second
"0 * * * * *" - run every minute, at *:00
"0 */5 * * * *" - run every 5 minutes
"0 0 * * * *" - run every hour, on the hour *:00:00

The format is:
{second} {minute} {hour} {day} {month} {dayofweek}


After you click the Create button, you'll see some C# code like this:


You can modify the code directly in the Portal UI, save, run and test the results.

Click on the "Integrate" tab on the left hand side, to see the various Input/Output bindings. Select "+ New Output", then "Azure Queue Storage":



On the next screen, configure the Queue you would like the Function to write to, and the name of the output parameter we'll need to modify in the code. By default, the param name is outputQueueItem.


Back on the Develop tab, add the outputQueueItem output param to your Azure Function method signature, and assign a value to the string variable. This will be the exact value that is added to the Storage Queue (imagine that, MS won't wrap it in XML... amazing...)



And that's it! Now a message will appear in your Storage Queue every 5 minutes! You can pick it up with a WebJob, or handle it with yet another Function... whatever you'd like.



Hope this helps!
Aaron






Saturday, January 14, 2017

Census.gov QuickFacts Data Set

To create a visualization for https://www.sizzleanalytics.com/ I had to compile census information from the census.gov QuickFacts tool. As for as I could tell, they don't provide an easy way to automatically download all the data, so I manually downloaded each state then used a simple node script to merge them together.

Data set available here:
https://data.world/aaronhoffman/census-gov-state-quickfacts


Hope this helps,
Aaron


Thursday, January 12, 2017

Azure Functions and AWS Lambdas

I'm working on a new talk about Azure Functions and AWS Lambdas. I'm writing this blog post to compile a list of links to more information for people that attended the talk.


Presentation Slides: https://drive.google.com/open?id=0BwgLvVq0rcS7cmxrQUhCWUxTNTQ

Azure Function Intro Source Code: https://github.com/Stonefinch/AzureFunctionIntro

Azure Intro Web App Source Code: https://github.com/Stonefinch/AzureIntro

Azure Function Tools for Visual Studio: https://blogs.msdn.microsoft.com/webdev/2016/12/01/visual-studio-tools-for-azure-functions/

AWS Lambda C# in Visual Studio: https://aws.amazon.com/blogs/developer/using-the-aws-lambda-project-in-visual-studio/


I'll add more links and update the slides as I continue to build out the talk.


Hope this helps,
Aaron