Handler

As discussed briefly in the earlier chapter, the handler is basically a function that Lambda calls first for execution. A handler function is capable of processing incoming event data that is passed to it as well as invoking other functions or methods from your code.

In this book, we will be concentrating a lot of our code and development on Node.js; however, the programming model remains more or less the same for the other supported languages as well.

A skeleton structure of a handler function is shown as follows:

exports.myHandler = function(event, context, callback) {    
  // Your code goes here. 
  callback(); 
} 

Here, myHandler is the name of your handler function. By exporting it, we make sure that Lambda knows which function it has to invoke first. The other parameters that are passed with the handler function are:

Here are a few simple examples of invoking callbacks in your handler:

Callback is supported only in Node.js runtime v4.3. You will have to use the context methods in case your code supports earlier Node.js runtime (v0.10.42)

Let us try out a simple handler example with a code:

exports.myHandler = function(event, context, callback) { 
  console.log("value = " + event.key); 
  console.log("functionName = ", context.functionName);   
  callback(null, "Yippee! Something worked!"); 
}; 

The preceding code snippet will print the value of an event (key) that we will pass to the function, print the function's name as part of the context object, and finally print the success message Yippee! Something worked! if all goes well!

Follow the given steps to create a Lambda function:

  1. Login to the AWS Management Console and select AWS Lambda from the dashboard.
  2. Select the Create a Lambda function option as we did in our earlier chapter.
  3. From the Select blueprint page, select the Blank Function blueprint.
  4. Since we are not configuring any triggers for now, simply click on Next at the Configure triggers page.

 

  1. Provide a suitable Name and Description for your Lambda function and paste the preceding code into the inline code editor as shown:
  1. Next, in the Lambda function handler and role section on the same page, type in the correct name of your Handler* as shown. The handler name should match with the handler name in your function to work. Remember also to select the basic-lambda-role for your function's execution before selecting the Next button:
 
  1. In the Review page, select the Create function option.
  2. With your function now created, select the Test option to pass the sample event to our function.

 

  1. In the Sample event section, pass the following event, and select the Save and test option:
        {
          "key": "My Printed Value!!"
        }

With your code execution completed, you should get a similar execution result as shown as follows. The important things to note here are the values for the event, context, and callback parameters. You can note the callback message being returned back to the caller as the function executed successfully. The other event and context object values are printed in the Log output section as highlighted:

In case you end up with any errors, make sure the handler function name matches the handler name that you passed during the function's configuration.