简体   繁体   中英

How to create hosted zone in AWS CDK?

Hi I am working on AWS CDK for creating hosted zone for my application deployed using ELB and ECS. I am familiar with cloud formation and below is my sample cloud formation template.

LocationServiceRoute53:
    Type: "AWS::Route53::RecordSet"
    Properties:
      HostedZoneId: !ImportValue "infra-r53-zones-region::PrivateZoneId"
      Comment: "Zone alias targeted to LoadBalancer"
      Name:
        !Join
        - "."
        - - "app"
          - "locationservices"
          - !ImportValue "infra-r53-zones-region::PrivateZoneName"

      Type: "A"
      AliasTarget:
        # yamllint disable-line rule:line-length
        HostedZoneId: {'Fn::ImportValue': !Sub 'location-agent-alb${StackSuffix}::MWSLoadBalancerHostedZoneId'}
        # yamllint disable-line rule:line-length
        DNSName: {'Fn::ImportValue': !Sub 'location-agent-alb${StackSuffix}::MWSLoadBalancerDNSName'}

I am re-writing in CDK as below.

 hostedZone = route.HostedZone.from_hosted_zone_attributes(self, 'HostedZone', hosted_zone_id='some id ', zone_name='zone name')
    recordName = 'record name'

    targetAlias = route.AddressRecordTarget.from_alias(alias_target )
    route.ARecord(self, id='AliasRecord', zone = hostedZone, comment='Zone alias targeted to LoadBalancer',
    record_name=recordName, target=targetAlias)

This is throwing AttributeError: 'ApplicationLoadBalancer' object has no attribute 'dns_name' Can someone help me to write it? Any help would be appreciated. Thanks

This worked for me.

 hostedZone = route.HostedZone.from_hosted_zone_attributes(self, 'HostedZone', hosted_zone_id='some id ', zone_name='zone name')
        recordName = 'r name'
        route.ARecord(self, id='AliasRecord', zone = hostedZone, comment='Zone alias targeted to LoadBalancer',
        record_name=recordName, target = route.RecordTarget.from_alias(route_targets.LoadBalancerTarget(lb)))

I took my working code and adapted to your case:

const cdk = require('@aws-cdk/core')
const route53 = require('@aws-cdk/aws-route53')
const alias = require('@aws-cdk/aws-route53-targets')

const hostedZoneId = cdk.Fn.importValue(`infra-r53-zones-region::PrivateZoneId`)
const zone = route53.HostedZone.fromLookup(this, hostedZoneId, {domainName: 'xxxx-xxx.com'});
new route53.ARecord(this, 'AliasRecord', {
  zone,
  target: route53.RecordTarget.fromAlias(new alias.LoadBalancerTarget(<loadBalancer>)),
  recordName: <your_domain_name>
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM