C# and AWS Lambdas, Part 1 - Hello World
Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.
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 set up by Pulumi - this will take a few posts over the next while.
- Part 1 - Hello World
- Part 2 - Web API and an API Gateway
- Part 3 - Pulumi IaC for Web API and an API Gateway
- Part 4 - Storing the Zip in S3, Setup with Pulumi IaC
- Part 5 - Updating the Zip in S3 and Updating the Running Lambda, with Pulumi IaC
- Part 6 - .NET 5 inside a Container inside a Lambda
- Part 7 - .NET 5 Web API inside a Container inside a Lambda, with API Gateway in front
- Part 8 - .NET 6, inside a Container, inside a Lambda
Getting Started
Install the AWS Lambda Templates.
1dotnet new -i Amazon.Lambda.Templates
Now you can create projects based on these templates.
Here is the full list of available templates.
1Templates Short Name Language Tags
2---------------------------------------------------- -------------------------------------------- ------------ ----------------------
3Order Flowers Chatbot Tutorial lambda.OrderFlowersChatbot [C#] AWS/Lambda/Function
4Lambda Custom Runtime Function (.NET 5.0) lambda.CustomRuntimeFunction [C#], F# AWS/Lambda/Function
5Lambda Detect Image Labels lambda.DetectImageLabels [C#], F# AWS/Lambda/Function
6Lambda Empty Function lambda.EmptyFunction [C#], F# AWS/Lambda/Function
7Lex Book Trip Sample lambda.LexBookTripSample [C#] AWS/Lambda/Function
8Lambda Simple Application Load Balancer Function lambda.SimpleApplicationLoadBalancerFunction [C#] AWS/Lambda/Function
9Lambda Simple DynamoDB Function lambda.DynamoDB [C#], F# AWS/Lambda/Function
10Lambda Simple Kinesis Firehose Function lambda.KinesisFirehose [C#] AWS/Lambda/Function
11Lambda Simple Kinesis Function lambda.Kinesis [C#], F# AWS/Lambda/Function
12Lambda Simple S3 Function lambda.S3 [C#], F# AWS/Lambda/Function
13Lambda Simple SNS Function lambda.SNS [C#] AWS/Lambda/Function
14Lambda Simple SQS Function lambda.SQS [C#] AWS/Lambda/Function
15Lambda ASP.NET Core Web API serverless.AspNetCoreWebAPI [C#], F# AWS/Lambda/Serverless
16Lambda ASP.NET Core Web Application with Razor Pages serverless.AspNetCoreWebApp [C#] AWS/Lambda/Serverless
17Serverless Detect Image Labels serverless.DetectImageLabels [C#], F# AWS/Lambda/Serverless
18Lambda DynamoDB Blog API serverless.DynamoDBBlogAPI [C#] AWS/Lambda/Serverless
19Lambda Empty Serverless serverless.EmptyServerless [C#], F# AWS/Lambda/Serverless
20Lambda Giraffe Web App serverless.Giraffe F# AWS/Lambda/Serverless
21Serverless Simple S3 Function serverless.S3 [C#], F# AWS/Lambda/Serverless
22Step Functions Hello World serverless.StepFunctionsHelloWorld [C#], F# AWS/Lambda/Serverless
23Serverless WebSocket API serverless.WebSocketAPI [C#] AWS/Lambda/Serverless
The Application
For this example, we are going to use lambda.EmptyFunction
.
1dotnet 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
.
1<Project Sdk="Microsoft.NET.Sdk">
2 <PropertyGroup>
3 <TargetFramework>netcoreapp3.1</TargetFramework>
4 <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
5 <AWSProjectType>Lambda</AWSProjectType>
6 <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
7 </PropertyGroup>
8 <ItemGroup>
9 <PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
10 <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.0.1" />
11 </ItemGroup>
12</Project>
The code of the Lambda can stay the same, it takes an input string and converts it to uppercase, and returns a string.
1using Amazon.Lambda.Core;
2
3// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
4[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
5
6namespace HelloWorldLambda
7{
8 public class Function
9 {
10
11 /// <summary>
12 /// A simple function that takes a string and does a ToUpper
13 /// </summary>
14 /// <param name="input"></param>
15 /// <param name="context"></param>
16 /// <returns></returns>
17 public string FunctionHandler(string input, ILambdaContext context)
18 {
19 return input?.ToUpper();
20 }
21 }
22}
Build this, and zip up 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 a later post, I’ll create it with Pulumi.
Open the AWS Lambada page - https://console.aws.amazon.com/lambda.
Click Create Function.
Give the function a name - HelloWorld is a good choice. Change the runtime to .NET Core 3.1 (C#/PowerShell).
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.
Edit the Handler to read
HelloWorldLambda::HelloWorldLambda.Function::FunctionHandler
This matches the namespace in the application we created.
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”.
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 a bunch of other information about the execution of the Lambda.
That’s it, a C# .NET Core 3.1 Hello World Lambda is 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.