aws / aws/aws-sam-cli

local start-api doesn't work with openapi spec and Fn::Sub

Open
#3,961 3 comments 1 reaction 0 assignees View on GitHub
area/local/start-api stage/bug-repro type/bug
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

### Description:
When trying to use `sam local start-api` with the CDK synth output of my successfully deployed SpecRestApi (API-Gateway with OpenAPI definition file), the execution fails with the message
> AttributeError: 'str' object has no attribute 'get'

This is probably related to me using an inline ApiDefinition with `Fn::Sub` to replace placeholders (e.g. ARNs, role names) in the openapi definition.

### Steps to reproduce:

- create a valid OpenAPI definition file, e.g. this one:
```
openapi: "3.0.3"
info:
title: MyAPI
version: 1.0.0
paths:
/hello-world:
get:
operationId: hello-world
summary: Test call
responses:
'200':
description: |-
200 response
content:
application/json:
examples:
foo:
value:
{
"message": "Hello World!"
}
x-amazon-apigateway-integration:
type: "mock"
credentials: "${ApiRoleArn}"
responses:
default:
statusCode: "200"
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
```
- supply the definition file to a SpecRestApi and replace the placeholders through `Fn::Sub`
```
const contract = yaml.load(fs.readFileSync("path/to/contract.yaml", "utf8"));
const replacements = {
ApiRoleArn: "..."
};
const apiDefinition = ApiDefinition.fromInline({"Fn::Sub": [yaml.dump(contract, { noRefs: true }), replacements]});
new SpecRestApi(scope, id, {apiDefinition});
```
- run `cdk synth --no-staging`
- try to locally start the api through SAM: `sam local start-api -t ./cdk.out/My-Stack.template.json`

### Observed result:
```
❱ sam local start-api -t ./cdk.out/My-Stack.template.json --debug
2022-06-10 17:23:43,184 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-06-10 17:23:43,184 | Using config file: samconfig.toml, config environment: default
2022-06-10 17:23:43,184 | Expand command line arguments to:
2022-06-10 17:23:43,184 | --template_file=/home/user/develop/oil/cdk/cdk.out/My-Stack.template.json --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/home/user/.aws-sam/layers-pkg --container_host=localhost
--container_host_interface=127.0.0.1
2022-06-10 17:23:43,239 | local start-api command is called
2022-06-10 17:23:43,239 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2022-06-10 17:23:43,252 | CDK Path for resource ApiRole1873F438 is ['My-Stack', 'ApiRole', 'Resource']
2022-06-10 17:23:43,252 | CDK Path for resource ApiRoleDefaultPolicyCFEBAD31 is ['My-Stack', 'ApiRole', 'DefaultPolicy', 'Resource']
2022-06-10 17:23:43,252 | CDK Path for resource helloworldServiceRole3FFFDE20 is ['My-Stack', 'hello-world', 'ServiceRole', 'Resource']
2022-06-10 17:23:43,253 | CDK Path for resource helloworldA481721C is ['My-Stack', 'hello-world', 'Resource']
2022-06-10 17:23:43,253 | CDK Path for resource helloworldCurrentVersionCFD266783a5b2782a49c10746dc4919d972c4ce2 is ['My-Stack', 'hello-world', 'CurrentVersion', 'Resource']
2022-06-10 17:23:43,253 | CDK Path for resource API62EA1CFF is ['My-Stack', 'API', 'Resource']
2022-06-10 17:23:43,253 | CDK Path for resource APIDeployment0E06874E77e6b3645783f3118581e15736eed8a3 is ['My-Stack', 'API', 'Deployment', 'Resource']
2022-06-10 17:23:43,253 | CDK Path for resource APIDeploymentStageapi1CB36A6A is ['My-Stack', 'API', 'DeploymentStage.api', 'Resource']
2022-06-10 17:23:43,253 | CDK Path for resource APICloudWatchRole927CEB3B is ['My-Stack', 'API', 'CloudWatchRole', 'Resource']
2022-06-10 17:23:43,253 | CDK Path for resource APIAccountD065B313 is ['My-Stack', 'API', 'Account']
2022-06-10 17:23:43,253 | CDK Path for resource CDKMetadata is ['My-Stack', 'CDKMetadata', 'Default']
2022-06-10 17:23:43,254 | 0 stacks found in the template
2022-06-10 17:23:43,254 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2022-06-10 17:23:43,265 | CDK Path for resource ApiRole1873F438 is ['My-Stack', 'ApiRole', 'Resource']
2022-06-10 17:23:43,265 | CDK Path for resource ApiRoleDefaultPolicyCFEBAD31 is ['My-Stack', 'ApiRole', 'DefaultPolicy', 'Resource']
2022-06-10 17:23:43,265 | CDK Path for resource helloworldServiceRole3FFFDE20 is ['My-Stack', 'hello-world', 'ServiceRole', 'Resource']
2022-06-10 17:23:43,266 | CDK Path for resource helloworldA481721C is ['My-Stack', 'hello-world', 'Resource']
2022-06-10 17:23:43,266 | CDK Path for resource helloworldCurrentVersionCFD266783a5b2782a49c10746dc4919d972c4ce2 is ['My-Stack', 'hello-world', 'CurrentVersion', 'Resource']
2022-06-10 17:23:43,266 | CDK Path for resource API62EA1CFF is ['My-Stack', 'API', 'Resource']
2022-06-10 17:23:43,266 | CDK Path for resource APIDeployment0E06874E77e6b3645783f3118581e15736eed8a3 is ['My-Stack', 'API', 'Deployment', 'Resource']
2022-06-10 17:23:43,266 | CDK Path for resource APIDeploymentStageapi1CB36A6A is ['My-Stack', 'API', 'DeploymentStage.api', 'Resource']
2022-06-10 17:23:43,266 | CDK Path for resource APICloudWatchRole927CEB3B is ['My-Stack', 'API', 'CloudWatchRole', 'Resource']
2022-06-10 17:23:43,266 | CDK Path for resource APIAccountD065B313 is ['My-Stack', 'API', 'Account']
2022-06-10 17:23:43,266 | CDK Path for resource CDKMetadata is ['My-Stack', 'CDKMetadata', 'Default']
2022-06-10 17:23:43,267 | 11 resources found in the stack
2022-06-10 17:23:43,267 | Found Lambda function with name='helloworldA481721C' and CodeUri='/home/user/develop/oil/cdk/cdk.out/asset.25ee579b79e9f78a3b184912272be99ada659b0910bac201a4e13289304c39f8'
2022-06-10 17:23:43,267 | --base-dir is not presented, adjusting uri /home/user/develop/oil/cdk/cdk.out/asset.25ee579b79e9f78a3b184912272be99ada659b0910bac201a4e13289304c39f8 relative to /home/user/develop/oil/cdk/cdk.out/My-Stack.template.json
2022-06-10 17:23:43,272 | Detected Inline Swagger definition
Traceback (most recent call last):
File "samcli/__main__.py", line 12, in
File "click/core.py", line 829, in __call__
File "click/core.py", line 782, in main
File "click/core.py", line 1259, in invoke
File "click/core.py", line 1259, in invoke
File "click/core.py", line 1066, in invoke
File "click/core.py", line 610, in invoke
File "click/decorators.py", line 73, in new_func
File "click/core.py", line 610, in invoke
File "samcli/lib/telemetry/metric.py", line 166, in wrapped
File "samcli/lib/telemetry/metric.py", line 124, in wrapped
File "samcli/lib/utils/version_checker.py", line 41, in wrapped
File "samcli/cli/main.py", line 87, in wrapper
File "samcli/commands/local/start_api/cli.py", line 117, in cli
File "samcli/commands/local/start_api/cli.py", line 192, in do_cli
File "samcli/commands/local/lib/local_api_service.py", line 37, in __init__
File "samcli/lib/providers/api_provider.py", line 37, in __init__
File "samcli/lib/providers/api_provider.py", line 64, in _extract_api
File "samcli/lib/providers/cfn_api_provider.py", line 61, in extract_resources
File "samcli/lib/providers/cfn_api_provider.py", line 114, in _extract_cloud_formation_route
File "samcli/lib/providers/cfn_base_api_provider.py", line 85, in extract_swagger_route
File "samcli/commands/local/lib/swagger/parser.py", line 72, in get_routes
AttributeError: 'str' object has no attribute 'get'
[30681] Failed to execute script __main__
```

### Expected result:
`sam local start-api` should be able to work with the synthesized CloudFormation template, as deploying it to AWS does work and a functioning API-Gateway is successfully created.

### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

1. OS: Ubuntu 20.04 through WSL2
2. `sam --version`: `SAM CLI, version 1.50.0`
3. AWS region: `eu-central-1`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.