AWS Lambda Function Handler in C#

When you create a Lambda function, you specify a handler that AWS Lambda can invoke
when the service executes the
function on your behalf.

You define a Lambda function handler as an instance or static method in a class. If
you want access to the Lambda
context object, it is available by defining a method parameter of type ILambdaContext, an
interface you can use to access information about the current execution, such as the
name of the current function,
the memory limit, execution time remaining, and logging.

inputType – The first handler parameter is the input to the handler, which can
be event data (published by an event source) or custom input that you provide such
as a string or any custom
data object.

returnType – If you plan to invoke the Lambda function synchronously (using
the RequestResponse invocation type), you can return the output of your function using any of the
supported data types. For example, if you use a Lambda function as a mobile application
backend, you are invoking
it synchronously. Your output data type will be serialized into JSON.

If you plan to invoke the Lambda function asynchronously (using the Event invocation type), the
returnType should be void. For example, if you use AWS Lambda with event sources such
as Amazon S3 or Amazon SNS, these event sources invoke the Lambda function using the
Event invocation
type.

Handling Streams

Only the System.IO.Stream type is supported as an input parameter by default.

In the example C# code, the first handler parameter is the input to the handler (MyHandler),
which can be
event data (published by an event source such as Amazon S3) or custom input you provide
such as a Stream
(as in this example) or any custom data object. The output is of type Stream.

Handling Standard Data Types

All other types, as listed below, require you to specify a serializer.

Unless your function input and output parameters are of type System.IO.Stream, you will need to
serialize them. AWS Lambda provides a default serializer that can be applied at the
assembly or method level of
your application, or you can define your own by implementing the ILambdaSerializer interface provided
by the Amazon.Lambda.Core library. For more information, see AWS Lambda Deployment Package in C#.

To add the default serializer attribute to a method, first add a dependency on
Amazon.Lambda.Serialization.Json in your project.json file.

Handler Signatures

When creating Lambda functions, you have to provide a handler string that tells AWS
Lambda where to look for the
code to invoke. In C#, the format is:

ASSEMBLY::TYPE::METHOD where:

ASSEMBLY is the name of the .NET assembly file for your application. When using
the .NET Core CLI to build your application, if you haven't set the assembly name
using the
buildOptions.outputName setting in project.json, the ASSEMBLY name
will be the name of the folder that contains your project.json file. For more information,
see .NET Core CLI. In this case, let's assume the folder name is HelloWorldApp.

TYPE is the full name of the handler type, which consists of the
Namespace and the ClassName. In this case
Example.Hello.

METHOD is name of the function handler, in this case
MyHandler.

Ultimately, the signature will be of this format:
Assembly::Namespace.ClassName::MethodName

If the method specified in your handler string is overloaded, you must provide the
exact signature of the
method Lambda should invoke. AWS Lambda will reject an otherwise valid signature if
the resolution would require
selecting among multiple (overloaded) signatures.

Lambda Function Handler Restrictions

Note that there are some restrictions on the handler signature

It may not be unsafe and use pointer types in the handler signature, though
unsafe context can be used inside the handler method and its dependencies. For more
information, see unsafe (C#
Reference).

It may not pass a variable number of parameters using the params keyword, or use
ArgIterator as an input or return parameter which is used to support variable number of
parameters.

The handler may not be a generic method (e.g. IList<T> Sort<T>(IList<T> input)).

Async handlers with signature async void are not supported.

Using Async in C# Functions with AWS Lambda

If you know your Lambda function will require a long-running process, such as uploading
large files to Amazon S3 or
reading a large stream of records from DynamoDB, you can take advantage of the async/await
pattern. By creating a
handler with this signature, Lambda will execute the function synchronously and wait
a maximum of 5 minutes for
execution to complete before returning or timing out. For example:

If you use this pattern, there are some considerations you must take into account:

AWS Lambda will not support async void methods.

If you create an async Lambda function without implementing the await operator, .NET will
issue a compiler warning and you will observe unexpected behavior. For example, some
async actions will
execute while others won't. Or some async actions won't complete before the function
execution is
complete.

Your Lambda function can include multiple async calls, which can be invoked in parallel.
You can use the
Task.WhenAll and Task.WhenAny methods to work with multiple tasks. To use the
Task.WhenAll method, you pass a list of the operations as an array to the method. Note that in
the example below, if you neglect to include any operation to the array, that call
may return before its
operation completes.