Sub topic 12
Custom domain names and HTTPS with ACM
This chapter assumes you already have a working distribution with a private S3 origin, as built in the earlier chapters. You should also be comfortable with DNS records (A, AAAA, CNAME) and running AWS CLI commands. By the end you will serve the site from your own domain, such as www.example.com, over HTTPS with a certificate from AWS Certificate Manager (ACM).
Why the default domain is not enough
Every distribution gets a domain like d111111abcdef8.cloudfront.net. HTTPS already works on that name, because CloudFront serves its own default certificate. That certificate only matches *.cloudfront.net. If you point www.example.com at the distribution without further setup, browsers show a certificate mismatch, and CloudFront rejects the request because it does not know the hostname.
Two settings fix this:
- Alternate domain names (also called CNAMEs) tell the distribution which hostnames it may answer for.
- A viewer certificate from ACM proves to browsers that the distribution is allowed to speak for those hostnames.
You also need a DNS record that sends the hostname to the distribution.
How the pieces connect
The viewer-facing TLS connection ends at the edge location. The connection from CloudFront to S3 is a separate one, and with the S3 REST endpoint it always uses HTTPS.
Step 1: Request a certificate in us-east-1
CloudFront reads certificates only from the US East (N. Virginia) us-east-1 region, whatever region your bucket is in. This is the most common mistake in this setup. A certificate created in eu-west-1 will not appear in the distribution's certificate list.
Request a public certificate with DNS validation:
aws acm request-certificate \
--region us-east-1 \
--domain-name www.example.com \
--subject-alternative-names example.com \
--validation-method DNSThe command returns a CertificateArn. Public ACM certificates are free. You can list several names on one certificate, which is useful when the apex (example.com) and www share a distribution.
Wildcards need care. *.example.com covers www.example.com and app.example.com. It does not cover example.com itself or a.b.example.com. Add the apex as a second name if you need it.
Validate ownership
ACM asks you to prove you control the domain. With DNS validation, ACM gives you a CNAME record to create. Fetch it with:
aws acm describe-certificate \
--region us-east-1 \
--certificate-arn arn:aws:acm:us-east-1:111122223333:certificate/EXAMPLE-ID \
--query "Certificate.DomainValidationOptions[].ResourceRecord"Create each returned record in your DNS zone. In the Route 53 console, ACM can create them for you with one click. Then wait for issuance:
aws acm wait certificate-validated \
--region us-east-1 \
--certificate-arn arn:aws:acm:us-east-1:111122223333:certificate/EXAMPLE-IDEmail validation also exists, but it needs a person to answer an email on each renewal, so prefer DNS.
Step 2: Attach the domain and certificate to the distribution
Add Aliases and a ViewerCertificate to the distribution. This CloudFormation excerpt builds on the distribution from earlier chapters. The stack must be deployed in us-east-1 because it creates the certificate in the same template. Otherwise, pass in the ARN of a certificate you created in us-east-1.
Resources:
SiteCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: www.example.com
ValidationMethod: DNS
DomainValidationOptions:
- DomainName: www.example.com
HostedZoneId: Z0123456789EXAMPLE
SiteDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
Aliases:
- www.example.com
ViewerCertificate:
AcmCertificateArn: !Ref SiteCertificate
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2021
# Origins, DefaultCacheBehavior, and the other settings
# stay as defined in the earlier chapters.These three ViewerCertificate fields matter:
| Field | Meaning | Typical value |
|---|---|---|
| `AcmCertificateArn` | The ACM certificate in `us-east-1` that covers every alias | ARN of your certificate |
| `SslSupportMethod` | How CloudFront serves the certificate to clients | `sni-only` |
| `MinimumProtocolVersion` | Oldest TLS version and cipher set CloudFront accepts | `TLSv1.2_2021` |
SNI versus dedicated IP
sni-only uses Server Name Indication. The browser names the host it wants during the TLS handshake, so many sites can share the same edge IP addresses. Every current browser and HTTP client supports SNI. The alternative, vip, gives your distribution dedicated IP addresses at a high monthly fee. It exists only for very old clients that lack SNI. Choose sni-only unless you have measured traffic from such clients.
Alias rules that cause errors
- The certificate must cover every name in
Aliases. If it does not, the update fails with an error about a missing or mismatched certificate. - A given alias can belong to only one distribution at a time. Moving a name between distributions requires removing it from the old one first, or proving you own the domain through a covering certificate.
- Pick a minimum protocol policy deliberately. Raising it to a newer policy blocks older clients, so check your viewer logs before tightening it.
Step 3: Point DNS at the distribution
Create an alias record in Route 53 that targets the distribution's domain name. For CloudFront targets, the alias hosted zone ID is always Z2FDTNDATAQYW2.
SiteAliasA:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: Z0123456789EXAMPLE
Name: www.example.com
Type: A
AliasTarget:
DNSName: !GetAtt SiteDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
SiteAliasAAAA:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: Z0123456789EXAMPLE
Name: www.example.com
Type: AAAA
AliasTarget:
DNSName: !GetAtt SiteDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2Distributions have IPv6 enabled by default, so create both the A and AAAA records. Route 53 alias records to CloudFront cost nothing per query.
If your DNS is hosted elsewhere, create a CNAME from www.example.com to the d111111abcdef8.cloudfront.net name. The DNS standard forbids a CNAME at the zone apex (example.com). Providers that offer "ALIAS" or "ANAME" flattening can work around this. Otherwise, serve the site at www and redirect the apex.
Step 4: Force HTTPS for viewers
A certificate makes HTTPS available but does not require it. The viewer protocol policy on each cache behavior controls that. The distribution from earlier chapters already uses redirect-to-https, which answers plain HTTP requests with a 301 redirect to the HTTPS URL. The other choice, https-only, rejects HTTP with a 403 error. Redirecting is friendlier for people who type the bare address.
Verify and troubleshoot
Check the setup from the command line:
curl -sI https://www.example.com/ | head -n 5A healthy response starts with HTTP/2 200 and includes x-cache and via headers that mention CloudFront. For certificate details, use openssl s_client -connect www.example.com:443 -servername www.example.com. The -servername flag matters, because without it you skip SNI and get the default certificate.
| Symptom | Likely cause |
|---|---|
| Certificate does not appear in the distribution's dropdown or API call | Certificate was requested outside `us-east-1` |
| Certificate stays in "Pending validation" | Validation CNAME missing, mistyped, or created in the wrong DNS zone |
| `403` from CloudFront with an unknown host message | Hostname is not in `Aliases`, or DNS points at the wrong distribution |
| Browser reports a name mismatch | Certificate lacks the hostname, such as the apex or a second-level wildcard |
| `CNAMEAlreadyExists` on update | Another distribution already uses that alias |
| Old DNS answers after a change | Record TTL has not expired yet |
Exercise
You request an ACM certificate for www.example.com in eu-west-1, because your S3 bucket lives there. The distribution form shows no certificates to choose. What is wrong, and what do you do? Also, which names does a certificate for *.example.com fail to cover?
Show answer
CloudFront only uses ACM certificates from us-east-1. Request a new certificate for www.example.com in us-east-1, validate it with the DNS CNAME, and attach its ARN as AcmCertificateArn. The bucket's region does not matter. A *.example.com wildcard does not cover the apex example.com or names with more than one label before the domain, such as a.b.example.com. Add those as extra names on the certificate.
Exercise
A teammate sets SslSupportMethod: vip "to be safe" for a new public website. Why is that a poor default, and what should they use instead?
Show answer
vip assigns dedicated IP addresses and carries a high monthly charge. It only helps clients that do not support SNI, which are very rare today. They should use sni-only, which is included in normal CloudFront pricing and works with current browsers and HTTP clients.