-
Notifications
You must be signed in to change notification settings - Fork 7
Tutorial
Here is a tutorial to quickly create an AWS Lambda function using the AWS sdk and a local runner based on the EMG.Lambda.LocalRunner
package.
This tutorial assumes that
- You are confident into using the
dotnet
console tooling (in this tutorial, PowerShell will be used but no special commandlet will be used) - You have some kind of text editor that you can invoke from console (in the tutorial, Visual Studio Code will be used by using the command
code
) - You have some tool to launch HTTP requests, by console or with GUI.
If you haven't done it before, you need to install the templates contained in the Amazon.Lambda.Templates
You can do it by simply executing the following command in your favourite shell
> dotnet new -i "Amazon.Lambda.Templates::*"
Create a directory for your test project and navigate into it
> md MyTest
> cd MyTest
Let's create a Lambda function to use as target for our runner.
In the command prompt of your choice, use dotnet new
to create a project containing a simple Lambda project. We will call it MyTestLambda
.
> dotnet new lambda.EmptyFunction -n MyTestLambda
The template provided by Amazon will create the following directory structure
MyTestLambda
- src
- MyTestLambda
- aws-lambda-tools-defaults.json
- Function.cs
- MyTestLambda.csproj
- README.md
- test
- MyTestLambda.Tests
- FunctionTest.cs
- MyTestLambda.Tests.csproj
Once we have created the Lambda function, it's time to create the console application that will contain the local runner.
The first step is to create the console application. While staying in the MyTest
directory, execute the command
> dotnet new console -n MyLambdaRunner
Ths will create a new directory called MyLambdaRunner
within the MyTest
one.
Let's add the EMG.Lambda.LocalRunner
package.
> dotnet add .\MyLambdaRunner\MyLambdaRunner.csproj package EMG.Lambda.LocalRunner
...
info : PackageReference for package 'EMG.Lambda.LocalRunner' version '1.0.0' added to file 'C:\Development\Tests\MyTest\MyLambdaRunner\MyLambdaRunner.csproj'.
Now we need to add a reference to the project containing the Lambda function
> dotnet add .\MyLambdaRunner\MyLambdaRunner.csproj reference .\MyTestLambda\src\MyTestLambda\MyTestLambda.csproj
Reference `..\MyTestLambda\src\MyTestLambda\MyTestLambda.csproj` added to the project.
Enter the MyLambdaRunner
directory and open the Program.cs
file with your text editor of choice.
> cd .\MyLambdaRunner\
> code .\Program.cs
At the beginning of the file, add a using statement to import the namespace of the Lambda function
using MyTestLambda;
Then add a using statement to impor the EMG.Lambda.LocalRunner
namespace
using EMG.Lambda.LocalRunner;
In the body of the Main
method, replace
Console.WriteLine("Hello World!");
with the following snippet
LambdaRunner.Create()
.Receives<string>()
.Returns<string>()
.UsesFunction<Function>((function, input, context) => function.FunctionHandler(input, context))
.Build()
.RunAsync()
.Wait();
Save everything and leave the editor.
We're now ready to run our application. In the console, let's build and run the console application. Since we have a project reference to the project containing the function, the compiler will also compile it.
> dotnet run
Hosting environment: Production
Content root path: C:\MyTest\MyLambdaRunner\bin\Debug\netcoreapp2.0\
Now listening on: http://127.0.0.1:5000
Application started. Press Ctrl+C to shut down.
The Lambda function is now being hosted on the port 5000. Any HTTP request on that port will be routed to the handler.
Now that our Lambda is being hosted, it's time for us to test it.
If you are using a tool like Postman, Fiddler or similar GUI tools for creating HTTP requests, simply use this raw request
POST HTTP/1.1
Host: localhost:5000
"hello world"
If you prefer curl
in a Linux/MacOSX bash console,
$ curl -X POST http://localhost:5000 -d '"hello world"'