I recently deployed a static website to AWS using S3, CloudFront, Route 53 and the AWS Certificate manager. There we a few 'gotchas' in there so I thought I'd outline by process here for others:
Firstly, I created a free AWS SSL Certificate. It's pretty straight forward, just go to the 'Certificate Manager' service in AWS then:
- Make sure you change regions to US East (N. Virginia) irrespective of which region you're setting anything else up in. CloudFront is 'regionless' and this is the region CloudFront uses for it's certificates.
- Click 'Request Certificate'
- Add the apex domain for the certificate (ie example.com)
- Add an additional domain and use a 'wild card' (ie *.example.com)
- Review and request, just follow the prompts and make sure you have access to one of the required email addresses so you can validate the request (which you should also do).
- Go into S3 and create a new bucket, using your site's apex domain as the bucket name (ie example.com).
- Make sure it's publicly accessible.
- Upload your files
- Go to the bucket's 'Properties' and select 'Static website hosting'
- Select 'Use this bucket to host a website' and add an index document (usually index.html)
- Take note of the Endpoint URL on this screen, that's your CloudFront endpoint (not the one that auto-completes for you in CloudFront).
- Create an empty S3 bucket with a 'www' subdomain (ie www.example.com).
- This bucket should be empty, however under Properties > Static website hosting you should select 'Redirect requests' and point it to the first bucket you made with the 'Protocol' set to 'https'.
- For bonus points, select all the files you just uploaded to the first S3 bucket, and click 'Add meta data' in the 'more' dropdown. Select
'Cache-Control' as a key and add a value of
max-age=7776000(90 days or whatever value you want)
Of course you can do this the other way around with the 'www' bucket being the main one and the apex domain one redirecting to it.
Now over to CloudFront:
- Click 'Create Distribution'
- Select 'Get Started' under 'Web'
- Set 'Origin Name' to the URL you made note of earlier for your S3 bucket.
- Set 'Viewer Protocol Policy' to 'Redirect HTTP to HTTPS'
- Under 'distribution settings' add 'Alternate Domain Names' with a comma separated list of your two domain names (ie example.com,www.example.com)
- Set the 'Default Root Object' to
- Finally, enable Gzip by going to 'behaviours' for your distribution and make sure that 'Compress Objects Automatically' is set to 'yes'
Every other setting can be left as the default.
It can take a good 20 mins to half an hour for CloudFront instances to become available. When it does though, your CloudFront domain will work (ie d2srergsg4h3ts.cloudfront.net)
Last step is your DNS configuration. You can just make a CName entry and point it to your CloudFront domain. However DNS will not allow you to make a CName entry for an Apex record. Route 53 will however.
- Go into Route53 and click 'hosted zones' then 'Create Hosted Zone'
- Enter URL and hit 'create'.
- Go into your hosted zone and click 'Create Record Set'.
- Leave 'Name' blank and 'Type' 'A - IP Address', then 'Alias' should be 'Yes'
- In 'Alias Target' your CloudFront distribution should be available in the alias dropdown list. Select 'Create'.
- Create another record set, but this time the 'Name' should be 'www', the 'Alias' should be 'yes' and the Alias target should be the record your just created in step 5 under the heading 'Records set in this hosting zone'.
- Click create.
Now you just need to make the appropriate adjustments to you Nameservers so they point to the Route53 Nameservers and you've got a very cheaply hosted website that is super fast and secure.
Big thanks to this article which got me 90% of the way.