The following tutorial goes through the basic steps required to write and deploy a C# based AWS Lambda. I won't be going into the detail of what Lambdas are, this will focus on the more practical aspect of writing one and deploying it to AWS. But for some context: AWS Lambda is Amazon's serverless compute service that allows you to run code without provisioning or managing servers.
You will need to add the Amazon.Lambda.Core package to the project you are going to write your Lambda in.
The following acts as an example piece of code that will run in a Lambda, what it does isn't necessarily very important it's just being used to deomnstrate the key parts of the code required and then ultimately used in the deployment. As such it's just very simply logs out Hello World:
using Amazon.Lambda.Core;
namespace MyFirstLambda
{
public class MyFirstLambda
{
public void FunctionHandler(ILambdaContext context)
{
context.Logger.LogLine("Hello World");
}
}
}
The key part of the code here for the Lambda is the handler function - FunctionHandler. This is entry point for the Lambda. The signature for this function can vary, the one shown in the above example only passes in ILambdaContext which gives runtime information for the Lambda to access (request ID, memory limits, logger, etc.).
To deploy the Lambda we can use a CLI-based Amazon tool, that can be installed using:
dotnet tool install -g Amazon.Lambda.Tools
Then to deploy, navigate to the folder where your project file is located and run the following command (don't forget to dotnet build on the project first):
dotnet lambda deploy-function
Running this deployment tool, you will be asked the following questions:
After answering all the questions, the Lambda should be deployed and you can go and test in the AWS console.