You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS CDK Python通过LoadBalancerTarget创建RecordSet报错求助

使用AWS CDK创建负载均衡器别名RecordSet报错

错误代码

self.r53_record_set = r53.RecordSet(
    self,
   "rcs-record-set",
     record_type=r53.RecordType.A,
    record_name=subdomain,
    zone=loadbalancer.load_balancer_canonical_hosted_zone_id,
    target=r53.RecordTarget.from_alias(targets.LoadBalancerTarget(loadbalancer))
)

错误信息

RuntimeError: Passed to parameter props of new aws-cdk-lib.aws_route53.RecordSet: Unable 

to deserialize value as aws-cdk-lib.aws_route53.RecordSetProps
├── 🛑 Failing value is an object
│      { '$jsii.struct': [Object] }
╰── 🔍 Failure reason(s):
    ╰─ Key 'zone': Unable to deserialize value as aws-cdk-lib.aws_route53.IHostedZone
        ├── 🛑 Failing value is a string
        │      '${Token[TOKEN.823]}'
        ╰── 🔍 Failure reason(s):
            ╰─ Value does not have the "$jsii.byref" key

问题原因

RecordSet构造函数的zone参数要求传入**IHostedZone类型的Route53托管区对象**,但你传入的是负载均衡器的load_balancer_canonical_hosted_zone_id(字符串类型的Zone ID),类型不匹配导致报错。

CDK高层API(如RecordSet)和底层CloudFormation资源(如CfnRecordSet)的参数逻辑不同:

  • 底层CfnRecordSet直接对应CloudFormation属性,接受字符串格式的hosted_zone_id;
  • 高层RecordSet是CDK封装的抽象,需要传入托管区对象,CDK会自动处理后续的ID映射和Token解析。

修正后的代码

假设你已经定义了Route53托管区对象(通过创建或导入方式),将zone参数替换为该对象即可:

# 示例:导入已有托管区,或用r53.HostedZone创建新托管区
# hosted_zone = r53.HostedZone.from_hosted_zone_attributes(self, "MyZone", hosted_zone_id="ZONE_ID", zone_name="example.com")

self.r53_record_set = r53.RecordSet(
    self,
    "rcs-record-set",
    record_type=r53.RecordType.A,
    record_name=subdomain,
    zone=hosted_zone,  # 传入你的Route53托管区对象
    target=r53.RecordTarget.from_alias(targets.LoadBalancerTarget(loadbalancer))
)

说明:LoadBalancerTarget会自动提取负载均衡器的DNS名称和Canonical Zone ID,无需手动传入这些值,只需确保zone参数指向你要创建RecordSet的Route53托管区对象。

内容的提问来源于stack exchange,提问作者Zu Jiry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 10:27:15