Hexo Serverless (part trois)

This document is a continuation of Putting the Hexo CMS into a Serverless world and part 2.

Let’s create our Hexo site, create some content, upload it to S3 and validate that we can access. If you don’t have Hexo installed already you can install it via npm install -g hexo-cli or use an alternative method defined at the Hexo Docs website.

Navigate back up to the root of your project directory and create a Hexo site with by running:

hexo init website
cd website
npm install

Let’s validate that we can access the boilerplate website locally before we move onto adding our content to the AWS S3 bucket by running a local Hexo server:

hexo server

Visit http://localhost:4000 in your browser. If you see the Hexo welcome page, you are ready to move on. If not, see the Hexo Docs for any installation steps that may have been missed or updates to Hexo. Stop the local server and generate the static content, just to be sure it’s all compiled and ready to be relocated.

hexo generate

At this point, there are a several means to getting our content to AWS S3 to be served to the world. A few options are to use the AWS Console and upload the content in the browser, use the AWS CLI or to use hooks in our version control system.

The best method is to have your version control system be integrated into your development pipeline. AWS, BitBucket, Azure, GitLab and others offer solutions to this.

TODO: Come back and add Terraform for a CodeCommit repo that kicks off CodeBuild

To create a Git repository to hold our project, create a file named codecommit.tf and add the following content. Run terraform apply afterwards and we will have an AWS CodeCommit Git repository for version control.

resource "aws_codecommit_repository" "domain" {
repository_name = "aws-serverless-hexo"
description = "Hexo served from AWS S3 and AWS CloudFront built via Terraform"
default_branch = "master"

An expansion on this will be made, but for simplicities sake, we will use the AWS CLI for now.

aws sync . s3://mydomain.com/

Visit your domain in the browser to see your new website that’s completely serverless!

You may have noticed that SSL/TLS didn’t work. That’s because AWS S3 Static Website Hosting doesn’t support it currently. We will need to add it to our AWS CloudFront Distribution, which we were going to use anyways to speed up content delivery as well as adding an added layer of protection free via AWS Shield just for using CloudFront. We will also adjust the permissions on our AWS S3 bucket to force users to go through AWS CloudFront instead of using our AWS S3 Static Website Hosting URL instead.

This portion was short and simple to ensure that we can work with a site locally and visit the content after being synced to AWS S3. Now we can head on over to part 4 to get crazy.