Sub topic 12
Custom domain names and HTTPS with ACM
This chapter assumes you can create a CloudFront distribution with an S3 origin, read IAM and JSON policies, and run AWS CLI commands. By default, a distribution answers on a name like d111111abcdef8.cloudfront.net. To serve www.example.com over HTTPS, you need three things: an alternate domain name on the distribution, a certificate from AWS Certificate Manager (ACM), and a DNS record that points the name at CloudFront.
The three pieces
- Alternate domain name (CNAME). A hostname you add to the distribution's settings, such as
www.example.com. CloudFront only accepts requests for hostnames it has listed. - ACM certificate. A TLS certificate that proves your server is the owner of the hostname. It must cover every alternate domain name on the distribution.
- DNS record. A record in your DNS zone that sends viewers to the distribution.
Request the certificate in us-east-1
CloudFront reads certificates only from the US East (N. Virginia) Region, us-east-1. This is true even if your bucket and the rest of your workload live elsewhere. A certificate requested in another Region does not appear in the CloudFront console.
aws acm request-certificate \
--region us-east-1 \
--domain-name www.example.com \
--subject-alternative-names example.com \
--validation-method DNSThe command returns a certificate ARN. ACM then gives you a CNAME record to create in your DNS zone. If your zone is in Route 53, the console can create it for you. After ACM sees the record, the status changes from PENDING_VALIDATION to ISSUED. Keep the validation record in place, because ACM uses it to renew the certificate automatically.
Attach the certificate and the name
In the distribution settings, add the hostnames under alternate domain names and select the issued certificate. In CloudFormation, these settings go in DistributionConfig:
Aliases:
- www.example.com
- example.com
ViewerCertificate:
AcmCertificateArn: arn:aws:acm:us-east-1:111122223333:certificate/EXAMPLE-ID
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2021sni-only uses Server Name Indication, which all current browsers support, and it adds no extra charge. Dedicated-IP custom SSL costs a monthly fee and is needed only for very old clients. Also confirm the viewer protocol policy on your cache behavior is redirect-to-https so plain HTTP requests are upgraded.
Point DNS at the distribution
Create a DNS record for each hostname that targets the distribution's domain name. In Route 53, use an alias record of type A (and AAAA if IPv6 is enabled). Alias records work at the zone apex, such as example.com, where a standard CNAME is not allowed. At other DNS providers, use a CNAME for subdomains like www.
Common errors
| Symptom | Likely cause |
|---|---|
| Certificate missing from the dropdown | Requested outside `us-east-1`, or not yet `ISSUED` |
| `CNAMEAlreadyExists` when saving | Another distribution already uses that hostname |
| Browser shows a name mismatch warning | The certificate does not cover the hostname |
| 403 from CloudFront on your domain | The hostname is not listed as an alternate domain name |
| Certificate stuck in `PENDING_VALIDATION` | The DNS validation record is missing or in the wrong zone |
Exercise
You requested an ACM certificate in eu-west-1 for your bucket's Region, but it does not appear when you edit the distribution. What is wrong, and how do you fix it?
Show answer
CloudFront only uses certificates from us-east-1. Request (or import) a new certificate in us-east-1, validate it, then select it in the distribution.
Exercise
Why should you use a Route 53 alias record instead of a CNAME for example.com?
Show answer
DNS does not allow a CNAME at the zone apex. A Route 53 alias record works at the apex and points to the CloudFront distribution.