Thursday, December 19, 2013

How to host a static website on S3

UPDATE: I was horribly wrong about how to do the domain forwarding for naked domains – however there’s a simple answer. The good folks at wwizer have got a free naked domain rewriting service. In order to use it simple change your A (Host) to point to the ip address 174.129.25.170 and you’ll be sorted. wwwizer naked domain redirect

In February this year Amazon announced that you can now host static websites on S3. This is a great cost effective way of hosting small, simple websites (like an iPhone app site for example).
Here’s how you can do it:
  • Buy the domain name for the site you want to host. It’s best to do this first, because the S3 bucket name has to be the same as the domain.
  • Get an S3 account (it’s cheaper than you think, I only get billed 3c a month).
  • Log into the aws management console and create a bucket with the same name as your domain (complete with www). So for my website www.bigbuttonsapp.com I called the bucket www.bigbuttonsapp.com S3 Hosting Bucket
  • So that people have permission to view your website you’ll need to upload a bucket policy. Select on the newly created bucket, and click Actions > Properties. On the permissions tab click Add bucket policy and copy the following in (swapping www.bigbuttonsapp.com for your own domain):
{
  "Version":"2008-10-17",
  "Statement":[{
    "Sid":"PublicReadGetObject",
        "Effect":"Allow",
      "Principal": {
            "AWS": "*"
         },
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::www.bigbuttonsapp.com/*"
      ]
    }
  ]
}
S3 Hosting policy editor
  • While you’ve got the bucket panel open, click the Website tab and check Enabled. Also put the filename of your index file in the Index Document field. S3 Website panel
You can also optional include the name of your Error Document. This is the will be the document that is shown for any 4XX class of errors.
  • Click the Upload button and upload your website into that bucket. (In my case it was 3 files, index.html, error.html and styles.css)
  • To test it worked, click on the Endpoint link on the Websites tab of the bucket properties. Hopefully you should see your website up and running! (It might take a few minutes to kick in).

Domain names

Almost there, now just have to get the domain name running correctly. For this you’ll need to set up a CNAME redirect for www.
I know it’s horrible, but I use Godaddy, and here’s how I got this to work.
  • Log into the Godaddy domain management console and edit your zone file.
  • Amazon S3 static website doesn’t support naked domain rewriting, but thankfully wwizer provide it as a free service. In order to use it simply change the A (Host) to the ip address 174.129.25.170 wwwizer naked domain redirect
  • Edit the www subdomain to point to the endpoint that S3 gave you. Godaddy - edit subdomain
  • Save the Zone File
  • Click save, and now you should be able to browse to your site, all up and running!

BONUS UPLOAD SCRIPT

Here’s a script I created that let’s you upload your files to S3 with ease. It doesn’t handle subdirectories, and it uploads all the files all the time (which isn’t very cost efficient) – but for small websites it’s pretty useful.
To use it:
  • Install the S3 gem with gem install aws-s3
  • Copy the script into your website directory
  • Change the access key, secret and bucket name to your amazon credentials.
  • Run it with: ruby upload.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
require 'rubygems'
require 'aws/s3'
# Change the access key and secret to your amazon credentials
AWS::S3::Base.establish_connection!(
:access_key_id => 'ACCESS_KEY_ID',
:secret_access_key => 'SECRET'
)
# Change this to your S3 bucket name
WEBSITE_BUCKET_NAME = "YOUR_BUCKET_NAME"
# Will raise an error if bucket doesn't exist
AWS::S3::Bucket.find WEBSITE_BUCKET_NAME
Dir["*"].each do |file|
# Don't upload this file!
if file != __FILE__
print "Uploading #{file}.."
AWS::S3::S3Object.store(file, open(file), WEBSITE_BUCKET_NAME)
puts "done!"
end
end
view raw upload.rb hosted with ❤ by GitHub

No comments: