Bug: Unable to use !Sub in function name for local start-api
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
### Description:
When using `sam local start-api`, and using a cloudformation function such as !Sub in a FunctionName property, the API fails with the error 'ValueError: Function name is required'.
Attempting to provide parameters to the template via --parameter-overrides or a parameter_overrides value in the samconfig.toml does not fix the issue. Using a FunctionName value of `!Sub "MyFunc"` also does not work, even though it does not use a parameter that might not be available.
### Steps to reproduce:
Create a simple api gateway resource with an attached lambda function, and as the FunctionName, use the !Sub cloudformation function, the `sam local start-api` command will run, but hitting the endpoint will throw an internal server error.
Example template.yml:
```
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Test api
Parameters:
Environment:
Type: String
Default: "MyEnv"
Resources:
ApiGateWay:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub 'MyApiGateway2-${Environment}'
Description: 'This is my api gateway'
Resource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref ApiGateWay
ParentId: !GetAtt ApiGateWay.RootResourceId
PathPart: "hello"
HelloGetMethod:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: GET
ResourceId: !Ref Resource
RestApiId: !Ref ApiGateWay
AuthorizationType: NONE
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Join ['', ['arn:aws:apigateway:', !Ref AWS::Region, ':lambda:path/2015-03-31/functions/', !GetAtt HelloGetFunction.Arn, '/invocations']]
HelloGetFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: myfunc1/
Handler: app.lambdaHandler
Runtime: nodejs16.x
FunctionName: !Sub 'MyFunction-${Environment}'
```
### Observed result:
The command will execute fine, even after doing a fresh `sam build`, however hitting the api gateway endpoint related to the function, will return the message "Internal server error" and a command line error of "ValueError: Function name is required"
### Expected result:
The function name to be able to use parameter values when running `sam local start-api` which can be provided by the samconfig.toml or command args.
The response from the api should be a successful execution of the function.
### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
1. OS: Ubuntu 20.04
2. `sam --version`: SAM CLI, version 1.70.0
3. AWS region: Local
```
# Paste the output of `sam --info` here
```
{
"version": "1.70.0",
"system": {
"python": "3.7.10",
"os": "Linux-5.15.0-57-generic-x86_64-with-debian-bullseye-sid"
},
"additional_dependencies": {
"docker_engine": "20.10.23",
"aws_cdk": "Not available",
"terraform": "Not available"
}
}
`Add --debug flag to command you are running`
```
sam local start-api --debug
2023-01-20 18:05:20,162 | Config file location: /home/reece/workspace/playground/sam-cli-test/my-new-one/samconfig.toml
2023-01-20 18:05:20,162 | Loading configuration values from [default.['local', 'start-api'].parameters] (env.command_name.section) in config file at '/home/reece/workspace/playground/sam-cli-test/my-new-one/samconfig.toml'...
2023-01-20 18:05:20,164 | Configuration values successfully loaded.
2023-01-20 18:05:20,164 | Configuration values are: {}
2023-01-20 18:05:20,170 | Using SAM Template at /home/reece/workspace/playground/sam-cli-test/my-new-one/.aws-sam/build/template.yaml
2023-01-20 18:05:20,189 | Using config file: samconfig.toml, config environment: default
2023-01-20 18:05:20,189 | Expand command line arguments to:
2023-01-20 18:05:20,189 | --template_file=/home/reece/workspace/playground/sam-cli-test/my-new-one/.aws-sam/build/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/home/reece/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2023-01-20 18:05:20,271 | local start-api command is called
2023-01-20 18:05:20,278 | Collected default values for parameters: {'Environment': 'MyEnv'}
2023-01-20 18:05:20,292 | There is no customer defined id or cdk path defined for resource ApiGateWay, so we will use the resource logical id as the resource id
2023-01-20 18:05:20,292 | There is no customer defined id or cdk path defined for resource Resource, so we will use the resource logical id as the resource id
2023-01-20 18:05:20,292 | There is no customer defined id or cdk path defined for resource HelloGetMethod, so we will use the resource logical id as the resource id
2023-01-20 18:05:20,292 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloGetFunction is HelloGetFunction
2023-01-20 18:05:20,293 | Unable to resolve property Uri: OrderedDict([('Fn::Join', ['', ['arn:aws:apigateway:', OrderedDict([('Ref', 'AWS::Region')]), ':lambda:path/2015-03-31/functions/', OrderedDict([('Fn::GetAtt', ['HelloGetFunction', 'Arn'])]), '/invocations']])]). Leaving as is.
2023-01-20 18:05:20,293 | 0 stacks found in the template
2023-01-20 18:05:20,293 | Collected default values for parameters: {'Environment': 'MyEnv'}
2023-01-20 18:05:20,304 | There is no customer defined id or cdk path defined for resource ApiGateWay, so we will use the resource logical id as the resource id
2023-01-20 18:05:20,304 | There is no customer defined id or cdk path defined for resource Resource, so we will use the resource logical id as the resource id
2023-01-20 18:05:20,305 | There is no customer defined id or cdk path defined for resource HelloGetMethod, so we will use the resource logical id as the resource id
2023-01-20 18:05:20,305 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloGetFunction is HelloGetFunction
2023-01-20 18:05:20,305 | Unable to resolve property Uri: OrderedDict([('Fn::Join', ['', ['arn:aws:apigateway:', OrderedDict([('Ref', 'AWS::Region')]), ':lambda:path/2015-03-31/functions/', OrderedDict([('Fn::GetAtt', ['HelloGetFunction', 'Arn'])]), '/invocations']])]). Leaving as is.
2023-01-20 18:05:20,305 | 4 resources found in the stack
2023-01-20 18:05:20,305 | Found Serverless function with name='HelloGetFunction' and CodeUri='HelloGetFunction'
2023-01-20 18:05:20,305 | --base-dir is not presented, adjusting uri HelloGetFunction relative to /home/reece/workspace/playground/sam-cli-test/my-new-one/.aws-sam/build/template.yaml
2023-01-20 18:05:20,309 | Skipping resource 'ApiGateWay'. Swagger document not found in Body and BodyS3Location
2023-01-20 18:05:20,309 | This Integration URI format is not supported: OrderedDict([('Fn::Join', ['', ['arn:aws:apigateway:', OrderedDict([('Ref', 'AWS::Region')]), ':lambda:path/2015-03-31/functions/', OrderedDict([('Fn::GetAtt', ['HelloGetFunction', 'Arn'])]), '/invocations']])])
2023-01-20 18:05:20,309 | Extracted Function ARN: None
2023-01-20 18:05:20,309 | 1 APIs found in the template
2023-01-20 18:05:20,313 | Mounting None at http://127.0.0.1:3000/hello [GET]
2023-01-20 18:05:20,313 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template
2023-01-20 18:05:20,313 | Localhost server is starting up. Multi-threading = True
2023-01-20 18:05:20 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
```
Contributor guide
Assessment
This issue has not been assessed yet.