C# and AWS Lambdas, Part 1 - Hello World
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
Install the AWS Lambda Templates.
Now you can create projects based on these templates.
Here is the full list of available templates.
For this example, we are going to use
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
The code of the Lambda can stay the same, it takes an input string and converts it to uppercase, and returns a string.
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
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.
- C# and AWS Lambdas, Part 5 – Updating the Zip in S3 and Updating the Running Lambda, with Pulumi IaC
- C# and AWS Lambdas, Part 4 – Storing the Zip in S3, Setup with Pulumi IaC
- C# and AWS Lambdas, Part 3 – Pulumi IaC for Web API and an API Gateway
- C# and AWS Lambdas, Part 2 - Web API and an API Gateway
- Using Step Functions and C# Lambdas to Orchestrate API Calls