使用 targets.LoadbalancerTarget 在AWS CDK中使用Python创建RecordSet。

huangapple go评论121阅读模式
英文:

Use targets.LoadbalancerTarget to create RecordSet in AWS CDK with Python

问题

我想创建一个带有别名的 RecordSet,用于从负载均衡器中加载,我使用了 aws_cdk.aws_route53_targets 作为目标。

以下是我得到的错误信息:

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

这个错误是什么原因引起的?因为 "Token" 应该在 cdk deploy 期间被访问,所以我不应该得到这个错误吗?

以下是一个可以正常工作的示例(使用 Cfn):

self.r53_record_set = r53.CfnRecordSet(
    self,
    "rcs-record-set",
    hosted_zone_id=loadbalancer.load_balancer_canonical_hosted_zone_id,
    name=subdomain + str(loadbalancer.load_balancer_canonical_hosted_zone_id),
    type="A",
    alias_target=r53.CfnRecordSet.AliasTargetProperty(
        dns_name=loadbalancer.load_balancer_dns_name,
        hosted_zone_id=loadbalancer.load_balancer_canonical_hosted_zone_id
    )
)

我做错了什么吗?感谢您的帮助!

英文:

I want to create a RecordSet with an alias from a Loadbalancer, for which I use the

aws_cdk.aws_route53_targets as targets

This does not work, but I'd like it to work and am not understanding the error:

    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))
    )

The Error I get -

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

I should not get this error? As the "Token" is should be accessed during the cdk deploy?

This works fine (Cfn):

    self.r53_record_set = r53.CfnRecordSet(
        self,
        "rcs-record-set",
        hosted_zone_id=loadbalancer.load_balancer_canonical_hosted_zone_id,
        name=subdomain + str(loadbalancer.load_balancer_canonical_hosted_zone_id),
        type="A",
        alias_target=r53.CfnRecordSet.AliasTargetProperty(
            dns_name=loadbalancer.load_balancer_dns_name,
            hosted_zone_id=loadbalancer.load_balancer_canonical_hosted_zone_id
        )
    )

Am I doing something wrong? Thank you for your help!

答案1

得分: 0

如@gshpychka指出的那样:我错过了,我需要一个托管区域而不是一个ID。

self.r53_hosted_zone = r53.HostedZone(
    self,
    "rcs-hosted-zone",
    zone_name=hosted_zone_name 
)
self.r53_record_set = r53.RecordSet(
    self,
    "rcs-record-set",
    record_type=r53.RecordType.A,
    record_name=subdomain,
    zone=self.r53_hosted_zone,
    target=r53.RecordTarget.from_alias(targets.LoadBalancerTarget(loadbalancer))
)
英文:

As @gshpychka pointed out: I missed, that I needed a hosted zone instead of an id.

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

huangapple
  • 本文由 发表于 2023年7月12日 22:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定