Create a Lambda Function
Now it's time to create your first Lambda function. You can use the dotnet new command to create a new application from a range of available templates. For this example, we will create an empty Lambda function.
The created folder structure contains the following files and folders:
MyFirstLambda└── src ├── MyFirstLambda ├── Function.cs ├── MyFirstLambda.csproj└── test ├── MyFirstLambda.Test ├── FunctionTest.cs
We will come to tests in a later section, let's first focus on the Lambda function code itself in Function.cs
. Open up the MyFirstLambda.csproj
in the IDE of your choice.
Lambda Functions in .NET
In it's simplest form, a Lambda function is made up of a single public method. This is the method that will be invoked by the Lambda service. It can be called whatever you want, call it banana if you like.
Typically, it's a good idea to standardize on something along the lines of Handler
or FunctionHandler
.
The Function Handler
The handler itself is a simple method call. It be sync or async, that's up to you. The important parts are the method parameters. A Lambda function handler can take up to 2 parameters.
The first is the payload. This is the event payload the Lambda service will pass into your function. In this example, we are passing in a simple string. But as you will see in later examples this can also be a POCO.
The second is the ILambdaContext
. This is an optional parameter, delete it if you wish. The ILambdaContext
object holds contextual information about this specific invoke. Things like the RequestId
, FunctionName
and the FunctionVersion
.
The Lambda Serializer
The .NET runtime for Lambda can automatically de/serialize Lambda requests and responses to objects and back again. Specifying this line at the top of your function tells Lambda how to perform this de/serialization. There is built in support for System.Text.Json
and Newtonsoft.Json
as well as the ability to provide your own custom serializer.
Lambda Functions in .NET
In it's simplest form, a Lambda function is made up of a single public method. This is the method that will be invoked by the Lambda service. It can be called whatever you want, call it banana if you like.
Typically, it's a good idea to standardize on something along the lines of Handler
or FunctionHandler
.
The Function Handler
The handler itself is a simple method call. It be sync or async, that's up to you. The important parts are the method parameters. A Lambda function handler can take up to 2 parameters.
The first is the payload. This is the event payload the Lambda service will pass into your function. In this example, we are passing in a simple string. But as you will see in later examples this can also be a POCO.
The second is the ILambdaContext
. This is an optional parameter, delete it if you wish. The ILambdaContext
object holds contextual information about this specific invoke. Things like the RequestId
, FunctionName
and the FunctionVersion
.
The Lambda Serializer
The .NET runtime for Lambda can automatically de/serialize Lambda requests and responses to objects and back again. Specifying this line at the top of your function tells Lambda how to perform this de/serialization. There is built in support for System.Text.Json
and Newtonsoft.Json
as well as the ability to provide your own custom serializer.
And that is all there is to your first Lambda function. Feeling good? Now let's go and deploy it to your AWS account.