Skip to content

Instantly share code, notes, and snippets.

@theprojectsomething
Last active May 20, 2017 12:02
Show Gist options
  • Save theprojectsomething/45c47703156e3e983e505d50ccd49966 to your computer and use it in GitHub Desktop.
Save theprojectsomething/45c47703156e3e983e505d50ccd49966 to your computer and use it in GitHub Desktop.
Create a Serverless SSL Redirect with AWS

AWS Serverless SSL Redirect

Creating a serverless redirect requires four AWS services:

  • S3
  • Certificate Manager
  • Cloudfront
  • Route 53

Before You Start

Use the template below to create index.html. Include the URL you are redirecting to in place of {{ URL }}

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="refresh" content="0; url='{{ URL }}'">
	<link rel="canonical" href="{{ URL }}" />
</head>
<body onload="javascript:window.location='{{ URL }}'"></body>
</html>

S3

  1. Create a bucket with same name as your url e.g. 'example.com'
  2. Upload index.html into the root of the bucket, making sure the world has read access to the file
  3. In the bucket properties set Static Website Hosting to 'Enable Website Hosting' and both documents to 'index.html'

Certificate Manager

  1. Open Certificate Manager and change your region to 'US East (N. Virginia)' .. this region is required for Cloudfront (step 3.)
  2. Request a Certificate for your domain name, you can also include 'www' as an additional domain e.g. 'example.com' with additional: 'www.example.com'
  3. The owner of the domain will be sent an email asking them to confirm the request, get them to do that
  4. Once confirmed wait for the status against the certificate to change from 'Pending validation' to 'Issued'

Cloudfront

  1. Open Cloudfront, select Create Distribution and choose Get Started under Web
  2. Under Origin Settings click the Origin Domain Name input and select the previously created s3 bucket e.g. 'example.com.s3.amazonaws.com'
  3. Under Distribution Settings -> SSL Certificate choose Custom SSL Certificate and select the previously requested cert (only US East certs work here)
  4. If you included 'www' as an additional name on the certificate, include this in the Alternative Domain Names input above e.g. 'www.example.com'
  5. Type 'index.html' into the Default Root Object input
  6. Click the Create Distribution button at the bottom of the form, once the status has changed from 'In Progress' to 'Deployed' go to the next step

Route 53

  1. Your domain needs to be managed in Route 53, if it isn't you'll need to start the transfer
  2. If your domain doesn't already have a hosted zone, create one (it only needs NS records for now)
  3. Update/create a new 'A - IPv4 address'
  4. Under Alias select 'yes' and in the Alias Target input choose the previously created Cloudfront distribution (you may have to wait for it to load)
  5. Repeat the steps above for the 'www' subdomain if you have one (including 'www' in the name input this time)

Congrats, you're done

Test your redirects by browsing to the http and https versions of your domain in a private browser (if you're experiencing DNS caching try viewing the source of the url with caching disabled in Chrome or Firefox - you should see the contents of index.html)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment