Publishing Simple Static Sites with npm, Grunt and S3
In this post I'll explain how to easily publish a simple static website using npm, Grunt and Amazon S3. I assume you already have an AWS account in place. I wrote this guide using OS X Yosemite, so you will need to adapt it to other systems.
First, what is Node.js?
Node.js is a platform for building JavaScript applications. It is based on the runtime shipped with the Google Chrome web browser. My favourite way to install it is using Homebrew:
Brilliant, now let's ensure Grunt is installed globally using npm:
Grunt is a JavaScript task runner. It is similar to Phing in PHP or Rake in Ruby. Now we can create an empty directory to hold our project:
And create a public directory to store our HTML:
Managing the dependencies
We'll use npm to create a new package.json
file in the root of our
project. npm uses this file to manage our project's dependencies.
I normally push enter a few times and accept the defaults. My
package.json
file now looks like this:
We also need to add a few other libraries we'll use later:
Ok great, in the next step let's create a Gruntfile.
Creating the Gruntfile
Grunt uses a file called Gruntfile.js
to manage common tasks associated
with your project. Create this file and paste in the following code:
In my Gruntfile I load the required plugins and configure two tasks. The first
task is responsible for publishing to S3. The second task sets up a basic
webserver we can use to preview our site when we are developing it. I have
marked the webserver as the default task. We also need to make a file called
.aws.json
to store our AWS credentials. A word of warning here.
Never commit your AWS credentials if you are using GitHub to version
your code. I've heard plenty of stories of bots automatically
extracting them and wreaking some havoc within your account. My .aws.json
file looks like this (with the AWS credentials redacted):
Be sure to insert your access keys and bucket name or you will not be able to publish to S3.
Creating some HTML
Before we publish our site we actually need to make one. Create an empty HTML file:
Open it in your text editor and add some basic text, e.g:
my site works!
Lets take a quick look at it, try running:
Then open the following URL in your browser: http://0.0.0.0:8000.
Great now we can publish to S3!
Publishing to S3
AWS is a great platform and S3 has a feature for serving static websites.
We'll use the Grunt grunt-aws
task we installed earlier to easily publish
our site to S3. Run the command:
If you correctly configured your AWS credentials and bucket name, Grunt will connect to S3 and publish your website!
Configuring S3 to serve your website
Finally we need to ensure S3 is configured to serve our site using a bucket or custom domain name. For more information on how to configure a static site using an S3 bucket, follow the article here: http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html.
Wrap Up
We have now published a simple static site using npm, Grunt and S3. If you have a rich static site you could look at integrating CSS & JavaScript precompilers for SASS or CoffeeScript. You could also investigate other static hosting solutions such as Github Pages, Divshot or Netlify. If you have any questions send me an email or tweet.