July 6, 2021 . 19 MIN READ
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html
In CloudFront, an alternate domain name, also known as a CNAME, lets you use your own domain name (for example, www.example.com) in your files’ URLs instead of using the domain name that CloudFront assigns to your distribution. Both web and RTMP distributions support alternate domain names.
When you create a distribution, CloudFront returns a domain name for the distribution, for example:
d111111abcdef8.cloudfront.net
When you use the CloudFront domain name for your files, the URL for a file called /images/image.jpg is:
https://d111111abcdef8.cloudfront.net/images/image.jpg
If you want to use your own domain name, such as www.example.com, instead of the cloudfront.net domain name, you can add an alternate domain name to your distribution, like www.example.com. You can then use the following URL to view /images/image.jpg:
https://www.example.com/images/image.jpg
Topics
The following task list describes how to use the CloudFront console to add an alternate domain name to your distribution so that you can use your own domain name in your links instead of the CloudFront domain name. For information about updating your distribution using the CloudFront API, see Working with Distributions.
If you want viewers to use HTTPS with your alternate domain name, see Using Alternate Domain Names and HTTPS.
Before you begin: Make sure that you do the following before you update your distribution to add an alternate domain name:
Adding an Alternate Domain Name
https://www.example.com/logo.jpg). If you use your CloudFront distribution domain name (https://d111111abcdef8.cloudfront.net/logo.jpg), a viewer might behave as follows, depending on the value that you choose for Clients Supported:
For more information, see Choosing How CloudFront Serves HTTPS Requests.
If your DNS record already points to a distribution that is not the distribution that you are updating, then you only add the alternate domain name to your distribution after you update your DNS. For more information, see Restrictions on Using Alternate Domain Names.
If you already have an existing CNAME record for your domain name, update that record or replace it with a new one that points to the CloudFront domain name for your distribution.
In addition, confirm that your CNAME resource record set points to your distribution’s domain name and not to one of your origin servers.
dig or a similar DNS tool, confirm that the resource record set that you created in step 7 points to the domain name for your distribution.The following example shows a dig request on the www.example.com domain, as well as the relevant part of the response.
PROMPT> dig www.example.com
; <<> DiG 9.3.3rc2 <<> www.example.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15917
;; flags: qr rd ra; QUERY: 1, ANSWER: 9, AUTHORITY: 2, ADDITIONAL: 0
;; QUESTION SECTION:
;www.example.com. IN A
;; ANSWER SECTION:
www.example.com. 10800 IN CNAME d111111abcdef8.cloudfront.net. ...
The answer section shows a CNAME record that routes queries for www.example.com to the CloudFront distribution domain name d111111abcdef8.cloudfront.net. If the name on the right side of CNAME is the domain name for your CloudFront distribution, the CNAME record is configured correctly. If that is any other value, for example, the domain name for your Amazon S3 bucket, then the CNAME record is configured incorrectly. In that case, go back to step 4 and correct the CNAME record to point to the domain name for your distribution.
If you want to move an alternate domain name from one CloudFront distribution to another distribution, the steps you must take depend on the domain name that you want to move:
www.example.com, you can move the domain yourself. For detailed steps, see Move a subdomain name to another distribution.example.com (an apex domain), you must work with AWS Support to move the domain to another distribution Move an apex domain name to another distribution.Follow these steps to move a subdomain name, for example www.example.com.
To move a subdomain name to a new distribution
www.example.com, add the alternate domain name *.example.com. For more information, see Using Wildcards in Alternate Domain Names That You Add to CloudFront.
You can’t add a wildcard to a top-level domain name, such as *.com, so if you want to move a domain name like example.com to a new distribution, see Move an apex domain name to another distribution.
www.example.com to route traffic to the CloudFront domain name for your distribution, d111111abcdef8.cloudfront.net.To update the configuration, do one of the following:
At this point, the subdomain is still served by the original distribution because that is where the alternate domain is currently configured.
dig or a similar DNS tool, confirm that the resource record set that you created in step 4 points to the domain name for your distribution.The following example shows a dig request on the www.example.com domain, as well as the relevant part of the response.
PROMPT> dig www.example.com
; <<> DiG 9.3.3rc2 <<> www.example.com
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15917
;; flags: qr rd ra; QUERY: 1, ANSWER: 9, AUTHORITY: 2, ADDITIONAL: 0
;; QUESTION SECTION:
;www.example.com. IN A
;; ANSWER SECTION:
www.example.com. 10800 IN CNAME d111111abcdef8.cloudfront.net. ...
The answer section shows a CNAME record that routes queries for www.example.com to the CloudFront distribution domain name d111111abcdef8.cloudfront.net. If the name on the right side of CNAME is the domain name for your CloudFront distribution, the CNAME record is configured correctly. If that is any other value, for example, the domain name for your Amazon S3 bucket, then the CNAME record is configured incorrectly. In that case, go back to step 4 and correct the CNAME record to point to the domain name for your distribution.
When using Route 53 alias records, it is not possible to use dig to confirm that the resource record points to the new distribution. In this case, you can either change the type of the resource record from alias to CNAME, or wait until the record’s time to live (TTL) value has expired.
While these changes propagate, the alternate domain name may be served by either the original or new distribution at random. This behavior may persist for a few minutes after both distributions have reached status Deployed.
For apex domain names, like example.com, you must contact AWS Support to move the domain name to another CloudFront distribution. The extra steps are required because moving a domain yourself, as described in the previous procedure, requires setting up domain routing using a wildcard for part of the domain name. For apex domains, for this step, you would have to set up routing as *.com, which isn’t allowed.
Before you get started, if you don’t have a new distribution to move the domain name to, create one. For more information, see Creating a Distribution.
Moving a domain name like example.com to a new distribution takes two steps:
Step 1: Provide proof to AWS Support that you own the domain name by creating a TXT record for your domain at your DNS service provider. This helps prevent someone else from making changes to your distribution configuration.
Step 2: Request that AWS Support move your domain to the new CloudFront distribution.
Here are the specific steps to take.
Step 1: Create a TXT record for your domain
domain name TXT CloudFrontdistribution domain nameFor example: example.com TXT d111111abcdef8.cloudfront.net
If your service provider does not allow a TXT record for a domain to have the same information as a CNAME record, consider creating a TXT record that uses your domain name with an underscore (_) prepended to it. For an example, see the following Knowledge Center article: Resolve CNAME Already Exists Error.
Step 2: Request that AWS Support move your domain to the new CloudFront distribution
AWS Support can’t verify your domain ownership until they can view the TXT record that you created for your domain. Be aware that records that you create at your DNS provider can take a while (up to several days) to propagate through the DNS system.
If you want to stop routing traffic for a domain or subdomain to a CloudFront distribution, follow the steps in this section to update both the DNS configuration and the CloudFront distribution.
It’s important that you remove the alternate domain names from the distribution as well as update your DNS configuration. This helps prevent issues later if you want to associate the domain name with another CloudFront distribution. If an alternate domain name is already associated with one distribution, it can’t be set up with another.
If you want to remove the alternate domain name from this distribution so you can add it to another one, follow the steps in Moving an Alternate Domain Name to a Different CloudFront Distribution. If you follow the steps here instead (to remove a domain) and then add the domain to another distribution, there will be a period of time during which the domain won’t link to the new distribution because CloudFront is propagating to the updates to edge locations.
To remove an alternate domain name from a distribution
When you add alternate domain names, you can use the * wildcard at the beginning of a domain name instead of adding subdomains individually. For example, with an alternate domain name of *.example.com, you can use any domain name that ends with example.com in your object URLs, such as www.example.com, product-name.example.com, and marketing.product-name.example.com. The name of an object is the same regardless of the domain name, for example:
www.example.com/images/image.jpg
product-name.example.com/images/image.jpg
marketing.product-name.example.com/images/image.jpg
Follow these requirements for alternate domain names that include wildcards:
*. ).*domain.example.com.subdomain.*.example.com.A wildcard alternate domain name, such as *.example.com, can include another alternate domain name, such as example.com, as long as they’re both in the same CloudFront distribution or they’re in distributions that were created by using the same AWS account.
When you add an alternate domain name, such as www.example.com, to a CloudFront distribution, the following are requirements:
A trusted certificate is one that is issued by AWS Certificate Manager (ACM) or by another valid certificate authority (CA); you can’t use a self-signed certificate. CloudFront supports the same certificate authorities as Mozilla. For the current list, see Mozilla Included CA Certificate List.
To verify an alternate domain name by using the certificate that you attach, including alternate domain names that include wildcards, CloudFront checks the subject alternative name (SAN) on the certificate. The alternate domain name that you’re adding must be covered by the SAN.
Only one certificate can be attached to a CloudFront distribution at a time.
You prove that you are authorized to add a specific alternate domain name to your distribution by doing one of the following:
product-name.example.com.The following examples illustrate how using wildcards in domain names in a certificate work to authorize you to add specific alternate domain names in CloudFront.
marketing.example.com as an alternate domain name. You list in your certificate the following domain name: *.example.com. When you attach this certificate to CloudFront, you can add any alternate domain name for your distribution that replaces the wildcard at that level, including marketing.example.com. You can also, for example, add the following alternate domain names:
product.example.comapi.example.comHowever, you can’t add alternate domain names that are at levels higher or lower than the wildcard. For example, you can’t add the alternate domain names example.com or marketing.product.example.com.
example.com as an alternate domain name. To do this, you must list the domain name example.com itself on the certificate that you attach to your distribution.marketing.product.example.com as an alternate domain name. To do this, you can list *.product.example.com on the certificate, or you can list marketing.product.example.com itself on the certificate.Note the following restrictions on using alternate domain names:
However, you can add a wildcard alternate domain name, such as *.example.com, that includes (that overlaps with) a non-wildcard alternate domain name, such as www.example.com. Overlapping alternate domain names can be in the same distribution or in separate distributions as long as both distributions were created by using the same AWS account.
If you have overlapping alternate domain names in two distributions, CloudFront sends the request to the distribution with the more specific name match, regardless of the distribution that the DNS record points to. For example, marketing.domain.com is more specific than *.domain.com.
To correct this, sign in to your DNS provider and remove the existing DNS record, or contact your DNS provider to remove it for you. Then create the correct DNS record for your distribution, following the steps for adding or changing the alternate domain name for a distribution. For more information, see Adding an Alternate Domain Name or Moving an Alternate Domain Name to a Different CloudFront Distribution.
www.example.com, and then issue a request for www.example.org.
To prevent cases where domain fronting crosses different AWS accounts, CloudFront makes sure that the AWS account that owns the certificate that it serves for a specific connection always matches the AWS account that owns the request that it handles on that same connection.
If the two AWS account numbers do not match, CloudFront responds with an HTTP 421 Misdirected Request response to give the client a chance to connect using the correct domain.
example.com, the zone apex is example.com. You can’t create a CNAME record for example.com, but you can create CNAME records for www.example.com, newproduct.example.com, and so on.
If you’re using Route 53 as your DNS service, you can create an alias resource record set, which has two advantages over CNAME records. You can create an alias resource record set for a domain name at the top node (example.com). In addition, when you use an alias resource record set, you don’t pay for Route 53 queries.
If you enable IPv6, you must create two alias resource record sets: one to route IPv4 traffic (an A record) and one to route IPv6 traffic (an AAAA record). For more information, see Enable IPv6 in the topic Values That You Specify When You Create or Update a Distribution.
https://forums.aws.amazon.com/thread.jspa?threadID=54187
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html
https://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html#VirtualHostingCustomURLs