Introduction to Serverless and AWS Lambda in C#
Serverless architecture (serverless computing) is a development model where the developer focuses exclusively on business logic code, while the cloud provider manages the infrastructure (servers, scaling, monitoring). AWS Lambda is one of the most popular services for implementing serverless functions, and it has excellent support for the .NET ecosystem.
In this article, we will cover the complete lifecycle of working with AWS Lambda in C#: from project creation to deployment and debugging. You will learn how to set up the environment, write an efficient function, handle events, and avoid common mistakes. The material is aimed at C# developers who want to master the serverless approach in Amazon Web Services.
1. Environment Setup and Creating Your First Project
Installing Tools
To work with AWS Lambda in C#, you will need:
- .NET SDK version 6.0 or 8.0 (latest LTS version recommended).
- AWS CLI — command line interface for managing AWS services.
- Amazon.Lambda.Templates — a set of templates for .NET CLI.
- Docker Desktop (optional) — for local testing with Lambda emulation.
Install the Lambda templates via the terminal:
dotnet new -i Amazon.Lambda.TemplatesCreating a Project
Create a new Lambda function using the command:
dotnet new lambda.EmptyFunction -n MyFirstLambda -o ./MyFirstLambdacd MyFirstLambdaThe project structure includes a Function.cs file with a base Function class and a handler method. By default, a template for a simple function that accepts a string and returns a string is used.
Open Function.cs and modify it as follows:
using Amazon.Lambda.Core;using Amazon.Lambda.Serialization.SystemTextJson;
[assembly: LambdaSerializer(typeof(DefaultLambdaJsonSerializer))]
public class Function{ public string FunctionHandler(string input, ILambdaContext context) { context.Logger.LogInformation($"Received input: {input}"); return $"Hello, {input}! Your function executed successfully."; }}Local Testing
To run the function locally, use the dotnet lambda invoke utility or the Lambda Runtime Interface Emulator (RIE) in Docker. Example with RIE:
docker run --rm -v "$PWD/bin/Release/net8.0/publish":/var/task:ro,delegated \ -p 9000:8080 \ public.ecr.aws/lambda/dotnet8:latest \ MyFirstLambda::MyFirstLambda.Function::FunctionHandlerSend a test request via curl:
curl -X POST "http://localhost:9000/2015-03-31/functions/function/invocations" \ -d '"World"'2. Handling Different Types of Events
Working with API Gateway (HTTP Requests)
Most often, Lambda is used as a backend for REST APIs via Amazon API Gateway. In this case, the handler accepts an APIGatewayProxyRequest object and returns an APIGatewayProxyResponse.
Install the NuGet package:
dotnet add package Amazon.Lambda.APIGatewayEventsExample of an HTTP POST request handler function:
using Amazon.Lambda.APIGatewayEvents;using System.Text.Json;
public class Function{ public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { context.Logger.LogInformation($"Received request: {request.HttpMethod} {request.Path}");
// Parse request body var body = JsonSerializer.Deserialize<Dictionary<string, string>>(request.Body); var name = body?.GetValueOrDefault("name") ?? "Guest";
//