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.
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:
- event: Lambda uses this parameter to pass any event related data back to the handler.
- context: Lambda again uses this parameter to provide the handler with the function's runtime information such as the name of the function, the time it took to execute, and so on.
- callback: This parameter is used to return any data back to its caller. The callback parameter is the only optional parameter that gets passed when writing handlers. If not specified, AWS Lambda will call it implicitly and return the value as null. The callback parameter also supports two optional parameters in the form of error and result; error will return any of the function's error information back to the caller, while result will return any result of your function's successful execution.
Here are a few simple examples of invoking callbacks in your handler:
- callback()
- callback(null, 'Hello from Lambda')
- callback(error)
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:
- Login to the AWS Management Console and select AWS Lambda from the dashboard.
- Select the Create a Lambda function option as we did in our earlier chapter.
- From the Select blueprint page, select the Blank Function blueprint.
- Since we are not configuring any triggers for now, simply click on Next at the Configure triggers page.
- Provide a suitable Name and Description for your Lambda function and paste the preceding code into the inline code editor as shown:

- 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:

- In the Review page, select the Create function option.
- With your function now created, select the Test option to pass the sample event to our function.
- 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:
