Friday, 2 April 2021

Like It or Not - Azure Function

 👍Here is what I like about Azure Functions:

  1. Resource Group - the concept of resource group is a good one. It allows me to see all the related resources in the same screen. In the Numbers Game example, it lists the related App Service Plan, Function App and Application Insights on the same screen (on Portal and VSCode). Also, if I want to delete the whole thing, I can just delete the resource group. That's just a few of the goodies that RG offers.
  2. Function App - this is another layer of the hierarchy that can group all the related functions under the same app. Settings like Keys, Logging, CORS can be specified at this level and apply to all the functions of the app. However, this is a double-edged sword (see below).
  3. Portal GUI is very intuitive
  4. Console and the other Development Tools - I can go into the console to see what's happening behind the scene. (OK, for serverless you shouldn't have to care about the server or what's behind the scene. But I am curious).
  5. Code is quite performant - speed of execution is pretty impressive - around 500ms - 800ms to solve the game. 
  6. Although not required for my function, I like the fact that Azure allows you to scale up by changing the App Service Plan. It would have been even nicer to be able to combine AWS' concurrency control with the scaling options here. 
👎What I don't quite like about Azure Functions:
  1. Portal GUI (and VSCode) loading of the screens are quite slow - loading of code takes a couple of seconds (with only 350 - 400 lines of code). However, the App Service Editor development tool from the portal is much quicker. (in fact, the Azure CLI/Powershell feel very slow in general).
  2. The language/Stack setting is at Function App level. Therefore, I am stuck with the same language for all functions underneath. In my example, the main code of solve game was developed a few years ago in Javascript, so it's natural to choose Node.js. However, the GetNumbersGameHtml function would be much easier to write in Python taking advantage of its """ multi-line string syntax. 
My overall impression about Azure Function is very positive albeit the tooling may still need some polishing and some missing features like data mapping and validation declaratively outside of the code.

Thursday, 1 April 2021

Numbers Game on Azure Functions

Porting the Numbers Game onto Azure Function is so easy!
Just like in the AWS Lambda approach, I created two Azure Functions in the same Function App:
  • GetNumbersGameHtml - triggered by HTTP GET
  • SolveNumbersGame - triggered by HTTP POST
Both functions are configured with the same Function Key value.

Code for GetNumbersGameHtml:


module.exports = async function (context, req) {
    const key= req.headers["x-functions-key"] || req.query.code;

    context.res = {
        // status: 200, /* Defaults to 200 */
        headers: {
            "Content-Type": "text/html"
        },
        body: myHtml+key+myHtml2
    };
}

const myHtml = '<!DOCTYPE html>\n'+
'<html>\n'+...
Code for SolveNumbersGame:

module.exports = async function (context, req) {
    const e=req.body;
    var numbers=[e.n1,e.n2,e.n3,e.n4,e.n5,e.n6], target=e.target;
    context.log("solving: "+JSON.stringify(e));
	var answer=solveAllGames(numbers, target);
    
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: JSON.stringify(answer)
    };
}
...
Although the speed of Azure's CLI/Powershell/Portal is very slow comparing to AWS, the Function execution is blazingly fast - between 500ms-800ms - this is on the lowest App Service Plan (F1). The AWS Lambda function can be 10x as slow, usually between 3-8s - I had to increase the timeout setting of the function. In fact, to cater for the long wait time on the web GUI, I downloaded and customised a nifty spinner like below. I kind of enjoy seeing it going 4-5 cycles before it disappears when the solution is displayed; but with Azure, I can hardly see it complete one cycle! (a bit disappointed 😋).

Please wait while getting the solutions...

100
25
5
4
7
9

AWS REST APIs offer fine-grained control such as Model, Mapping Template, which are missing in Azure Function. On the other hand, the way Azure uses Keys for the Function App and Function is very simple to use and effective.