Azure

Fun with Azure: Creating an Azure function

Sometimes you need to run some code – somewhere.  You don’t care where.  But clients need to be able to get to it.  You don’t want servers, VMs, or web apps, just something very lightweight.  So Azure Functions sounded like a workable solution.  I’m starting from a blank slate:

image

I clicked to add an app and filled in the blanks:

image
image
image

Resources were successfully created and deployed:

image

From there, I can click “Go to resource” to get to here:

image

And then “New function” to create one.  I’ll choose “In-portal” to keep things simple:

image

And then “Webhook + API”:

image

So now I’m left with a code sample that takes a “name” query parameter and writes a response containing that value.  So it’s a good starting point:

image

But I want to use a counter, so I am going to add an input:

image

And I can give my new table a name:

image

While I’m at it, I’ll make this anonymous and turn off POST requests (not necessary in my case, GET is fine):

image

Let’s take a detour into the Azure Table objects in the storage account tied to this function:

image

I can click to add one and specify the name, countTable, but to see what’s in it I need to switch to Storage Explorer, where I can add an entity (row) and specify values:

image

How exciting, one column added.  But it gets me set up for what I really wanted to do:  Create a function that generates a sequential computer name.  That shouldn’t be too hard, but I chose to do it with C# script (basically, code compiled on the fly) which made debugging a little bit more challenging.  And I added in Azure table storage, which I hadn’t used before either.  So let’s just say it took me a couple of hours to get to this:

image

The code itself can be found in an attachment at the bottom of this blog post.

So I can then call it pretty easily from PowerShell.  The simplest example:

image

Magic.  That’s a little more straight-forward than the last time I did something similar, and it’s entirely in the cloud.

Attachment: run.csx.zip

Categories: Azure