3 Steps to Serverless Computing in Microsoft Azure


Serverless computing.  An interesting term that is getting quite a bit of press.  My guess is because of its name.  How can computing be serverless?  Just like cloud computing, there is a server somewhere.  The serverless part comes from your not needed to maintain nor pay for a server nor an operating system - true Platform as a Service (PaaS).  As a developer, you write code and only pay by the transaction when that code is run.  By contrast, Infrastructure as a Service (IaaS) is charged for on an hourly basis whether active or not.  Microservices is a better, more accurate name for serverless computing, but serverless gets more press due to its shock value.  Serverless has also been called Function as a Service (FaaS) and Back-end as a Service (BaaS).

I find microservices extremely interesting as the next evolution of agility in the cloud journey.  Physical servers have been replaced and simplified by virtual servers by removing hardware dependencies.  Virtual servers have been simplified (if not replaced) by containers by removing OS dependencies.  Containers could be even further simplified my microservices by removing application package dependencies.  If you are a developer, you just want to write code, not worry about package dependencies, operating systems, virtual machines nor physical hardware.  Microservices allow the developer to just write code.

You can find the Wikipedia entry for microservices here and the entry for serverless computing here.

The Microsoft Azure version of microservices is Azure Functions.  Microsoft's description of Azure Functions is "An event-based serverless compute experience to accelerate your development. It can scale based on demand and you pay only for the resources you consume."  You can find a nice discussion of everything Azure Functions here.

Microsoft makes it easy to get started with Functions.  Here are the steps:


Step 1: Create a Function App

At the time of the writing of this post, Functions were not organized into one of the Azure categories.  Click + and type Function in the search field.  You will see the result Function App come up.  Click that, then choose Function App from the next page, then Create .





After clicking Create, you will be presented with this page asking you a few details about your new Function App.  Fill in the fields and click Create.  It should take about a minute for your Function App to be created.



The next step is to choose a pre-made function template or to write your function from scratch.  Let's keep our lives easy and choose the pre-made C# Webhook function template and click Create this function.



Step 2: Modify the Function App

In about one second, your template will be ready, you will be placed in the function IDE and a tutorial walk-through will be displayed.  Step through the tutorial to get yourself familiar with the IDE interface.  The Develop section is where you write your code, the Integrate section is where you connect input, output and triggers, the Function App Settings section is where you set setting for you app,  



This sample function is already functional. If you click Test (in the upper right) and then Run, you will see the function run and the output displayed in the Output window.




Step 2: Test the Function App

The variable being used in this function is "name".  If you paste the Function URL at the top of the page into a web browser and append &name=YourName, the function will run and display "Hello YourName".  For my first function test, I typed in a Magic 8 Ball case statement that would pick a random answer to a question each time the function was run.  The code looks like this:

// Magic 8 ball generator
    int numberOfChoices = 20;
    string response = "Default";
    Random rnd = new Random();
            switch (rnd.Next(numberOfChoices+1))
            {
                case 0:
                    response = "It is decidedly so";
                    break;
                case 1:
                    response = "Without a doubt";
                    break;
                case 2:
                    response = "Yes, definitely";
                    break;
                case 3:
                    response = "You may rely on it";
                    break;
                case 4:
                    response = "As I see it, yes";
                    break;
                case 5:
                    response = "Most likely";
                    break;
                case 6:
                    response = "Outlook good";
                    break;
                case 7:
                    response = "Yes";
                    break;
                case 8:
                    response = "Signs point to yes";
                    break;
                case 9:
                    response = "Reply hazy; try again";
                    break;
                case 10:
                    response = "Ask again later";
                    break;
                case 11:
                    response = "Better not tell you now";
                    break;
                case 12:
                    response = "Cannot predict now";
                    break;
                case 13:
                    response = "Concentrate and ask again";
                    break;
                case 14:
                    response = "Don't count on it";
                    break;
                case 15:
                    response = "My reply is no";
                    break;
                case 16:
                    response = "My sources say no";
                    break;
                case 17:
                    response = "Outlook not so good";
                    break;
                case 18:
                    response = "Very doubtful";
                    break;
                case 19:
                    response = "It is certain";
                    break;
            }
    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please ask a question in the query string")

        : req.CreateResponse(HttpStatusCode.OK, "The answer to '" + name + "' is " + response);

Here is an example run of my function:

https://faucher2.azurewebsites.net/api/HttpTriggerCSharp2?code=VhzXFh9Y[snip]6WqBJQ==&name=Is this a good blog post? 

The answer to 'Is this a good blog post?' is Better not tell you now

That's basically it for a simple Microsoft Azure Function App example.  Pretty easy and the first 10,000 calls are free.  I hope you have enjoyed this post.  I look forward to your comments.




Comments