Why Serverless is Not Just Hype, But a Lifesaver for Your Wallet and Nerves
\n\nImagine: you wrote a cool API in C#, deployed it on a virtual machine, and it sits idle for 23 hours a day. You're paying for nothing. Sound familiar? If so, then serverless functions in C# are your lifeline. Serverless computing (or Function-as-a-Service) allows you to run code only when it's actually needed. No server rental, no manual CI/CD setup for infrastructure, no scaling headaches.
\n\nIn this article, we'll explore how to write serverless functions in C# for AWS Lambda and Azure Functions, compare them, write real code, and understand why this is the best choice for microservices, event handlers, and mobile app backends. And yes, there will be plenty of code — no fluff.
\n\nAWS Lambda and C#: How to Make Two Powerful Tools Work Together
\n\nAWS Lambda C# is arguably the most popular option for .NET developers working with Amazon Web Services. Lambda supports .NET Core 3.1, .NET 6, .NET 8, and even .NET 9 (check for current support at the time of reading). The main advantage is that you write in familiar C#, and AWS handles the runtime itself.
\n\nCreating Your First Lambda Function in C#
\n\nFirst, install the AWS Toolkit for Visual Studio or use the .NET CLI. Let's create a project using the "AWS Lambda Project" template.
\n\ndotnet new lambda.EmptyFunction --name HelloWorldFunction\ncd HelloWorldFunction\ndotnet add package Amazon.Lambda.Core\ndotnet add package Amazon.Lambda.Serialization.SystemTextJson\n\nNow let's write the function itself. A classic example — processing an API Gateway request and returning JSON.
\n\nusing Amazon.Lambda.Core;\nusing Amazon.Lambda.Serialization.SystemTextJson;\n\n[assembly: LambdaSerializer(typeof(DefaultLambdaJsonSerializer))]\n\npublic class Function\n{\n public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest input, ILambdaContext context)\n {\n var message = $"Hello, world! Your request: {input.Body ?? "empty"}";\n context.Logger.LogInformation("Lambda function invoked");\n\n return new APIGatewayProxyResponse\n {\n StatusCode = 200,\n Body = JsonSerializer.Serialize(new { message, timestamp = DateTime.UtcNow }),\n Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }\n };\n }\n}\n\nNote: the code is asynchronous, uses built-in JSON serialization and logging. This is a minimum viable product that can already handle load.
\n\nWhy AWS Lambda Wins for C#?
\n- \n
- Cold start: AWS optimized .NET function startup with SnapStart — now startup takes less than 200 ms. \n
- Integration: Works out of the box with S3, DynamoDB, SQS, API Gateway. \n
- Monitoring: CloudWatch collects logs, metrics, and errors. \n
Azure Functions for C#: A Worthy Alternative from Microsoft
\n\nIf your project is already tied to the Microsoft ecosystem (Azure DevOps, Active Directory, SQL Server), then Azure Functions C# might be more convenient. They support both isolated process and in-process models.
\n\nExample of a C# Function for Azure
\n\nLet's create an HTTP trigger using Azure Functions Core Tools:
\n\nfunc init MyFunctionApp --dotnet-isolated\ncd MyFunctionApp\nfunc new --name HttpExample --template "HTTP trigger"\n\nFunction code:
\n\nusing Microsoft.Azure.Functions.Worker;\nusing Microsoft.Extensions.Logging;\n\npublic class HttpExample\n{\n private readonly ILogger<HttpExample> _logger;\n\n public HttpExample(ILogger<HttpExample> logger)\n {\n _logger