Running .NET on AWS Lambda: A Quick Start Guide

Amazon Web Services (AWS) Lambda is a serverless compute service that lets you run code without provisioning or managing servers. With support for .NET, you can build Lambda functions in C# and deploy them seamlessly. In this guide, you'll learn how to create, build, and deploy a .NET Lambda function using the AWS .NET tooling.

Prerequisites

Before you start, make sure you have:

Install the Lambda tooling with:

dotnet tool install -g Amazon.Lambda.Tools

Step 1: Create a Lambda Project

You can create a new Lambda project using one of the AWS templates:

dotnet new lambda.EmptyFunction --name MyLambdaFunction
cd MyLambdaFunction

The main function logic will be in Function.cs, under the method FunctionHandler.

Step 2: Write Your Function Logic

Edit Function.cs to match your logic. For example:

public class Function
{
    public string FunctionHandler(string input, ILambdaContext context)
    {
        return $"Hello, {input}!";
    }
}

Step 3: Test Locally

You can test the function locally with:

dotnet lambda invoke-function MyLambdaFunction --payload "World"

Step 4: Deploy to AWS

Deploy using the Lambda tools:

dotnet lambda deploy-function MyLambdaFunction

You’ll be prompted for details like the IAM role, region, and function name. You can also use --function-role, --region, and other flags to script deployments.


Step 5: Invoke from AWS Console or API

Once deployed, you can:

aws lambda invoke \
  --function-name MyLambdaFunction \
  --payload "\"John Doe\"" \
  output.json

Optional: Set Up API Gateway

To expose your Lambda via HTTP, connect it to API Gateway:

  • In the AWS Console, go to API Gateway.

  • Create a new HTTP API.

  • Add an integration with your Lambda.

  • Deploy the API and use the URL to invoke your Lambda.


Cleanup

To delete the Lambda and avoid charges:

aws lambda delete-function --function-name MyLambdaFunction

Conclusion

Running .NET on AWS Lambda is a great way to take advantage of serverless architecture while using C#. With a few CLI commands, you can deploy scalable functions to the cloud without managing infrastructure.

Need advanced patterns, like dependency injection, configuration, or async I/O? AWS .NET Lambda projects support all of them. Let me know if you want a follow-up post on those topics.

References

0
Subscribe to my newsletter

Read articles from Renato Ramos Nascimento directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Renato Ramos Nascimento
Renato Ramos Nascimento

With over 14 years in software development, I specialize in backend systems using .NET, Python, and Java. I bring full lifecycle expertise, including requirements analysis, client/server and data layer development, automated testing (unit, integration, end-to-end), and CI/CD implementations using Docker, GitLab Pipelines, GitHub Actions, Terraform, and AWS CodeStar.