Route 53 Template Snippets
Topics
Amazon Route 53 Resource Record Set Using Hosted Zone Name or ID
When you create an Amazon Route 53 resource record set, you must specify the hosted zone where you want to add it. AWS CloudFormation provides two ways to do this. You can explicitly specify the hosted zone using the HostedZoneId property or have AWS CloudFormation find the hosted zone using the HostedZoneName property. If you use the HostedZoneName property and there are multiple hosted zones with the same domain name, AWS CloudFormation doesn't create the stack.
Adding RecordSet using HostedZoneId
This example adds an Amazon Route 53 resource record set containing an SPF record
for the domain name mysite.example.com that uses the
HostedZoneId property to specify the hosted zone.
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"'
Adding RecordSet using HostedZoneName
This example adds an Amazon Route 53 resource record set containing A records for the domain name "mysite.example.com" using the HostedZoneName property to specify the hosted zone.
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
Using RecordSetGroup to Set Up Weighted Resource Record Sets
This example uses an AWS::Route53::RecordSetGroup to set up two CNAME records for the
"example.com." hosted zone. The RecordSets property contains
the CNAME record sets for the "mysite.example.com" DNS name. Each record set
contains an identifier (SetIdentifier) and weight (Weight). The weighting for
Frontend One is 40% (4 of 10) and Frontend Two is 60% (6 of 10). For more
information about weighted resource record sets, see Setting Up Weighted Resource Record Sets in Route 53 Developer
Guide.
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" : "4", "ResourceRecords" : ["example-ec2.amazonaws.com"] }, { "Name" : "mysite.example.com.", "Type" : "CNAME", "TTL" : "900", "SetIdentifier" : "Frontend Two", "Weight" : "6", "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: '4' ResourceRecords: - example-ec2.amazonaws.com - Name: mysite.example.com. Type: CNAME TTL: '900' SetIdentifier: Frontend Two Weight: '6' ResourceRecords: - example-ec2-larger.amazonaws.com
Using RecordSetGroup to Set Up an Alias Resource Record Set
This example uses an AWS::Route53::RecordSetGroup to set up an alias resource record set for the "example.com." hosted zone. The RecordSets property contains the A record for the zone apex "example.com." The AliasTarget property specifies the hosted zone ID and DNS name for the myELB LoadBalancer by
using the GetAtt intrinsic function to retrieve the CanonicalHostedZoneNameID and DNSName properties
of myELB resource. For more information about alias resource record sets, see Creating Alias Resource Record Sets in the Route 53 Developer Guide.
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"] } } } ] } }
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
Alias Resource Record Set for a CloudFront Distribution
The following example creates an alias record set that routes queries to the specified CloudFront distribution domain name.
Note
When you create alias resource record sets, you must specify Z2FDTNDATAQYW2 for the HostedZoneId property, as shown in the following example.
Alias resource record sets for CloudFront can't be created in a private zone.
JSON
"myDNS" : { "Type" : "AWS::Route53::RecordSetGroup", "Properties" : { "HostedZoneId" : { "Ref" : "myHostedZoneID" }, "RecordSets" : [{ "Name" : { "Ref" : "myRecordSetDomainName" }, "Type" : "A", "AliasTarget" : { "HostedZoneId" : "Z2FDTNDATAQYW2", "DNSName" : { "Ref" : "myCloudFrontDistributionDomainName" } } }] } }
YAML
myDNS: Type: AWS::Route53::RecordSetGroup Properties: HostedZoneId: Ref: myHostedZoneID RecordSets: - Name: Ref: myRecordSetDomainName Type: A AliasTarget: HostedZoneId: Z2FDTNDATAQYW2 DNSName: Ref: myCloudFrontDistributionDomainName
