chalice.cdk Chalice object with custom domain generates broken sam.json file
- Dominant language
- Python
- Stars
- 11.1k
- Forks
- 1k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 2
Description
### Steps to reproduce
1. chalice new-project
2. test
3. [CDK] Rest API with a DynamoDB table
4. edit test/infrastructure/stacks/chaliceapp.py
```python
self.chalice = Chalice(
self, 'ChaliceApp', source_dir=RUNTIME_SOURCE_DIR,
stage_config={
"api_gateway_custom_domain": {
"domain_name": "api.example.com",
"certificate_arn": "arn:aws:acm:us-east-1:1234567:certificate/00000-00000-000000",
},
'environment_variables': {
'APP_TABLE_NAME': self.dynamodb_table.table_name
}
}
)
```
5. cdk synth
### output
```
jsii.errors.JavaScriptError:
Error: Element used in Ref expression with logical ID: 'RestAPI.Stage' not found
at CfnParser.parseIfCfnIntrinsic (/tmp/jsii-kernel-5AgLxw/node_modules/@aws-cdk/core/lib/cfn-parse.js:409:31)
at CfnParser.parseValue (/tmp/jsii-kernel-5AgLxw/node_modules/@aws-cdk/core/lib/cfn-parse.js:379:39)
at CfnParser.parseValue (/tmp/jsii-kernel-5AgLxw/node_modules/@aws-cdk/core/lib/cfn-parse.js:385:33)
at Function._fromCloudFormation (/tmp/jsii-kernel-5AgLxw/node_modules/@aws-cdk/aws-apigateway/lib/apigateway.generated.js:554:51)
at CfnInclude.getOrCreateResource (/tmp/jsii-kernel-5AgLxw/node_modules/@aws-cdk/cloudformation-include/lib/cfn-include.js:557:48)
at new CfnInclude (/tmp/jsii-kernel-5AgLxw/node_modules/@aws-cdk/cloudformation-include/lib/cfn-include.js:66:18)
at /tmp/tmpq4500vx5/lib/program.js:2700:58
at Kernel._wrapSandboxCode (/tmp/tmpq4500vx5/lib/program.js:3128:24)
at Kernel._create (/tmp/tmpq4500vx5/lib/program.js:2700:34)
at Kernel.create (/tmp/tmpq4500vx5/lib/program.js:2441:29)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 6, in
ChaliceApp(app, 'cdktest')
File "/home/gentili/.virtualenvs/concursion-infrastructure/lib/python3.8/site-packages/jsii/_runtime.py", line 83, in __call__
inst = super().__call__(*args, **kwargs)
File "/mnt/c/Users/gentili/git/cdktest/infrastructure/stacks/chaliceapp.py", line 19, in __init__
self.chalice = Chalice(
File "/home/gentili/.virtualenvs/concursion-infrastructure/lib/python3.8/site-packages/jsii/_runtime.py", line 83, in __call__
inst = super().__call__(*args, **kwargs)
File "/home/gentili/.virtualenvs/concursion-infrastructure/lib/python3.8/site-packages/chalice/cdk/construct.py", line 77, in __init__
self.sam_template = cloudformation_include.CfnInclude(
File "/home/gentili/.virtualenvs/concursion-infrastructure/lib/python3.8/site-packages/jsii/_runtime.py", line 83, in __call__
inst = super().__call__(*args, **kwargs)
File "/home/gentili/.virtualenvs/concursion-infrastructure/lib/python3.8/site-packages/aws_cdk/cloudformation_include/__init__.py", line 396, in __init__
jsii.create(CfnInclude, self, [scope, id, props])
File "/home/gentili/.virtualenvs/concursion-infrastructure/lib/python3.8/site-packages/jsii/_kernel/__init__.py", line 265, in create
response = self.provider.create(
File "/home/gentili/.virtualenvs/concursion-infrastructure/lib/python3.8/site-packages/jsii/_kernel/providers/process.py", line 347, in create
return self._process.send(request, CreateResponse)
File "/home/gentili/.virtualenvs/concursion-infrastructure/lib/python3.8/site-packages/jsii/_kernel/providers/process.py", line 329, in send
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Element used in Ref expression with logical ID: 'RestAPI.Stage' not found
```
### Cause
package.py:730
```python
template['Resources'][cfn_name + 'Mapping'] = {
'Type': 'AWS::ApiGateway::BasePathMapping',
'Properties': {
'DomainName': {'Ref': 'ApiGatewayCustomDomain'},
'RestApiId': {'Ref': 'RestAPI'},
'BasePath': domain_name.api_mapping.mount_path,
'Stage': {'Ref': 'RestAPI.Stage'},
}
}
```
The generated sam.json file references `RestAPI.Stage` when no corresponding `AWS::ApiGateway::Stage` resource has been specified.
### Workaround
comment out `'Stage': {'Ref': 'RestAPI.Stage'},`
Contributor guide
Research direction
Start at package.py:730 and reproduce the issue with the shown Chalice CDK stack by running cdk synth. Inspect the generated sam.json BasePathMapping and verify that synthesis no longer references the missing RestAPI.Stage resource while preserving the custom-domain mapping.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, python
- Domain
- api, backend, cloud
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100