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
    //'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.

    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 = function(data, message) {

        if(sys.isObject(data)) {            

            if(!sys.isDefined(message)) message = "object";
            for(var p in data) {
                this.log(" .."+p + ":" + data[p]);
        else {
                console.log(message+" ~ "+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;;
    return l;

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

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

    this.handler = function(event, context) {

        $this._context = context;
            $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 ""
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 (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 ""
"[Fri Mar 11 2016 03:02:52 GMT+0000 (UTC)] Looks OK"