Has anyone had any luck getting a .NET 6 REST API project to work in an AWS Lambda?

Viewed 265

I have a very simple ASP.NET 6.0 Web API application, with a Home controller with one Get method returning text:

[ApiController]
[Route("[controller]")]
public class HomeController : Controller
{
    // GET
    [HttpGet]
    public IActionResult Get()
    {
        return Ok(new { message = "Hello, World!" });
    }
}

I've been able to get ASP.NET projects < 6.0 to work, but with .NET 6 I'm running into issues. There's no longer a Startup class; that functionality moved to the implicit Program class. So in my LambdaEntryPoint class (which inherits from APIGatewayProxyFunction) I'm using Program as the Startup:

    protected override void Init(IWebHostBuilder builder)
    {
        builder.UseStartup<Program>();
    }

I'm getting an error when manually testing from the AWS console: Amazon.Lambda.RuntimeSupport.ExceptionHandling.LambdaValidationException: Unable to load assembly. I believe that my naming is correct: MyAssembly::MyAssembly.LambdaEntryPoint::FunctionHandlerAsync

The only thing different about my Program class is that I had to add public partial class Program { } at the bottom so the unit tests would could find Program and run.

My Test event looks like this:

{
  "resource": "/Home",
  "path": "/Home",
  "httpMethod": "GET",
  "isBase64Encoded": true
}

It is a cut down version of the default Amazon API Gateway AWS Proxy

2 Answers

The fact that you don't have a Startup class doesn't mean you can't make one, I made a Startup class for my API in .NET 6 and it works fine

You do not need to add a Startup.cs class. The only thing you have to do if you are working with NET6 is in your Program.cs file you have to add this line of code.

This is if you are using a rest API or API GATEWAY

builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi);

This is if you are using your lambda with a url

builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);

..and in aws you just have to change the handler with the name of your project and that's it. MyAssembly

See the example here in aws

Related