dotnet2 minimal Lambda throws 200 with Object reference not set error

Viewed 27

I am attempting to setup a simple Lambda frontend with an API Gateway, but I am constantly faced with this error, regardless of how I configure the gateway or initiate requests. Here are my steps:

  1. Scaffold calculator project with dotnet 6 minimal Lambda API: dotnet new serverless.AspNetCoreMinimalAPI -n DemoCalc
  2. Open Program.cs and change LambdaEventSource.RestApi to LambdaEventSource.HttpApi
  3. Hit F5 and test the Lambda locally in Postman by hitting localhost:5001 and seeing Welcome to running ASP.NET Core Minimal API on AWS Lambda in the response body
  4. Create a new Lambda function in the AWS Console with only these properties deviating from the defaults:
Function name: DemoCalc
Runtime: .NET 6
Advanced settings > Enable function URL: Checked
Advanced settings > Auth type: NONE
  1. In the Lambda console, under Runtime settings, click Edit and change the Handler value to DemoCalc and click Save
  2. Add a trigger with these values:
Source: API Gateway
Create a new API: Checked
API type: REST API
Security: Open
  1. Deploy the Lambda project to AWS by navigating into the generated \DemoCalc\src\DemoCalc folder and executing dotnet lambda deploy-function DemoCalc -frun dotnet6
  2. At the top of the Lambda console, copy the Function URL value and test in Postman, seeing Welcome to running ASP.NET Core Minimal API on AWS Lambda in the response body
  3. Open the API Gateway console, click on DemoCalc, select the ANY resource and click on TEST
  4. Select Method: GET and click Test

The following is displayed in the test output and CloudWatch log groups:

Endpoint response body before transformations: {
  "errorType": "NullReferenceException",
  "errorMessage": "Object reference not set to an instance of an object.",
  "stackTrace": [
    "at Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction.MarshallRequest(InvokeFeatures features, APIGatewayHttpApiV2ProxyRequest 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)"
  ]
}

Lambda execution failed with status 200 due to customer function error: Object reference not set to an instance of an object.. Lambda request id: 065208ae-f219-4920-a450-f0474f27b474

Method completed with status: 502

What am I missing to get this working via API Gateway?

EDIT: An alternative method of testing the Lambda is:

  1. In step #2 leave this code: LambdaEventSource.RestApi
  2. Deploy as in step #5 dotnet lambda deploy-function DemoCalc -frun dotnet6
  3. In the Lambda console click on the Test tab
  4. Change the Event JSON to:
{
  "path": "/",
  "httpMethod": "GET"
}
  1. Click the Test button and the Execution results above will show:
{
  "statusCode": 200,
  "headers": {},
  "multiValueHeaders": {
    "Content-Type": [
      "application/json; charset=utf-8"
    ]
  },
  "body": "15",
  "isBase64Encoded": false
}

However, I still cannot test the Lambda via API Gateway with Postman as all I get is 403 Forbidden, even though both the Lambda test and API Gateway resource test consoles both return good results.

0 Answers
Related