Handler function

First, we'll look at the handler function, the entry point for all Lambda functions. You choose your handler when creating the function. When the function executes, the service calls the handler function and passes in some event data and useful objects.

Let's have a look at the syntax for some handlers in different languages:

  • Node.js:
exports.handler = function(event, context, callback) {
callback(null, "executed successfully");
}

The following is the fat-arrow syntax:

exports.handler = (event, context, callback) => {
callback(null, "executed successfully");
}
  • Python:
def handler(event, context):
return "executed successfully"

C# and Java are a little different compared to Node.js and Python.

First, you specify a return type. If you're invoking the Lambda synchronously (RequestResponse), then this will be the data type of the object or thing you are turning. If you're invoking the Lambda asynchronously (using the event type), then you don't expect any data to be returned, so you can use a void return type.

You also need to specify the type of the incoming event data, and it's usually a string.

  • The syntax for C# is as follows:
returnType handler-name(inputType input, ILambdaContext context){
return;
}

Because this is slightly more complex, let's look at an example that takes a string as input and returns the same string in upper case. This is an example of a RequestResponse invocation type:

public string Handler(string input, ILambdaContext context){
return input?.ToUpper();
}
  • The Java syntax is quite similar:
outputType handler-name(inputType input, Context context){
return;
}

Let's put this into an example for an Event type of invocation:

public void handler(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
}

Now, we'll look at context objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.18