Skip to content

Instantly share code, notes, and snippets.

@tomislacker
Last active December 29, 2016 17:56
Show Gist options
  • Select an option

  • Save tomislacker/9fa38a83abde2dff8d652a895d33c52b to your computer and use it in GitHub Desktop.

Select an option

Save tomislacker/9fa38a83abde2dff8d652a895d33c52b to your computer and use it in GitHub Desktop.
S3 Website Hosting Crashcourse

S3 Website Hosting Crashcourse

Procedure (Without an explicit domain)

In this case, your bucket name does not matter

  1. Create the bucket (I'll use myassets) as an example
  2. In the S3 interface, goto the bucket's Properties
  3. Under Permissions add a grantee: - Grantee: Everyone - Check only "View Permissions"
  4. Add/Edit the bucket policy to reflect:
```json
{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "PublicReadForGetBucketObjects",
            "Effect": "Allow",
            "Principal": {
	            "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::assets.example.com/*"
        }
    ]
}
```
  1. Under "Static Website Hosting" toggle "Enable website hosting"
You'll need to specify index & error documents but they don't have to exist.
  1. Take note of the "Endpoint:" that looks something like: myassets.s3-website-us-east-1.amazonaws.com

Your assets will now be accessible there. So a key called avatar.jpg would be available at http://myassets.s3-website-us-east-1.amazonaws.com/avatar.jpg

Procedure (With an explicit domain)

For the sake of the example, I'll be hosting my content at assets.example.com

  1. Create a bucket named assets.example.com
  2. Follow the same steps from the non-explicit domain procedure
  3. Update your DNS to have a CNAME from assets.example.com to the endpoint you found above.

Caveats

  • With S3, there are IAM policies and bucket policies; you'll only need to deal with the latter for this use case
  • Remember that all S3 bucket names must be globally unique
  • The S3 bucket name must be identical to the hostname you CNAME (if you choose to use an explicit hostname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment