# Deploying .NET APIs to AWS Lambda with API Gateway

Serverless doesn't mean giving up full-featured APIs. With AWS Lambda and API Gateway, you can run .NET Web APIs in a fully managed, scalable way without provisioning servers. In this post, we'll walk through deploying a minimal .NET API to AWS Lambda using API Gateway as the front door.

---

## Prerequisites

* [.NET 6 or later SDK](https://dotnet.microsoft.com/download)
    
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) (configured with credentials)
    
* [Amazon.Lambda.Tools CLI](https://github.com/aws/aws-extensions-for-dotnet-cli)
    

Install the AWS Lambda tools globally if you haven’t already:

```bash
dotnet tool install -g Amazon.Lambda.Tools
```

## Step 1: Create a Minimal API

We'll start with a simple .NET 6+ Web API using Minimal APIs:

```bash
dotnet new webapi -n MyLambdaApi
cd MyLambdaApi
```

(Optional) Clean up the template a bit and define your own minimal endpoint in `Program.cs`:

```csharp
var app = WebApplication.Create(args);
app.MapGet("/hello", () => "Hello from Lambda!");
app.Run();
```

## Step 2: Add Lambda Support

Install the necessary NuGet packages:

```bash
dotnet add package Amazon.Lambda.AspNetCoreServer
dotnet add package Amazon.Lambda.RuntimeSupport
```

Then, add a new class `LambdaEntryPoint.cs`:

```csharp
public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{
    protected override void Init(IWebHostBuilder builder)
    {
        builder.UseStartup<Startup>();
    }
}
```

Make sure `Startup.cs` exists and configures your services like normal.

---

## Step 3: Add Lambda Project Configuration

Create a `aws-lambda-tools-defaults.json` file in the project root:

```json
{
  "profile": "default",
  "region": "us-east-1",
  "configuration": "Release",
  "framework": "net6.0",
  "function-runtime": "dotnet6",
  "function-handler": "MyLambdaApi::MyLambdaApi.LambdaEntryPoint::FunctionHandlerAsync",
  "function-memory-size": 512,
  "function-timeout": 30,
  "function-name": "MyLambdaApi",
  "function-role": "arn:aws:iam::123456789012:role/your-lambda-role"
}
```

---

## Step 4: Deploy to AWS Lambda

Build and deploy using the Lambda tools:

```bash
dotnet lambda deploy-serverless
```

This command packages your app, creates a Lambda function, and wires it to an API Gateway endpoint.

After deployment, it will print a URL like:

```bash
https://xyz123.execute-api.us-east-1.amazonaws.com/Prod/hello
```

You can test it using `curl` or a browser.

---

## Step 5: Test the Endpoint

```bash
curl https://xyz123.execute-api.us-east-1.amazonaws.com/Prod/hello
# → Hello from Lambda!
```

## Cleanup

To avoid charges:

```bash
aws cloudformation delete-stack --stack-name MyLambdaApi
```

---

## Summary

You’ve just deployed a real .NET Web API to AWS Lambda using API Gateway with no servers in sight. This pattern is perfect for lightweight APIs, backend services, and microservices.

Stay tuned for the next post in the CloudSharp series, where we'll wire this API to a DynamoDB backend.

### References

* [AWS Lambda for .NET](https://docs.aws.amazon.com/lambda/latest/dg/dotnet.html)
    
* [Amazon.Lambda.Tools GitHub Repo](https://github.com/aws/aws-extensions-for-dotnet-cli)
    
* [Deploying .NET Lambda Functions](https://docs.aws.amazon.com/lambda/latest/dg/csharp-deployment-package.html)
    
* [ASP.NET Core Minimal APIs](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis)
    
* [API Gateway and Lambda Integration](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-getting-started-with-rest-apis.html)
