0

My local dotnet version is 6.0.400 and dotnet lambda --help shows version 5.4.5.

I have scaffolded a test lambda project with dotnet new serverless.AspNetCoreMinimalAPI -n TestCalc.

If I load this in VSCode and hit F5 it runs just fine; Giving me the classic localhost:5000/calculator responses.

If I then:

  1. Create a new Lambda in the AWS Console
  2. Set the Function URL Auth type to NONE
  3. Check Configure cross-origin resource sharing (CORS)
  4. Set the handler to TestCalc
  5. Set the TestCalc.csproj value PublishReadyToRun to false
  6. Publish the TestCalc project locally with dotnet publish -c Release --output=publish
  7. Zip the contents of the \TestCalc\publish folder
  8. Upload the publish.zip file to the Lambda

And hit the function URL, I get this error logged to CloudWatch:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction.MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
   at Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction`2.FunctionHandlerAsync(TREQUEST request, ILambdaContext lambdaContext)
   at Amazon.Lambda.RuntimeSupport.HandlerWrapper.<>c__DisplayClass26_0`2.<<GetHandlerWrapper>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InvokeOnceAsync(CancellationToken cancellationToken)

It appears (to me) that the function is being invoked but the AWS Lambda library is failing to receive something it expects.

How can I give it what it needs?

The generated code (plus some logging from me) is this:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();

// Add AWS Lambda support. When application is run in Lambda Kestrel is swapped out as the web server with Amazon.Lambda.AspNetCoreServer. This
// package will act as the webserver translating request and responses between the Lambda event source and ASP.NET Core.
builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi);

var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.MapGet("/", () => "Welcome to running ASP.NET Core Minimal API on AWS Lambda");

app.Logger.LogInformation($"before app.Run()"); // appears in CloudWatch
app.Run();
app.Logger.LogInformation($"after app.Run()"); // does NOT appear in CloudWatch
Matt W
  • 11,753
  • 25
  • 118
  • 215

1 Answers1

0

After finding this github post I figured I should check the options on the LambdaEventSource and switched it to LambdaEventSource.HttpApi. This solved the problem.

Matt W
  • 11,753
  • 25
  • 118
  • 215