Say that you would like to host your static site on Amazon S3 with a custom domain and, of course, HTTPS.
Create two S3 buckets
To serve requests from both root domain such as codetengu.com
and subdomain such as www.codetengu.com
, you must create two buckets named exactly codetengu.com
and www.codetengu.com
.
In this post, I assume that you want to redirect www.codetengu.com
to codetengu.com
.
ref:
https://docs.aws.amazon.com/AmazonS3/latest/dev/website-hosting-custom-domain-walkthrough.html
Upload your static files
$ cd /path/to/your_project_root/
$ aws s3 sync . s3://codetengu.com \
--acl "public-read" \
--exclude "*.DS_Store" \
--exclude "*.gitignore" \
--exclude ".git/*" \
--dryrun
$ aws s3 website s3://codetengu.com --index-document index.html --error-document error.html
ref:
https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html
Setup bucket policy for public accessing
In your S3 Management Console, click codetengu.com
bucket > Properties > Edit bucket policy, enter:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::codetengu.com/*"
}
]
}
Setup www redirecting
In your S3 Management Console, click www.codetengu.com
bucket > Properties > Static Website Hosting, choose Redirect all requests to another host name, type codetengu.com
.
Now you're able to access your website via:
- http://codetengu.com.s3-website-ap-northeast-1.amazonaws.com/
- http://www.codetengu.com.s3-website-ap-northeast-1.amazonaws.com
Configure a custom domain
In the "Setting Up a Static Website Using a Custom Domain" guide I mentioned above, it uses Amazon Route 53 to manage DNS records; In this post, I use CloudFlare as my website's DNS provider instead.
- Create a CNAME for
codetengu.com
to point tocodetengu.com.s3-website-ap-northeast-1.amazonaws.com
- Create a CNAME for
www.codetengu.com
to point tocodetengu.com.s3-website-ap-northeast-1.amazonaws.com
Yep, you CAN create a CNAME record for root domain on CloudFlare, just like your can add an "Alias" on Route 53.
Wait for the DNS records to propagate then visit https://codetengu.com/.