Sitemap

How to apply SSL for HTTPS on a WebApp hosted in AWS S3 Bucket

6 min readDec 13, 2022

So you are ready to publish your web application to be available to the world, but you notice the URL is only serving HTTP traffic?

Press enter or click to view image in full size
Overview Diagram

Steps Overview

  1. Register domain name using Route 53 (or point your name servers to AWS)
  2. Generate an SSL certificate for your domain
  3. Create an S3 bucket with the SAME NAME as you want your domain to show (example.com)
  4. Create CloudFront Distribution that points to the S3 bucket with the SAME NAME you want your traffic routed to (using the HTTP link, not selecting the S3 bucket)
  5. Create an A record in Route 53 that points to the CloudFront Distribution previously created in step 4

Steps Breakdown

Step 1- Register domain name using Route 53 (or point your name servers to AWS)

If you don’t have an AWS account, create a free account here https://aws.amazon.com/free/

  1. Go to Route 53
Press enter or click to view image in full size
Search for Route 53 within services

2. Create a Hosted Zone

Press enter or click to view image in full size
Name it the same as your domain name
Press enter or click to view image in full size
Check if the domain name is available and register it

Or point the domain name servers to AWS

I bought this domain a while back, I know is Corny but hey is a domain to show you lol

Press enter or click to view image in full size
For example this domain was registered with Google but is pointing to AWS name servers
Press enter or click to view image in full size
Add the AWS Name servers

Step 2 - Generate an SSL certificate for your domain

  1. Go to AWS Certificate Manager and request a Certificate
Press enter or click to view image in full size
Click Request
Press enter or click to view image in full size
Click Next
Press enter or click to view image in full size
Type in your domain name, and leave all the defaults, then press next.

Once requested, this might take a long time, it took around 1 hour for me, but in the meantime, you could continue on the next steps.

Step 3 - Create an S3 bucket with the SAME NAME as you want your domain to show (example.com)

  1. Go to S3 AWS Service
Press enter or click to view image in full size
Click on Create Bucket

2. Name your bucket the same as your domain

Press enter or click to view image in full size

3. For Block Public Access Settings for this bucket — option, unselect Block all public access, and check the Acknowledge

Press enter or click to view image in full size
Check the I acknowledge checkbox

4. Leave the rest options as default and click on Create Bucket

Press enter or click to view image in full size
Click on Create Bucket

5. Go into the bucket and select properties

Press enter or click to view image in full size
Select Properties

6. We need to Enable this S3 bucket to host a static website, Scroll all the way down to the bottom where it says Static Website Hosting.

Then click on edit to enable

Press enter or click to view image in full size
Click on Edit

7. Once we enable Static Website Hosting we need to select which pages will be the default (type index.html) for both fields

Press enter or click to view image in full size

8. Next we have to make Objects Public we do this by applying a bucket policy, by going to Permissions and then clicking on Edit under Bucket Policy

Press enter or click to view image in full size

Then enter the following JSON object, but notice you have to put your own bucket name under the Resource

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your-bucket-name-here/*"
}
]
}

The bucket Policy should look like this:

Press enter or click to view image in full size
This allows all to be able to get items from this bucket — making it public

9. Upload your website content to the S3 bucket

If you are using a React Application you have to Upload the contents of the BUILD folder, which contains the index.html

If you don’t have a React App create one like this

# create react app
$ npx create-react-app my-cool-app

# create build of the react app, this will create a build folder with an index.html within
$ npm run build

Upload the contents of the BUILD folder NOT the BUILD folder itself!

Press enter or click to view image in full size
Run `npm run build` then open the location where the build folder was created

10. Test the URL provided by the S3 bucket, go to the S3 bucket, select properties, and scroll all the way down

Press enter or click to view image in full size
Click on the link
Press enter or click to view image in full size
Then your Web Application should show up on the link

Step 4 — Create CloudFront Distribution that points to the S3 bucket with the SAME NAME you want your traffic routed to (using the HTTP link, not selecting the S3 bucket)

  1. Go search for CloudFront and click on Create Distribution
Press enter or click to view image in full size
Click on Create Distribution

For Origin Domain, paste in the URL provided by the S3 bucket (without the HTTP://)

DO NOT select the S3 BUCKET from the dropdown

Press enter or click to view image in full size
Paste the URL that was provided by the S3 bucket (without the HTTP)
Press enter or click to view image in full size
Select Redirect HTTP to HTTPS and select GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
Press enter or click to view image in full size
Make sure to add an Alternative Domain Name CNAME — NOT OPTIONAL

Make sure to add an Alternative Domain Name CNAME — NOT OPTIONAL if you want the A Record to be able to point to it

Press enter or click to view image in full size
Then click on Create Distribution

Then copy the Distribution Domain Name and it should show the content from the S3 bucket, in this case, is the web application we created earlier

Press enter or click to view image in full size
Visit the Distribution Domain Name and it should show the web application in the S3 bucket

Step 5— Create an A record in Route 53 that points to the CloudFront Distribution previously created in step 4

  1. Go to Route 53 service and create a Hosting Zone
Press enter or click to view image in full size
Create a Hosting Zone

Select Define Simple Record — Choose A type, select Alias to CloudFront Distribution then the CloudFront Distribution record should show up in the list

Press enter or click to view image in full size
Define Record
Define Simple Record

Then finally when anyone visits your domain name it should display the contents of your S3 bucket using HTTPS

For example my domain name codewithdad.com

Press enter or click to view image in full size
Notice the HTTPS in the lock in front of the domain name

--

--

JonathanSanchez.Dev
JonathanSanchez.Dev

Written by JonathanSanchez.Dev

Developer by day, rockin' drummer by night. Loves dogs🐶 and collaborating with fellow engineers. Passionate, creative, and always up for a coding adventure! 🚀

Responses (2)