As the name suggest, today I will show you how you can use AWS to deploy a static website to S3 and also configure SSL. I know, the internet is full of this kind of tutorials, but I could not found one that shows you in detail how to configure every service that is needed for a fully fledged static website.
Hit www.freenom.com and register a free custom domain. We will later on need to change the nameservers to point to your Route53 hosted zone.
I just registered for free n-ai.cf
.
Choose one of the following ways to install and build a static site using Gatsby:
npm install -g gatsby-cli
gatsby new gatsby-starter-blog https://github.com/gatsbyjs/gatsby-starter-blog
gatsby build
git clone https://github.com/gatsbyjs/gatsby-starter-blog
cd gatsby-starter-blog && npm install
npm run build
At the end of creating a static website section, we will have a new public
folder in gatsby-starter-blog
. The public
folder contains all the necessary files for your static website to work properly.
Go ahead and open up the AWS console and navigate to S3. All of the resources will be created on region us-east-1
.
Hit Create bucket
and make sure that you untick Block all public access
from under Bucket settings for Block Public Access
.
We do this because we what your site to be available to the world.
Go to AWS Policy Generator and generate a public s3:GetObject
action for your arn:aws:s3:::<bucket-name>/*
resource:
{
"Version": "2012-10-17",
"Id": "Policy1603609277940",
"Statement": [
{
"Sid": "Stmt1603609154318",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::<your_bucket>/*"
}
]
}
Static website hosting
for your domain. At this step you need to configure which is the file that will be first requested when someone visits your website and the error page.
Before uploading the content using AWS CLI, you must first be sure that your user has programmatic access enabled and you have the approriate rights to create objects on S3.
Create a user with programmatic access to S3
Navigate to the folder where you have your gatsby-starter-blog
template and cd public
folder.
Run:
aws s3 sync . s3://n-ai.cf
After this step, all the files from the public
directory will be live on S3.
You can access the bucket with the URL that looks like:
http://<your-bucket-name>.s3-website-us-east-1.amazonaws.com/
All the contents of your bucket should now be available for anyone on the web:
Just type in the Domain name
and hit Create
As domain names:
DNS Validation
CNAME
record with a click of a button.Create record in Route 53
Create Distribution
Origin Domain Name
pointing to your static hosted bucket URL. Be aware that by default in the dropdown you’ll see the name of the bucket, but that one is not the url to your static hosted bucket. To find exact the url to your static hosted bucket you need to access S3 and copy the URL from the Static Hosted Website that AWS provided when you first changed the properties for your bucket.It will look like:
http://<bucket-name>.s3-website-us-east-1.amazonaws.com
Viewer Protocol Policy
to Redirect HTTP to HTTPS
Alternate Domain Names (CNAMEs)
to the name of your website. In our case n-ai.cf
and *.n-ai.cf
because we want our site to also be available using www
.
Adding *.n-ai.cf
will enable Route53 to automatically detect your alias when you want to create one. We will see how all this lay our in the next section.
SSL Certificate
-> Custom SSL Certificate
Create Distribution
After max 15 minutes CloudFront will finish deploying all your files and you’ll have a link to access your CloudFront Distribution under Domain Name
for your specifc distribution ID.
Now that we have everthing set up, all that remains is to route traffic comming from www.n-ai.cf
or n-ai.cf
to the newly created CloudFront Distribution.
www.n-ai.cf
Now your site should be highly available when you type your domain name into the browser.