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:
- Scaffold calculator project with dotnet 6 minimal Lambda API:
dotnet new serverless.AspNetCoreMinimalAPI -n DemoCalc - Open
Program.csand changeLambdaEventSource.RestApitoLambdaEventSource.HttpApi - Hit
F5and test the Lambda locally in Postman by hittinglocalhost:5001and seeingWelcome to running ASP.NET Core Minimal API on AWS Lambdain the response body - 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
- In the Lambda console, under Runtime settings, click Edit and change the Handler value to
DemoCalcand click Save - Add a trigger with these values:
Source: API Gateway
Create a new API: Checked
API type: REST API
Security: Open
- Deploy the Lambda project to AWS by navigating into the generated
\DemoCalc\src\DemoCalcfolder and executingdotnet lambda deploy-function DemoCalc -frun dotnet6 - At the top of the Lambda console, copy the
Function URLvalue and test in Postman, seeingWelcome to running ASP.NET Core Minimal API on AWS Lambdain the response body - Open the API Gateway console, click on DemoCalc, select the
ANYresource and click on TEST - Select
Method: GETand 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:
- In step #2 leave this code:
LambdaEventSource.RestApi - Deploy as in step #5
dotnet lambda deploy-function DemoCalc -frun dotnet6 - In the Lambda console click on the
Testtab - Change the
Event JSONto:
{
"path": "/",
"httpMethod": "GET"
}
- Click the
Testbutton and theExecution resultsabove 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.