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:
- Create a new Lambda in the AWS Console
- Set the Function URL
Auth typetoNONE - Check
Configure cross-origin resource sharing (CORS) - Set the handler to
TestCalc - Set the
TestCalc.csprojvaluePublishReadyToRuntofalse - Publish the
TestCalcproject locally withdotnet publish -c Release --output=publish - Zip the contents of the
\TestCalc\publishfolder - Upload the
publish.zipfile 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