aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.47k stars 1.16k forks source link

Getting `ValueError: Function name is required` when running API Gateway from Terraform #7236

Open sevetseh28 opened 1 month ago

sevetseh28 commented 1 month ago

Hi everyone. I'm running my API Gateway locally with:

sam local start-api --hook-name terraform --disable-authorizer

The API Gateway starts without issues ,but when hitting any endpoint I get the following:

Traceback (most recent call last):                                                                                                     
  File "flask/app.py", line 1473, in wsgi_app                                                                                          
  File "flask/app.py", line 882, in full_dispatch_request                                                                              
  File "flask/app.py", line 880, in full_dispatch_request                                                                              
  File "flask/app.py", line 865, in dispatch_request                                                                                   
  File "samcli/local/apigw/local_apigw_service.py", line 725, in _request_handler                                                      
  File "samcli/local/apigw/local_apigw_service.py", line 618, in _invoke_lambda_function                                               
  File "samcli/commands/local/lib/local_lambda.py", line 118, in invoke                                                                
  File "samcli/lib/providers/sam_function_provider.py", line 122, in get                                                               
ValueError: Function name is required

I ran the above with and without the authoriser, but I've also tried running the authoriser individually with: sam local invoke ModuleIdeApiAwsLambdaFunctionAuthorizerFunction836CD508 --hook-name terraform --skip-prepare-infra --event authoriser-event.json

and it works well when running the Lambda function alone. Any ideas what could be wrong?

Thank you!

sevetseh28 commented 1 month ago

BTW the endpoint im calling is using an AWS Integration (for example, to start execution of a State machine). I guess this might be why, as it's trying to invoke a lambda function for this integration. Is there any way I can mock it?

lucashuy commented 1 month ago

Can you provide a sample project for us to reproduce this issue? The bounds of what we support with sam local is limited, and we may not support your use case (ie. APIGW integration with a state machine), but we'll be able to help further with a project.

sevetseh28 commented 1 month ago

hey @lucashuy thanks for your reply. I ended up changing my integration type to Lambda proxy integration for more flexibility and endpoint customisation. I was initially using Step Functions integration but sam doesn't seem to support it, I read the source code here. I thought that the SAM tool would mimic the Integration through a Lambda function locally

lucashuy commented 1 month ago

Our support for Lambda invocations locally is limited to directly invoking the Lambda (through sam local invoke and sam local start-lambda), and with the API Gateway Lambda integrations (using sam local start-api).

Unfortunately we don't support the local invocations of Lambda through other integration means with API Gateway.

The AWS Step Functions team created a tool to use together with SAM CLI to help test Step Functions locally and requires a little bit of setup to use. This can be an alternative to using SAM CLI as an "all-in-one" solution. https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local-lambda.html

I'm going to leave this issue open for now as the only item for our team to take from this issue is that we probably should fix that method that returns a ValueError, to return our own error instead of reusing the built-in exception.