Skip to content

Configure Bucket as Website

Timothy Kay edited this page Dec 12, 2013 · 4 revisions

This document shows you how to configure a bucket as a website, using just the "aws" command line.

NOTE: I have been unable to get website redirection working, using just the AWS Console. Any help would be appreciated.

Create a file "website.json", containing a bucket policy that allows everybody to read the contents. Change web.timkay.com to your domain name.

{
  "Version":"2012-10-17",
  "Statement":[{
  "Sid":"AddPerm",
        "Effect":"Allow",
          "Principal": {
            "AWS": "*"
         },
      "Action":["s3:GetObject"],
      "Resource":["arn:aws:s3:::web.timkay.com/*"]
    }
  ]
}

Create a file "website.xml". You can use this same file for all website buckets.

<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <IndexDocument>
    <Suffix>index.html</Suffix>
  </IndexDocument>
  <ErrorDocument>
    <Key>4XX.html</Key>
  </ErrorDocument>
</WebsiteConfiguration>

Create a bucket, configure it as a website, make it world readable, add some content, and an error document:

$ aws mkdir web.timkay.com
$ aws put web.timkay.com?website website.xml
$ aws put web.timkay.com?policy website.json
$ echo 'hello, world!' |aws put web.timkay.com/index.html
$ echo 'Warning, Will Rogers!!!' |aws put web.timkay.com/4XX.html

Create a DNS CNAME entry:

web CNAME web.timkay.com.s3-website-us-east-1.amazonaws.com.

See It Work

http://web.timkay.com - shows the index.html file http://web.timkay.com/missing - shows the 4XX.html file

Content-Type Header

When uploading a file, "aws" reads /etc/mime.types and mime.types (from the current directory) to determine the proper Content-Type header. If "aws" correctly determines the Content-Type, then the file will display in the browser. If the Content-Type is set incorrectly, the file will be downloaded. For example, http://web.timkay.com/index.html has header "Content-Type: text/html", and http://web.timkay.com/indez.html (created without a mime.types file) has no Content-Type header. The first (index.html) will display in the browser, and the second (indez.html) will download.

You can force the MIME type:

$ echo 'hello, world!' |aws put web.timkay.com/index.html "Content-Type: text/html"

Official Amazon S3 documentation:

http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html

http://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html

http://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html