hosting a static site for less than a dollar a month

The nice thing about Amazon S3 (there are a lot of nice things about it, but I’ll concentrate on just a couple for now) is that you can store pretty much any amount of data on it. But the part which eludes most is that you can turn your bucket into a website with just a couple of commands. This gives you the 11 9’s of durability and 4 9’s of availability out-of-the-box without any additional tweaking of the configuration and without creating a single restart script.

Let’s get started.

Here are the commands that you need to run to create the bucket and make it into a ‘website’:

BUCKET_NAME=my-awesome-site-unique

# Create the bucketaws s3 mb s3://$BUCKET_NAME/ --region us-east-1
# Add the ‘website’ configurationaws s3 website s3://$BUCKET_NAME/ --index-document index.html --error-document 404.html
# Add the bucket policy
cat s3-bucket-policy.json | sed 's/REPLACE_ME/'$BUCKET_NAME'/' > /tmp/s3-bucket-policy.jsonaws s3api put-bucket-policy --bucket $BUCKET_NAME --policy file:///tmp/s3-bucket-policy.json

Here’s the s3-bucket-policy.json file mentioned above:

{
  "Version":"2012-10-17",  "Statement":[{
    "Sid":"PublicReadForGetBucketObjects",    "Effect":"Allow",    "Principal": "*",    "Action":["s3:GetObject"],    "Resource":["arn:aws:s3:::REPLACE_ME/*"    ]
  }
  ]
}

You’re DONE. Now, all you have to do is move some files into the bucket. Here’s a command to do that too:

cd my-website-dir
aws s3 sync . s3://my-awesome-site-unique

What the above command does is copies everything from the current directory (which is you website’s root on your machine) to the bucket. Now, all you need to do is visit the bucket at my-awesome-site-unique.s3-website-us-east-1.amazonaws.com

You’re all set! Make sure the bucket name is unique.

 
comments powered by Disqus