aws/aws-lambda-dotnet

Object reference not set to an instance of an object when proxying from API Gateway

samprakos opened this issue · 5 comments

Describe the bug

I have a simple "Hello world" lambda (c#) that works when invoking it via a function url. However, once I try to invoke it via API Gateway (lambda integration), I get "Object reference not set to an instance of an object" from APIGatewayHttpApiV2ProxyFunction.MarshallRequest.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

Messages from API Gateway are processed properly.

Current Behavior

Null reference error

Reproduction Steps

  1. Create a lambda using the attached zip
  2. Run dotnet lambda deploy-function from the project directory
  3. Navigate to the function in AWS Console, create a Function URL
  4. Invoke the lambda using the Function URL from a browser (should see "Hello from AWS Lambda!")
  5. Copy the Lambda ARN so you can map an API Gateway "proxy" call to it.
  6. Got API Gateway, create a REST API.
  7. Map a resource as a "proxy" resource.
  8. Add an "ANY" method to the resource.
  9. Configure the "ANY" method to proxy calls to the lambda ARN
  10. Test the call using the "Test" tab, a GET to the root of the API
  11. Should get "Object reference not set to an instance of an object" error.

I've tried setting up the lambda using services.AddAWSLambdaHosting(LambdaEventSource.RestApi); and services.AddAWSLambdaHosting(LambdaEventSource.RestApi) and services.AddAWSLambdaHosting(LambdaEventSource.HttpApi)...same result.

TodoApi.zip

Possible Solution

No suggestions...but the stack trace I get doesn't include file line numbers...that would be helpful. The stacktrace I get is this:

2025-02-19T20:03:33.732Z	7af2e7e1-d232-4fb4-a1b6-882286c3dd37	fail	System.NullReferenceException: Object reference not set to an instance of an object.
   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 ---

Additional Information/Context

Here is how I set up the Integration request for my api gateway method. I have tried setting "Lambda proxy integration" to true and false...same result.

Image

AWS .NET SDK and/or Package version used

Amazon.Lambda.AspNetCoreServer.Hosting version 1.7.3
Dotnet 8

Targeted .NET Platform

.net 8

Operating System and version

x86_64

@samprakos Good afternoon. Thanks for opening the issue. For API Gateway REST API, you would need to use APIGatewayProxyRequest and APIGatewayProxyResponse as request and response types respectively. Please refer Amazon.Lambda.APIGatewayEvents Readme.

Also refer Invoking a Lambda function using an Amazon API Gateway endpoint which specifies request and response format details.

Thanks,
Ashish

Hi @ashishdhingra thanks for the response. It is my understanding that the Amazon.Lambda.AspNetCoreServer.Hosting lib handles that via MarshallRequest. It expects an APIGatewayHttpApiV2ProxyRequest‎. The example in the link you provide is a node example, which wouldn't use the dotnet Hosting lib.

More detail: The API Gateway "type" that I need to work is Rest (versus HTTP). If I set up my sample Lambda with LambdaEventSource.HttpApi and define an api gateway of type HTTP, it works. If I set up my sample Lambda with LambdaEventSource.RestApi and define an api gateway of type Rest, it doesn't work. In the case of Rest, the request gets routed to MarshallRequest (different class than my previous comment).

I got it to work. The key for me was to NOT check the "Proxy resource" toggle when creating the API Gateway resource that proxies requests to the lambda.

Image

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.