Hosting on AWS with (Free) SSL
It's possible to easily host on AWS with SSL from AWS ACM (Amazon Certificate Manager) that is free. This holds true regardless of who the domain registrar is.
- AWS S3 stores the index.html and other static files.
- AWS Cloudfront is the CDN that serves the site.
- AWS Cloudfront helps redirect HTTP to HTTPS (for www subdomain).
- AWS S3 helps redirect naked domain to www subdomain, always to HTTPS.
- AWS Route 53 Hosted Zone provides DNS.
- AWS ACM provides SSL.
This guide shows how to achieve the following setup for a given domain.
- http://example.com --redirects--> https://www.example.com
- https://example.com --redirects--> https://www.example.com
- http://www.example.com --redirects--> https://www.example.com
- https://www.example.com --serves--> index.html stored in S3 bucket, served and cached by CloudFront CDN.
Steps to Set Up
Once everything is setup, 2 Cloudfront distributions and 2 S3 buckets will exist.
Create AWS Route 53 Hosted Zone
The domain must have an AWS Route 53 Hosted Zone in order to serve DNS records.
- Create the AWS Route 53 Hosted Zone.
- If the domain is not registered with AWS as the domain registrar, update the domain registrar to use the Hosted Zone nameserver records as the nameservers.
Create AWS ACM SSL Certificate
To support SSL for HTTPS, an AWS ACM Certificate must exist.
- Start requesting/creating a new ACM certificate.
- Set all necessary names, eg:
- example.com
- www.example.com
- *.example.com
- Leave default option of DNS validation selected.
- Click 'Request' to start the process of acquiring the certificate.
- Open the certificate back up for editing.
- Click
Create records in Route 53
. - Only once ACM certificate shows the status for each domain switching from 'Pending validation' to '' will the certificate be ready. If the nameservers were recently switched for this domain it may take some time.
Create S3 "Content Bucket"
To store the index.html and other static content/files that comprise the site, an S3 bucket is used.
- Create an S3 bucket, with any desired name, eg:
example.com-content
. - Make publicly accessible with website hosting enabled.
- Create Bucket Policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example.com-content/*"
}
]
}
- Leave other options as defaults. Create the bucket.
- Upload static files to the bucket.
Note that this bucket stores files but does not allow them to be accessed publicly directly.
Create CloudFront "Content Distribution"
To actually serve the static file stored in the content bucket (created previously), AWS Cloudfront will provide the CDN that does so.
- Create a new Cloudfront distribution.
- For
Origin domain
, use the previously created S3 bucket's website address, eg:example.com-content.s3-website-us-east-1.amazonaws.com
- Under
Viewer protocol policy
Redirect HTTP to HTTPS
. - Under
Alternate domain name (CNAME) - optional
clickAdd item
and specify the desired domain name on which the site will be hosted. Eg:www.example.com
- Under
Custom SSL certificate - optional
choose the certificate that was previously created for this domain. - Under
Default root object - optional
enterindex.html
. - Leave all other options as defaults.
- Click
Create distribution
.
Create Route 53 Records for WWW Subdomain DNS
Create the DNS records to map www subdomain to CloudFront distribution.
- Edit the Route 53 Hosted Zone for the domain.
- Click
Create record
. - Enter
www
forRecord name
. - Leave default
Record type
ofA
record. - Under
Value
setAlias
to true.- Under
Choose endpoint
selectAlias to CloudFront distribution
. - In
Choose distribution
field select the content distribution.
- Under
- Leave other defaults.
- Click
Create records
. - Click
Create record
to create a 2nd record for IPV6. - Enter
www
forRecord name
. - Choose
Record type
ofAAAA
record. - Under
Value
setAlias
to true.- Under
Choose endpoint
selectAlias to CloudFront distribution
. - In
Choose distribution
field select the content distribution.
- Under
- Leave other defaults.
- Click
Create records
.
At this point the following observations should hold true:
- Accessing http://example.com will serve nothing.
- Accessing https://example.com will serve nothing.
- Accessing http://www.example.com will redirect to https://www.example.com.
- Accessing https://www.example.com will serve the index.html file from the S3 content bucket.
Create S3 "Redirect Bucket"
An S3 Bucket is capable of redirecting requests (as opposed to providing files). This redirect behavior is used to redirect from the naked domain to the www subdomain.
- Create an S3 bucket, with any desired name, eg:
example.com-redirect
. - Deselect
Block all public access
. - Leave other options as defaults. Create the bucket.
- Open the bucket back up for editing.
- Go to the
Properties
tab. - Under
Static website hosting
clickedit
. - Under
Static website hosting
clickEnable
. - Under
Hosting type
selectRedirect requests for an object
. - In
Hostname
specify the www subdomain address. Eg:www.example.com
- For
Protocol - Optional
selecthttps
. This will prevent an unnecessary extra intermediate redirect across the http-based address.
Create Cloudfront "Redirect Distribution"
To actually serve the redirect on HTTPS, the S3 redirect bucket must be served by a CloudFront distribution.
- Create a new Cloudfront distribution.
- For
Origin domain
, specify the website endpoint, eg:http://example.com-redirect.s3-website-us-east-1.amazonaws.com
.- This website endpoint can be found under the bucket's properties, after enabling
Static website hosting
.
- This website endpoint can be found under the bucket's properties, after enabling
- Under
Viewer protocol policy
leave the defaultHTTP and HTTPS
. This is so that the S3 bucket can directly redirect http://example.com to https://www.example.com. - Under
Alternate domain name (CNAME) - optional
clickAdd item
and specify the naked domain. Eg:example.com
- Under
Custom SSL certificate - optional
choose the certificate that was previously created for this domain. - Under
Default root object - optional
leave it empty. - Leave all other options as defaults.
- Click
Create distribution
.
Create Route 53 Records for WWW Subdomain DNS
Create the DNS records to map www subdomain to CloudFront distribution.
- Edit the Route 53 Hosted Zone for the domain.
- Click
Create record
. - Leave
Record name
empty. - Leave default
Record type
ofA
record. - Under
Value
setAlias
to true.- Under
Choose endpoint
selectAlias to CloudFront distribution
. - In
Choose distribution
field select the redirect distribution.
- Under
- Leave other defaults.
- Click
Create records
. - Click
Create record
to create a 2nd record for IPV6. - Leave
Record name
empty. - Choose
Record type
ofAAAA
record. - Under
Value
setAlias
to true.- Under
Choose endpoint
selectAlias to CloudFront distribution
. - In
Choose distribution
field select the redirect distribution.
- Under
- Leave other defaults.
- Click
Create records
.
At this point all desired hosting and redirect behavior should be functioning.