Hugo beautifully facilitates the creation of static web sites that are cheap to host and trivial to scale. Historically, the trade-off with static blogs has been a clunkier process for publishing new articles. The days of simply hitting “save” in a web browser to update a blog post from anywhere went away for a little while, but AWS Cloud9 is bringing them back.
Cloud9 is a web based IDE that is perfectly suited to managing a Hugo blog in the cloud. The static site source files can reside on any server accessible via SSH, but Cloud9 really shines when backed by an EC2 instance that it manages on your behalf. Persistent files are stored on an EBS volume and the EC2 instance can be automatically stopped after a configurable period of inactivity.
The IDE provided by Cloud9 is surprisingly powerful, but I’m not ready to make it my daily driver for general programming tasks. That said, it makes an excellent interface for managing specific projects in the cloud (for convenience, collaboration, etc).
Getting Started
Create a new Cloud9 Environment in the AWS Console for the Hugo blog (the defaults suited my needs).
The following instructions assume you are using a managed EC2 instance.
Install Hugo
Install Hugo by running the following in the Cloud9 Terminal:
# Create the ~/bin directory (if it does not exist):
mkdir -p ~/bin
# Download the latest Hugo release
# Thanks https://gist.github.com/steinwaywhw/a4cd19cda655b8249d908261a62687f8#gistcomment-2172418
curl -s https://api.github.com/repos/gohugoio/hugo/releases/latest | grep browser_download_url | grep Linux-64bit.tar.gz | cut -d '"' -f 4 | wget -q -i - -O - | tar xvz -C ~/bin hugo
Create/Upload a Hugo Site
Follow the Hugo Quick Start guide to create a new site
or upload existing source files via the browser interface.
Managed EC2 instances have git
installed so it is also possible to clone an existing repo.
Preview Changes
Cloud9 supports previewing applications running on port 8080.
Click on Preview > Preview Running Application.
You will be greeted by an “Oops” message because the hugo server is not running. Copy the preview URL to the clipboard.
Start the hugo server with the following arguments (use the preview URL as the value for -b
):
hugo serve --bind=0.0.0.0 -p 8080 -b PREVIEW_URL --appendPort=false --disableFastRender
Refresh the preview to see your site.
Pro Tip: Your preview URL won’t change, so feel free to create a new Run Configuration for this command to easily run it later. Click Run > Run Configurations > New Run Configuration.Publish Changes
If your site is hosted on S3, simply use the pre-installed AWS CLI to upload the contents of the public directory:
# build the site with hugo
hugo
# upload the site to S3
aws s3 sync ./public s3://your_bucket
See docs for additional options.
To define advanced publishing rules, check out s3-publish (by me).
Advanced Usage
Pro Tip: Cloud9 can be used to generate SSL certificates with Let’s Encrypt. See my other post for details.Configure SSH Key
The web based Cloud9 Terminal does a perfectly serviceable job for many tasks, but connecting to the EC2 instance via SSH using your local Terminal is much more powerful.
Local Config
If no ~/.ssh/id_rsa.pub file exists on your local machine, follow these instructions to create one.
Run the following in the local Terminal to display your SSH Public Key:
cat ~/.ssh/id_rsa.pub
AWS Cloud9 Config
Click the icon in the top right corner of the Environment panel and select Show Home in Favorites and Show Hidden Files.
Paste your SSH Public Key into the ~/.ssh/authorized_keys file.
SSH Command
Connecting to an EC2 instance managed by Cloud9 requires a bit of detective work. The external IP of the the EC2 instance is needed to connect to it via SSH. The local IP of the EC2 instance is required to forward ports to the local machine. Both of these values may change when the instance is stopped so, for convenience, here’s a script to detect the relevant IP addresses and output the appropriate SSH command:
This script can be installed by running the following in the Cloud9 Terminal:
wget -P ~/bin https://atj.me/u/c9ssh.sh && chmod +x ~/bin/c9ssh.sh
Run the script to output the ssh
command:
c9ssh.sh
Paste the ssh
command into the Local Terminal to connect to the
Cloud9 Environment.