本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
主題
使用託管區域名稱或 ID 的 Amazon Route 53 資源紀錄集
當您建立 Amazon Route 53 資源記錄集時,您必須指定要新增該記錄集的託管區域。CloudFormation 提供兩種指定託管區域的方式:
您可以使用
HostedZoneId
屬性來明確指定託管區域。您可以使用
HostedZoneName
屬性讓 CloudFormation 尋找託管區域。如果您使用HostedZoneName
屬性,而且有多個託管區域具有相同的名稱,CloudFormation 不會建立堆疊。
使用 HostedZoneId 新增 RecordSet
此範例新增了 Amazon Route 53 資源記錄集,其中包含mysite.example.com
使用 HostedZoneId
屬性指定託管區域的網域名稱SPF
記錄。
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"'
使用 HostedZoneName 新增 RecordSet
此範例使用 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::RecordSetGroup 設定 "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::RecordSetGroup 來設定名為 example.com
的別名資源記錄集,以將流量路由至 ELB 第 1 版 負載平衡器 (Classic Load Balancer) 以及第 2 版負載平衡器 (Application Load Balancer 或 Network Load Balancer)。AliasTarget 屬性myELB
LoadBalancer
會使用GetAtt
內部 函數指定 的託管區域 ID 和 DNS 名稱。 會GetAtt
擷取myELB
資源的不同屬性,取決於您要將流量路由到第 1 版或第 2 版負載平衡器:
第 1 版負載平衡器:
CanonicalHostedZoneNameID
和DNSName
第 2 版負載平衡器:
CanonicalHostedZoneID
和DNSName
如需有關別名資源記錄集的詳細資訊,請參閱《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