主题
使用托管区名称或 ID 的 Amazon Route 53 资源记录集
创建 Amazon Route 53 资源记录集时,必须指定要添加该记录集的托管区。CloudFormation 提供了两种指定托管区的方法:
您可以使用
HostedZoneId
属性明确指定托管区域。您可以使用
HostedZoneName
属性让 CloudFormation 查找托管区。如果使用HostedZoneName
属性且存在多个名称相同的托管区,则 CloudFormation 不会创建堆栈。
使用 HostedZoneId 添加 RecordSet
此示例添加了一个 Amazon Route 53 资源记录集,其中包含域名 mysite.example.com
的 SPF
记录(使用 HostedZoneId
属性指定托管区)。
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
)。路由到资源的 Internet 流量比例基于以下计算:
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(经典)负载均衡器和版本 2(应用程序或网络)负载均衡器。AliasTarget 属性使用 GetAtt
内置函数指定 myELB
LoadBalancer
的托管区 ID 和 DNS 名称。GetAtt
将根据是要将流量路由到版本 1 还是版本 2 的负载均衡器来检索 myELB
资源的不同属性。
版本 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