Serverless in C#: AWS Lambda, Azure Functions and the Future of Serverless Development

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Serverless is Not Just Hype, But a Lifesaver for Your Wallet and Nerves

\n\n

Imagine: 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\n

In 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\n

AWS Lambda and C#: How to Make Two Powerful Tools Work Together

\n\n

AWS 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\n

Creating Your First Lambda Function in C#

\n\n

First, install the AWS Toolkit for Visual Studio or use the .NET CLI. Let's create a project using the "AWS Lambda Project" template.

\n\n
dotnet new lambda.EmptyFunction --name HelloWorldFunction\ncd HelloWorldFunction\ndotnet add package Amazon.Lambda.Core\ndotnet add package Amazon.Lambda.Serialization.SystemTextJson
\n\n

Now let's write the function itself. A classic example — processing an API Gateway request and returning JSON.

\n\n
using 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\n

Note: the code is asynchronous, uses built-in JSON serialization and logging. This is a minimum viable product that can already handle load.

\n\n

Why 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
\n\n

Azure Functions for C#: A Worthy Alternative from Microsoft

\n\n

If 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\n

Example of a C# Function for Azure

\n\n

Let's create an HTTP trigger using Azure Functions Core Tools:

\n\n
func init MyFunctionApp --dotnet-isolated\ncd MyFunctionApp\nfunc new --name HttpExample --template "HTTP trigger"
\n\n

Function code:

\n\n
using 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                

Blogs

Book Recommendations