Thursday, March 10, 2016

Amazon Lambda Function


What Is AWS Lambda? 



My quick definition: A little bit of JavaScript or Python that can be invoked via an URL and will run in the cloud. A quick NodeJS instance in the cloud. The buzz word is MicroService. Here is the definition from amazon page.

I guess the equivalent on Azure is the new Azure Function, more on that later.

Other functionalities

With AWS Lambda you are allowed to
  • Inlude npm package
  • Execute binary file
AWS Lambda FAQ page


Code Structure

This is hello world lambda example. In the end a Lambda Function is a JavaScript function, an event handler running in a NodeJs environment.
The event object contains the input parameters. The context object allows to return a result.
console.log('Loading function');

exports.handler = function(event, context) {

    console.log('value1 =', event.key1);
    console.log('value2 =', event.key2);
    console.log('value3 =', event.key3);
    context.succeed(event.key1);  // Echo back the first key value
    // context.fail('Something went wrong');
};

Here is my attempt to write a Lambda function and build some re-usable code.
See fjs.lib to access the library Sys and String.  The file Sys.js and String.js were written to be compatible with nodejs or a browser.
The file Sys.js and String.js are located in the sub folder node_modules to be accessible by the lambda script.
require("Sys");
require("String");

/*
    Re usable code for next Lambda Function.
 */
var LambdaFuncBaseClass = function() {

    this.send = function(succeeded, data) {

        var d = new Date();
        var m = "[{0}, {1}]{2}".format(succeeded ? "PASSED":"FAILED", d, data);
        this.log(m);
        if(succeeded)
            this._context.succeed(m);
        else 
            this._context.fail(m);
    }
    this.log = function(data, message) {

        if(sys.isObject(data)) {            

            if(!sys.isDefined(message)) message = "object";
            console.log(message);
            for(var p in data) {
                this.log(" .."+p + ":" + data[p]);
            }
        }
        else {
            if(sys.isDefined(message))
                console.log(message+" ~ "+data);
            else
                console.log(data);
        }
    }
    this.__init__ = function() {

        console.log('Init lambda function:' + this.Name);
    }
}
/*
    A static member to initialize my customized Lambda Function
 */
LambdaFuncBaseClass.create = function(name, functionType) {

    var l            = new functionType(name);
    exports.handler  = l.handler;
    LambdaFuncBaseClass.call(l);
    l.__init__();
    return l;
}

/*
    My new Lambda Function
 */
var MyLambdaFunc = function(name) {

    var $this  = this;
    $this.Name = name;

    this.handler = function(event, context) {

        $this._context = context;
        try{
            $this.log(event, "Input parameters");
            $this.send(true, "Looks OK. ErrorCode:{0}".format(0));
        }
        catch(ex) {
            $this.send(false, "exception "+ex);
        }
    }
}
var myLambdaFunc = LambdaFuncBaseClass.create("myLambdaFunc", MyLambdaFunc);



How to call the Lambda Function from CURL.exe

C:\Tools\PortableGit\bin>curl -i -X GET "https://m956zj9ecj.execute-api.us-west-2.amazonaws.com/prod/FHelloWorld?phoneNumber=19787606031&message=HiMan"
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 52
Connection: keep-alive
Date: Fri, 11 Mar 2016 03:02:48 GMT
x-amzn-RequestId: bc86ed04-e735-11e5-b279-317fad6da791
X-Cache: Miss from cloudfront
Via: 1.1 2d480f42c6a626fc8d8c8aaaf1ec043d.cloudfront.net (CloudFront)
X-Amz-Cf-Id: kk-pRWjjHxOI7OJUEBvaQLC-7o389kuO1Zb4ByjyJOodKjZSpxNbvw==
"[Fri Mar 11 2016 03:02:48 GMT+0000 (UTC)] Looks OK"


C:\Tools\PortableGit\bin>curl  -X GET "https://m956zj9ecj.execute-api.us-west-2.amazonaws.com/prod/FHelloWorld?phoneNumber=19787606031&message=HiMan"
"[Fri Mar 11 2016 03:02:52 GMT+0000 (UTC)] Looks OK"



1 comment: