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 policyRedirect HTTP to HTTPS. - Under
Alternate domain name (CNAME) - optionalclickAdd itemand specify the desired domain name on which the site will be hosted. Eg:www.example.com - Under
Custom SSL certificate - optionalchoose the certificate that was previously created for this domain. - Under
Default root object - optionalenterindex.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
wwwforRecord name. - Leave default
Record typeofArecord. - Under
ValuesetAliasto true.- Under
Choose endpointselectAlias to CloudFront distribution. - In
Choose distributionfield select the content distribution.
- Under
- Leave other defaults.
- Click
Create records. - Click
Create recordto create a 2nd record for IPV6. - Enter
wwwforRecord name. - Choose
Record typeofAAAArecord. - Under
ValuesetAliasto true.- Under
Choose endpointselectAlias to CloudFront distribution. - In
Choose distributionfield 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
Propertiestab. - Under
Static website hostingclickedit. - Under
Static website hostingclickEnable. - Under
Hosting typeselectRedirect requests for an object. - In
Hostnamespecify the www subdomain address. Eg:www.example.com - For
Protocol - Optionalselecthttps. 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 policyleave 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) - optionalclickAdd itemand specify the naked domain. Eg:example.com - Under
Custom SSL certificate - optionalchoose the certificate that was previously created for this domain. - Under
Default root object - optionalleave 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 nameempty. - Leave default
Record typeofArecord. - Under
ValuesetAliasto true.- Under
Choose endpointselectAlias to CloudFront distribution. - In
Choose distributionfield select the redirect distribution.
- Under
- Leave other defaults.
- Click
Create records. - Click
Create recordto create a 2nd record for IPV6. - Leave
Record nameempty. - Choose
Record typeofAAAArecord. - Under
ValuesetAliasto true.- Under
Choose endpointselectAlias to CloudFront distribution. - In
Choose distributionfield select the redirect distribution.
- Under
- Leave other defaults.
- Click
Create records.
At this point all desired hosting and redirect behavior should be functioning.