Route 53 範本程式碼片段 - AWS CloudFormation

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Route 53 範本程式碼片段

使用託管區域名稱或 ID 的 Amazon Route 53 資源紀錄集

當您建立 Amazon Route 53 資源紀錄集時,必須指定您想要新增紀錄集的託管區域。 AWS CloudFormation 提供兩種方式來指定託管區域:

  • 您可以使用 HostedZoneId 屬性來明確指定託管區域。

  • 您可以使用該HostedZoneName屬性 AWS CloudFormation 找到託管區域。如果您使用該HostedZoneName屬性,並且有多個具有相同名稱的託管區域,則 AWS CloudFormation 不會建立堆疊。

新增 RecordSet 使用 HostedZoneId

此範例會新增包含使用 HostedZoneId 屬性指定託管區域之網域名稱 mysite.example.com SPF 記錄的 Amazon Route 53 資源紀錄集,

JSON

"myDNSRecord" : { "Type" : "AWS::Route53::RecordSet", "Properties" : { "HostedZoneId" : "Z3DG6IL3SJCGPX", "Name" : "mysite.example.com.", "Type" : "SPF", "TTL" : "900", "ResourceRecords" : [ "\"v=spf1 ip4:192.168.0.1/16 -all\"" ] } }

YAML

myDNSRecord: Type: AWS::Route53::RecordSet Properties: HostedZoneId: Z3DG6IL3SJCGPX Name: mysite.example.com. Type: SPF TTL: '900' ResourceRecords: - '"v=spf1 ip4:192.168.0.1/16 -all"'

新增 RecordSet 使用 HostedZoneName

此範例使用 HostedZoneName 屬性來指定託管區域,為網域名稱 "mysite.example.com" 新增 Amazon Route 53 資源紀錄集。

JSON

"myDNSRecord2" : { "Type" : "AWS::Route53::RecordSet", "Properties" : { "HostedZoneName" : "example.com.", "Name" : "mysite.example.com.", "Type" : "A", "TTL" : "900", "ResourceRecords" : [ "192.168.0.1", "192.168.0.2" ] } }

YAML

myDNSRecord2: Type: AWS::Route53::RecordSet Properties: HostedZoneName: example.com. Name: mysite.example.com. Type: A TTL: '900' ResourceRecords: - 192.168.0.1 - 192.168.0.2

用 RecordSetGroup 於設定加權資源記錄集

此範例使用「AWS::Route53::RecordSet群組」為「example.com」設定兩個 CNAME 記錄。託管區域。RecordSets 屬性包含 "mysite.example.com" DNS 名稱的 CNAME 記錄集。每一個記錄集都包含識別碼 (SetIdentifier) 和權重 (Weight)。路由至資源的網際網路流量比例是根據下列計算:

  • Frontend One: 140/(140+60) = 140/200 = 70%

  • Frontend Two: 60/(140+60) = 60/200 = 30%

如需有關加權資源記錄集的詳細資訊,請參閱《Amazon Route 53 開發人員指南》中的加權路由

JSON

"myDNSOne" : { "Type" : "AWS::Route53::RecordSetGroup", "Properties" : { "HostedZoneName" : "example.com.", "Comment" : "Weighted RR for my frontends.", "RecordSets" : [ { "Name" : "mysite.example.com.", "Type" : "CNAME", "TTL" : "900", "SetIdentifier" : "Frontend One", "Weight" : "140", "ResourceRecords" : ["example-ec2.amazonaws.com"] }, { "Name" : "mysite.example.com.", "Type" : "CNAME", "TTL" : "900", "SetIdentifier" : "Frontend Two", "Weight" : "60", "ResourceRecords" : ["example-ec2-larger.amazonaws.com"] } ] } }

YAML

myDNSOne: Type: AWS::Route53::RecordSetGroup Properties: HostedZoneName: example.com. Comment: Weighted RR for my frontends. RecordSets: - Name: mysite.example.com. Type: CNAME TTL: '900' SetIdentifier: Frontend One Weight: '140' ResourceRecords: - example-ec2.amazonaws.com - Name: mysite.example.com. Type: CNAME TTL: '900' SetIdentifier: Frontend Two Weight: '60' ResourceRecords: - example-ec2-larger.amazonaws.com

用 RecordSetGroup 來設定別名資源記錄集

下列範例使用AWS::Route53::RecordSet群組來設定名為的別名資源記錄集,example.com該記錄集會將流量路由至 ELB 第 1 版 (傳統) 負載平衡器和版本 2 (應用程式或網路) 負載平衡器。此AliasTarget屬性會使用GetAtt內建函式 LoadBalancer 來指定 MyELB 的託管區域識別碼和 DNS 名稱。 GetAtt檢索 MyELB 資源的不同屬性,具體取決於您是將流量路由到版本 1 還是版本 2 負載平衡器:

  • 第 1 版負載平衡器:CanonicalHostedZoneNameIDDNSName

  • 第 2 版負載平衡器:CanonicalHostedZoneIDDNSName

如需有關別名資源記錄集的詳細資訊,請參閱《Route 53 開發人員指南》中的選擇別名或非別名記錄

適用於第 1 版負載平衡器的 JSON

"myELB" : { "Type" : "AWS::ElasticLoadBalancing::LoadBalancer", "Properties" : { "AvailabilityZones" : [ "us-east-1a" ], "Listeners" : [ { "LoadBalancerPort" : "80", "InstancePort" : "80", "Protocol" : "HTTP" } ] } }, "myDNS" : { "Type" : "AWS::Route53::RecordSetGroup", "Properties" : { "HostedZoneName" : "example.com.", "Comment" : "Zone apex alias targeted to myELB LoadBalancer.", "RecordSets" : [ { "Name" : "example.com.", "Type" : "A", "AliasTarget" : { "HostedZoneId" : { "Fn::GetAtt" : ["myELB", "CanonicalHostedZoneNameID"] }, "DNSName" : { "Fn::GetAtt" : ["myELB","DNSName"] } } } ] } }

適用於第 1 版負載平衡器的 YAML

myELB: Type: AWS::ElasticLoadBalancing::LoadBalancer Properties: AvailabilityZones: - "us-east-1a" Listeners: - LoadBalancerPort: '80' InstancePort: '80' Protocol: HTTP myDNS: Type: AWS::Route53::RecordSetGroup Properties: HostedZoneName: example.com. Comment: Zone apex alias targeted to myELB LoadBalancer. RecordSets: - Name: example.com. Type: A AliasTarget: HostedZoneId: !GetAtt 'myELB.CanonicalHostedZoneNameID' DNSName: !GetAtt 'myELB.DNSName'

適用於第 2 版負載平衡器的 JSON

"myELB" : { "Type" : "AWS::ElasticLoadBalancing::LoadBalancer", "Properties" : { "Subnets" : [ {"Ref": "SubnetAZ1"}, {"Ref" : "SubnetAZ2"} ] } }, "myDNS" : { "Type" : "AWS::Route53::RecordSetGroup", "Properties" : { "HostedZoneName" : "example.com.", "Comment" : "Zone apex alias targeted to myELB LoadBalancer.", "RecordSets" : [ { "Name" : "example.com.", "Type" : "A", "AliasTarget" : { "HostedZoneId" : { "Fn::GetAtt" : ["myELB", "CanonicalHostedZoneID"] }, "DNSName" : { "Fn::GetAtt" : ["myELB","DNSName"] } } } ] } }

適用於第 2 版負載平衡器的 YAML

myELB: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: Subnets: - Ref: SubnetAZ1 - Ref: SubnetAZ2 myDNS: Type: AWS::Route53::RecordSetGroup Properties: HostedZoneName: example.com. Comment: Zone apex alias targeted to myELB LoadBalancer. RecordSets: - Name: example.com. Type: A AliasTarget: HostedZoneId: !GetAtt 'myELB.CanonicalHostedZoneID' DNSName: !GetAtt 'myELB.DNSName'

CloudFront 分配的別名資源記錄集

下列範例會建立別名記錄集,將查詢路由傳送至指定的 CloudFront 分佈。

注意

當您建立別名資源紀錄集時,您必須為 Z2FDTNDATAQYW2 屬性指定 HostedZoneId,如以下範例中所示。無法在私人區域中建立的別名資源記錄集。 CloudFront

JSON

{ "myDNS": { "Type": "AWS::Route53::RecordSetGroup", "Properties": { "HostedZoneId": { "Ref": "myHostedZoneID" }, "RecordSets": [ { "Name": { "Ref": "myRecordSetDomainName" }, "Type": "A", "AliasTarget": { "HostedZoneId": "Z2FDTNDATAQYW2", "DNSName": { "Fn::GetAtt": [ "myCloudFrontDistribution", "DomainName" ] } } } ] } } }

YAML

myDNS: Type: 'AWS::Route53::RecordSetGroup' Properties: HostedZoneId: !Ref myHostedZoneID RecordSets: - Name: !Ref myRecordSetDomainName Type: A AliasTarget: HostedZoneId: Z2FDTNDATAQYW2 DNSName: !GetAtt - myCloudFrontDistribution - DomainName