aws / aws/chalice

Use CDK to setup Route 53 alias for custom domain

Open
#1,728 4 comments 6 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
11.1k
Forks
1k
Avg merge
1d 22h
Merged PRs (30d)
2

Description

First of all: Having a lot of fun using Chalice with CDK, very cool project :)

### What I am trying to do

I basically followed https://aws.github.io/chalice/tutorials/cdk.html to setup my project.

I am trying to use a custom domain and let CDK configure an alias to it (basically following the solution outlined here: https://stackoverflow.com/questions/65986897/how-to-provide-a-custom-domain-name-for-a-lambda-based-apigateway-in-cdk).

So I am trying to do this:

```Python
from aws_cdk import (
aws_route53 as route53,
aws_route53_targets as route53_targets,
)

hosted_zone = route53.HostedZone.from_lookup(self, 'MyExistingHostedZone', domain_name="app.example.com")

rest_api =

route53.ARecord(
self, 'MyNewARecord',
zone=hosted_zone,
record_name="api",
target=route53.RecordTarget.from_alias(route53_targets.ApiGateway(rest_api))
)
```

### Problem

The problem is that I cannot get my hands on `rest_api` because it is created by Chalice and not by CDK. I tried a lot of ways starting with:

```Python
REST_API_RESOURCE_NAME = 'RestAPI'
cfn_api_gateway = self.chalice.get_resource(REST_API_RESOURCE_NAME)
```

Then for example:

```Python
from aws_cdk import (
aws_apigateway as apigateway,
)
apigateway.RestApi.from_rest_api_id(self, REST_API_RESOURCE_NAME, ???)
```

But the classes don't match up ... so for example I would get this error:

```
jsii.errors.JavaScriptError:
Error: Object of type @aws-cdk/aws-apigateway.RestApiBase is not convertible to @aws-cdk/aws-apigateway.RestApi
at Object.deserialize (C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:9107:35)
at Kernel._toSandbox (C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:8505:69)
at C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:8553:42
at Array.map ()
at Kernel._boxUnboxParameters (C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:8553:27)
at Kernel._toSandboxValues (C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:8539:29)
at C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:8154:75
at Kernel._wrapSandboxCode (C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:8582:24)
at Kernel._create (C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:8154:34)
at Kernel.create (C:\Users\JONASP~1\AppData\Local\Temp\tmpmocjnbdz\lib\program.js:7895:29)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "app.py", line 13, in
ChaliceApp(
File "[..]\venv\lib\site-packages\jsii\_runtime.py", line 83, in __call__
inst = super().__call__(*args, **kwargs)
File "[..]\infrastructure\stacks\chaliceapp.py", line 76, in __init__
target=route53.RecordTarget.from_alias(route53_targets.ApiGateway(rest_api))
File "[..]\venv\lib\site-packages\jsii\_runtime.py", line 83, in __call__
inst = super().__call__(*args, **kwargs)
File "[..]\venv\lib\site-packages\aws_cdk\aws_route53_targets\__init__.py", line 444, in __init__
jsii.create(ApiGateway, self, [api])
File "[..]\venv\lib\site-packages\jsii\_kernel\__init__.py", line 275, in create
response = self.provider.create(
File "[..]\venv\lib\site-packages\jsii\_kernel\providers\process.py", line 344, in create
return self._process.send(request, CreateResponse)
File "[..]\venv\lib\site-packages\jsii\_kernel\providers\process.py", line 326, in send
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Object of type @aws-cdk/aws-apigateway.RestApiBase is not convertible to @aws-cdk/aws-apigateway.RestApi
```

There also is another target called `route53_targets.ApiGatewayDomain` but I am not sure that is even the right thing to use (documentation is basically silent on this one) and also I am not having any more luck starting with the following either:

```Python
REST_API_RESOURCE_NAME = 'ApiGatewayCustomDomain'
cfn_api_gateway = self.chalice.get_resource(REST_API_RESOURCE_NAME)
```

### Question

Is this possible somehow? Is this a user error? Could it be made possible? Is there a workaround? :)

Thanks a lot!

Contributor guide

Open the contributing guide

Research direction

Start with the CDK tutorial and the resource access in infrastructure/stacks/chaliceapp.py, especially chalice.get_resource('RestAPI') and the attempted ApiGateway and ApiGatewayDomain targets. Check which object type Chalice exposes for the API and what Route 53 alias target requires. Done means the issue has a supported way to create the alias, or documents a verified workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
api, cloud
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.