AWS CDK,在 Route53 中为现有云前端分布创建别名记录

AWS CDK, creating an alias record for existing cloud front distribution in Route53

我正在使用 AWS CDK ( Python ) 创建针对现有云前端分布的别名记录,该分布已在不同的 AWS 账户和不同的 Route53 托管区域中配置。

我正在通过以下 CDK 代码导入现有的 CF 分发和 public Route53 区域。

    existinghostezone=route53.HostedZone.from_hosted_zone_attributes(self,"existinghostedzone",
            hosted_zone_id="HOSTEDZONE-ID",
            zone_name ="ZONENAME")

    existingdist=cloudfront.Distribution.from_distribution_attributes(self,"existingdist",
         distribution_id="DISTRIBUTION-ID",
           domain_name="DISTRIBUTION-DOMAIN-NAME" 
           )

下面是我创建新的 route53 托管区域并尝试创建 route53 指向上面导入的云前端分布的记录的代码。

#Creating new route53 zone

newZone=route53.HostedZone(self,"newZone",
                zone_name="new-zone-name",
                comment="Managed by CloudFormation")

#Creating alias entry config 

my_alias = route53.IAliasRecordTarget.bind(
            self,
            record=route53.AliasRecordTargetConfig(
               hosted_zone_id= existinghostezone.hosted_zone_id,
               dns_name=existingdist.domain_name)
            ) 

#Creating route53 a record

aRecord=route53.ARecord(self,"aRecord",
         target=route53.RecordTarget(my_alias),
         zone=newZone.hosted_zone_id,
         record_name="new-record-name",
         comment="Managed by CloudFormation"
         )

现在,当我尝试 运行 cdk synth 上面的代码时,我收到以下错误消息。

packages/jsii/_kernel/providers/process.py", line 332, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Expected object reference, got "${Token[TOKEN.58]}"

由于我是 AWS CDK ( Python ) 的新手,我真的不知道上面的错误是什么意思。

我也试过用下面的方法创建别名记录

#Creating new route53 zone
newZone=route53.HostedZone(self,"newZone",
                zone_name="new-zone-name",
                comment="Managed by CloudFormation")

#Creating alias entry config 

my_alias = route53.IAliasRecordTarget.bind(
            self,
            record=route53.AliasRecordTargetConfig(
               hosted_zone_id= existinghostezone.hosted_zone_id,
               dns_name=existingdist.domain_name)
            ) 

#Creating route53 a record

aRecord=route53.ARecord(self,"aRecord",
         target=route53.RecordTarget.from_alias(my_alias),
         zone=newZone.hosted_zone_id,
         record_name="new-record-name",
         comment="Managed by CloudFormation"
         )

现在,当我尝试 运行 cdk synth 上面的代码时,我收到以下错误消息。

raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Got 'undefined' for non-optional instance of {"name":"aliasTarget","type":{"fqn":"@aws-cdk/aws-route53.IAliasRecordTarget"}}

我试图找到一些使用现有云前端分发作为别名条目的示例,但是每个人都有不同的方法和答案,这些都不适合我。

请指教

创建您的 ARecord, you'll want to provide the zone object (IHostedZone) for the zone 属性

试试:

aRecord = route53.ARecord(self,"aRecord",
    target=route53.RecordTarget(my_alias),
    zone=newZone,  # rather than newZone.hosted_zone_id
    record_name="new-record-name",
    comment="Managed by CloudFormation"
)