Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task
samprakos opened this issue Feb 19, 2025 · 5 comments
Closed
1 task
Labels
bug This issue is a bug. module/aspnetcore-support p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to close soon in 7 days.

Comments

@samprakos
Copy link

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 samprakos added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 19, 2025
@ashishdhingra
Copy link
Contributor

ashishdhingra commented Feb 19, 2025

@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

@ashishdhingra ashishdhingra added response-requested Waiting on additional info and feedback. Will move to close soon in 7 days. p3 This is a minor priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Feb 19, 2025
@samprakos
Copy link
Author

samprakos commented Feb 20, 2025

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.

@samprakos
Copy link
Author

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).

@samprakos
Copy link
Author

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

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/aspnetcore-support p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to close soon in 7 days.
Projects
None yet
Development

No branches or pull requests

2 participants