Full source code available here.
This is the first in a series of posts on using .NET with AWS Lambdas. It will start with the simplest example that converts a lowercase string to an uppercase string, but by the end you will be running a .NET Web API powered by lambda, fronted by an API gateway where all the infrastructure is setup by Pulumi – this will take a few posts over the next while.
Getting Started
Install the AWS Lambda Templates.
dotnet new -i Amazon.Lambda.Templates
Now you can create projects based on these templates.
Here is the full list of available templates.
Templates Short Name Language Tags ---------------------------------------------------- -------------------------------------------- ------------ ---------------------- Order Flowers Chatbot Tutorial lambda.OrderFlowersChatbot [C#] AWS/Lambda/Function Lambda Custom Runtime Function (.NET 5.0) lambda.CustomRuntimeFunction [C#], F# AWS/Lambda/Function Lambda Detect Image Labels lambda.DetectImageLabels [C#], F# AWS/Lambda/Function Lambda Empty Function lambda.EmptyFunction [C#], F# AWS/Lambda/Function Lex Book Trip Sample lambda.LexBookTripSample [C#] AWS/Lambda/Function Lambda Simple Application Load Balancer Function lambda.SimpleApplicationLoadBalancerFunction [C#] AWS/Lambda/Function Lambda Simple DynamoDB Function lambda.DynamoDB [C#], F# AWS/Lambda/Function Lambda Simple Kinesis Firehose Function lambda.KinesisFirehose [C#] AWS/Lambda/Function Lambda Simple Kinesis Function lambda.Kinesis [C#], F# AWS/Lambda/Function Lambda Simple S3 Function lambda.S3 [C#], F# AWS/Lambda/Function Lambda Simple SNS Function lambda.SNS [C#] AWS/Lambda/Function Lambda Simple SQS Function lambda.SQS [C#] AWS/Lambda/Function Lambda ASP.NET Core Web API serverless.AspNetCoreWebAPI [C#], F# AWS/Lambda/Serverless Lambda ASP.NET Core Web Application with Razor Pages serverless.AspNetCoreWebApp [C#] AWS/Lambda/Serverless Serverless Detect Image Labels serverless.DetectImageLabels [C#], F# AWS/Lambda/Serverless Lambda DynamoDB Blog API serverless.DynamoDBBlogAPI [C#] AWS/Lambda/Serverless Lambda Empty Serverless serverless.EmptyServerless [C#], F# AWS/Lambda/Serverless Lambda Giraffe Web App serverless.Giraffe F# AWS/Lambda/Serverless Serverless Simple S3 Function serverless.S3 [C#], F# AWS/Lambda/Serverless Step Functions Hello World serverless.StepFunctionsHelloWorld [C#], F# AWS/Lambda/Serverless Serverless WebSocket API serverless.WebSocketAPI [C#] AWS/Lambda/Serverless
The Application
For this example we are going to use lambda.EmptyFunction
.
dotnet new lambda.EmptyFunction --name HelloWorldLambda
This sets up two new projects, one for the lambda, and one for the tests.
There seems to be a bug in the template as it is missing line 6, this is very important. Without it, you will get errors like – An assembly specified in the application dependencies manifest (HelloWorldLambda.deps.json) was not found: package: ‘Amazon.Lambda.Core’, version: ‘1.1.0’
Be sure to add
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
to HelloWorldLambda.csproj
.
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> <AWSProjectType>Lambda</AWSProjectType> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> </PropertyGroup> <ItemGroup> <PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" /> <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.0.1" /> </ItemGroup> </Project>
The code of the lambda can stay the same, it takes an input string and converts it to uppercase and returns a string.
using Amazon.Lambda.Core; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace HelloWorldLambda { public class Function { /// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public string FunctionHandler(string input, ILambdaContext context) { return input?.ToUpper(); } } }
Build this, and zip up the all the files in the bin/debug/netcoreapp3.1
directory. This zip will be uploaded to the AWS soon.
The Lambda Function
For this post I’m going to show how to create the lambda using the AWS UI. In later post I’ll create it with Pulumi.
Open the AWS Lambada page – https://console.aws.amazon.com/lambda.
Click Create Function.
Image may be NSFW.
Clik here to view.
Give the function a name – HelloWorld is a good choice.
Change the runtime to .NET Core 3.1 (C#/PowerShell).
Image may be NSFW.
Clik here to view.
Click Create Function in the bottom right (no shown in the image above).
Upload the zip created above by clicking Action in the Function Code section of the screen.
Image may be NSFW.
Clik here to view.
Edit the Handler to read
HelloWorldLambda::HelloWorldLambda.Function::FunctionHandler
This matches the namespace in the application we created.
Image may be NSFW.
Clik here to view.
Open the test tool in the top right of the screen and click “Configure test events”.
Set the event name, and replace the body with this “hello world”.
Image may be NSFW.
Clik here to view.
That’s everything setup. Let’s run it.
Running the Lambda
Hit the “Test” button in the top right.
You should see something like – “Execution result: succeeded(logs)”. Expand the Details and you will see “HELLO WORLD” and bunch of other information about the execution of the lambda.
Image may be NSFW.
Clik here to view.
That’s it, a C# .NET Core 3.1 Hello World lambda up and running.
In the next post I’ll do something a little more interesting, I’ll put an API Gateway in front of a lambda, so the lambda can be called from the web.
Full source code available here.