Hosting a Website on AWS
Hosting Options
- Hosting on Your Own Computer
- AWS Route 53 + AWS S3
- External Registrar + AWS Route 53 + AWS S3
- External Registrar + AWS Route 53 + AWS S3 + AWS Certificate Manager + AWS CloudFront + GitHub Actions
- Github Pages
- Netlify
Introduction
The instructions outlined below are for hosting a static website (ie, no server side code). It assumes you will have the following file types in your repository:
- HTML
- CSS
- JavaScript
Option 3: External Registrar + AWS Route 53 + AWS S3 + AWS Certificate Manager + AWS CloudFront + GitHub Actions
Option 3 - Tools and Accounts
Make sure you have setup accounts for the tools below. All AWS products will only require the setup of one root user login. Github and any external domain purchase is separate.
- Registered Domain through AWS Route 53 or An External Registrar (GoDaddy etc.)
- AWS S3
- AWS CloudFront
- GitHub Account
- AWS CodePipeline
- AWS Certificate Manager
Option 3 - Steps
- Signup and/or Login to AWS Console
- If you don’t already have one, sign-up for an Amazon Web Services Account
- Login to the AWS Console
- Signup and/or Login to GitHub
- If you don’t already have one, sign-up up for a Github Account
- Github Desktop - download the appropriate version of GitHub Desktop
- Note that these steps assume a Project Site and not a User or Organization site on GitHub. More information on the difference between the three can be found on the Get Started Guide in GitHub Pages
- Create a New Repository in Github
- Choose one of the following methods:
- Login to GitHub and create a new public repository.
- Create a new repository with GitHub Desktop.
- Choose one of the following methods:
- Add Folders and Files to Repository
- Create the following folder in your repository:
- /css
- /js
- /img
- Add the following files to the root of your repository:
- index.html
- error.html
- You can use any type of HTML you want for your index.html file or copy the following which is a modified version of this template
- Create the following folder in your repository:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<script src="js/index.js"></script>
</body>
</html>
- Here is a boilerplate template for the error.html from here
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<link rel="stylesheet" href="css/style.css">
</head>
<bod>
<h1>Page Not Found</h1>
<p>Sorry, but the page you were trying to view does not exist.</p>
</body>
</html>
-
You can also add
index.js
to your /js directory andstyles.css
to your /css directory. -
Your Repository should look something like this:
.
|-- /css
|-- styles.css
|-- /img
|-- /js
|-- index.js
|-- index.html
|-- error.html
- If your file structure looks totally different, don’t worry about it. We will change it in the future - just make sure that the index.html and error.html files are in the root directory.
- Register for a Domain Name
- Purchase and setup your domain name through your preferred registrar and login to the DNS Console in a separate tab.
- This example assumes the domain is registered with GoDaddy.
- Make note of your new domain which should be something like
https://example-site.com
.
- Purchase and setup your domain name through your preferred registrar and login to the DNS Console in a separate tab.
- Create a New S3 Bucket
- Login to the AWS S3 console
- Follow the AWS Docs Tutorial on Configuring a static website on Amazon S3.
- Make sure you Add a bucket policy that makes your bucket content publicly available
- Make sure that you DISABLE your S3 bucket’s block public access settings
- Make note of your S3 bucket’s endpoint without the leading http://.
- For our example, the S3 bucket website endpoint should be something like
http://example-site.com.s3-website.us-east-2.amazonaws.com
.
- Upload Files to Your New S3 Bucket
- Recall in Step 4, you created index.html and error.html. Upload both files to your New S3 Bucket.
- For now, just upload the index.html and error.html files and not anything else. In a later step, we will connect the repository to our S3 bucket for a seamless workflow.
- Follow the AWS S3 Docs To upload folders and files to an S3 bucket using the S3 Console.
- Test that these files have been uploaded by going to your S3 bucket’s endpoint.
- For our example, we should enter
http://example-site.com.s3-website.us-east-2.amazonaws.com
in our browser’s url bar and see our index.html file displayed.
- For our example, we should enter
- Recall in Step 4, you created index.html and error.html. Upload both files to your New S3 Bucket.
- Create a CloudFront Distribution
- Create a CloudFront Distribution by following the AWS Docs for Creating a CloudFront Distribution
- For Origin domain enter the endpoint that you copied in step 6. Note: Don’t select the bucket from the dropdown list. The dropdown list includes only the S3 Bucket REST API endpoints that aren’t used in this configuration.
- For our example, our Origin domain would be
http://example-site.com.s3-website.us-east-2.amazonaws.com
* For Viewer protocol policy select Redirect HTTP to HTTPS * Under Alternate domain name (CNAME) add the following: *.example-site.com
so any future subdomains will be re-directed. * Under Custom SSL certificate select “Request Certificate”. Note: complete the steps below then return to the Create Distribution tab.- This will open a new tab to the AWS Certificate Manager for your appropriate S3 Bucket’s region.
- Select Request public certificate
- Under Fully qualified domain name enter:
*.example-site.com
- Use an asterisk (*) to request a wildcard certificate to protect several sites in the same domain. For example, *.example.com protects www.example.com, site.example.com, and images.example.com.
- Under Select validation method choose DNS validation.
- Once you have created the certificate select View Certificate
- You will need the following values:
- CNAME Name
- This will look like
_a79865eb4cd1a6ab990a45779b4e0b96.example-site.com.
- This will look like
- CNAME Value
- This will look like
_424c7224e9b0146f9a8808af955727d0.hkmpvcwbzw.acm-validations.aws.
- This will look like
- CNAME Name
- You will need the following values:
- Login to your Registrar’s DNS Console for the Domain purchased in Step 5. For this example, I am using GoDaddy.
- Go to My Domains / Domain Settings / DNS Management / DNS Records.
- Select Add
- Under Type select CNAME
- Under Name copy and paste the CNAME Name from AWS Certificate Manager. For our example, this will look like
_a79865eb4cd1a6ab990a45779b4e0b96.example-site.com.
. Note that since we are using the GoDaddy, you need to modify the Name by truncating the apex domain (including the period) at the end of the NAME field, as follows:_a79865eb4cd1a6ab990a45779b4e0b96
. For more information, see DNS validation on GoDaddy fails. - Under Value copy and paste the CNAME value from AWS Certificate Manager. For our example, this will look like
_424c7224e9b0146f9a8808af955727d0.hkmpvcwbzw.acm-validations.aws.
- Back on the AWS CloudFront - Create Distribution tab, enter index.html for the Default root object.
- Select Create Distribution
- Note: you can only create the distribution once you have verified the SSL certificate with AWS Certificate Manager. This took less than 10 minutes for me, but can take longer depending on multiple different variables.
- Use AWS 53 to Route Traffic to the Amazon CloudFront distribution
- Sign in to the AWS Management Console and open the Route 53 console
- Setup a Hosted Zone for the website hosted on GoDaddy in the Route 53 console.
- Setup DNS Nameservers on GoDaddy to point to Route 53. See Making Route 53 the DNS service for a domain that’s in use
- Follow the AWS Route 53 Docs for Configuring Amazon Route 53 to route traffic to a CloudFront distribution.
- Create the A Record. Note: make sure that the Record Name in the Record matches the Alternate domain names in the Cloudfront Distribution. If the domain name in Cloudfront is
*.example-site.com
then the A Record must be the same. See this Stack Overflow Answer - Creat the AAAA Record the same way you setup the A Record.
- Create the A Record. Note: make sure that the Record Name in the Record matches the Alternate domain names in the Cloudfront Distribution. If the domain name in Cloudfront is
S3 Endpoint http://journalmakr.com.s3-website.us-east-2.amazonaws.com/
CloudFront Endpoint https://d3c2smw6z0koka.cloudfront.net
AWS CloudFront - Distributions console https://console.aws.amazon.com/cloudfront/v3/home?#/distributions
AWS S3 console https://s3.console.aws.amazon.com/s3/home?region=us-east-2®ion=us-east-2
GoDaddy Journalmakr dns https://dcc.godaddy.com/manage/journalmakr.com/dns?plid=1
Notes & Research
Notes & Research - localhost
- Got a simple webpage you want to host on localhost?
- TODO Grunt - The JavaScript Task Runner
- Install Node.js and npm using Homebrew on OS X and macOS
-
[Understanding client-side web development tools - Learn web development MDN](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Understanding_client-side_tools)
Notes & Research - GitHub
- GitHub Pages Documentation
- GitHub Desktop Documentation. Step-by-step guides to set up and use GitHub Desktop to manage your project work.
- Using GitHub to host a free static website
Notes & Research - AWS
Notes & Research - AWS S3
- Tutorial: Configuring a static website on Amazon S3
- AWS Website Endpoints
- Creating a Bucket on AWS S3
- Configuring block public access settings for your S3 buckets
- Uploading Objects
Notes & Research - AWS CloudFront
- Use CloudFront to serve a static website hosted on Amazon S3
- How do I use CloudFront to serve a static website hosted on Amazon S3?
- Using custom URLs by adding alternate domain names (CNAMEs)
Notes & Research - AWS Certificate Manager
Notes & Research - AWS Route 53
- Making Route 53 the DNS service for a domain that’s in use
- Routing traffic to an Amazon CloudFront distribution by using your domain name
- Configuring Amazon Route 53 as your DNS service - Amazon Route 53
Notes & Research - AWS CodePipeline
- Automate static website deployment from Github to S3 using AWS CodePipeline
-
[Automate static website deployment from Github to S3 using AWS CodePipeline by Sithum Jayarathna AVM Consulting Blog Medium](https://medium.com/avmconsulting-blog/automate-static-website-deployment-from-github-to-s3-using-aws-codepipeline-16acca25ebc1)
Notes & Research - GoDaddy
Notes & Research - Github Actions
Notes & Research - Option 3
- Boilerplate Template - index.html
- Boilerplate Template - error.html
- Host your static website in AWS under 2 minutes with S3
- How to build a Hugo website in AWS Lambda and deploy it to S3. See also a follow-up post Breakdown of costs for a static website on AWS after 18 months in production.
- Creating a static website using GoDaddy, AWS S3, and AWS CloudFront
- Setting Up a GoDaddy Domain Name with Amazon Web Services
- Host a Static Site on AWS, using S3 and CloudFront
- Domain by GoDaddy, DNS by Route53