Sunday, October 11, 2015

Hosting a Website in S3 Bucket and Enabling Redirection in S3 BUcket

Hosting a Website in S3 Bucket:

It is possible to host your images and video for your websites on Amazon S3 for a long time. However, it was not that easy to host your entire website on S3. Why? If a user enters a site address (www.example.com) and the CNAME in the site’s DNS record resolves to the root of an S3 bucket (www.example.com.s3.amazonaws.com), Amazon S3 would list the contents of the bucket in XML form. In order to work around this, customers would host their home page on an Amazon EC2 instance. This is no longer necessary.
You can now host an entire website on Amazon S3. You can now configure and access any of your S3 buckets as a “website.” When a request is made to the root of your bucket configured as a website, Amazon S3 returns a root document. Not only that, if an error occurs your users receive an HTML error document instead of an XML error message. 

You can also provide your own error documents for use when a 4xx-class error occurs.
Here’s more detail on the new features…

 

Website Endpoints:

To access this website functionality, Amazon S3 exposes a new website endpoint for each region (US Standard, US West, EU, or Asia Pacific). For example, s3-website-ap-southeast-1.amazonaws.com is the endpoint for the Asia Pacific Region. Existing buckets and endpoints continue to work the same way they always have.

 

Root and Index Documents:

When you configure your bucket as a website, you can specify the index document you want returned for requests made to the root of your website or for any subdirectory.  For example, a GET request made to the following URI (either direct or via a CNAME):
mywebsitedomain.s3-website-us-east-1.amazonaws.com/images/subdirectory/
Will return the following S3 object
mywebsitedomain.s3.amazonaws.com/images/subdirectory/index.html


Error Document:

When you access a website-configured bucket through the new website endpoint, and an error occurs, Amazon S3 now returns a new HTML error page instead of the current XML error. Also, you can now specify your own custom error page when a 4XX error occurs.
You can use the S3 tab of the AWS Management Console to enable your bucket as a website.

Steps:

 1. Create your S3 bucket with proper naming.
 2.  Upload the static content to your s3 bucket.
 3. Now under properties, select Static Web Hosting.
 4. Select Enable website hosting option.
 5. Mention Index Document :  [index.htm] and Error Document: [error.html] and save the configuration.
 6. Under permission, add a bucket a policy which makes s3 bucket as public and save the configuration.




  policy:
{
  "Version": "2008-10-17",
  "Statement": [{
    "Sid": "AllowPublicRead",
    "Effect": "Allow",
    "Principal": { "AWS": "*" },
    "Action": ["s3:GetObject"],
    "Resource": ["arn:aws:s3:::your-bucket-name/*" ]
  }]
}

Note:
Once the static web hosting is enabled, your s3 bucket will have url in below format.
https://s3-ap-southeast-1.amazonaws.com/bucket-name/file.html
 It is not possible to point  whole url to any domain name. Thus, in order to overcome from this issue, point only https://s3-ap-southeast-1.amazonaws.com/ to domain name. 

Enabling Redirection in S3 Bucket:

In order to redirect any .html file to another .html file in same S3 bucket, use meta data concept.

Select the file. In properties section, select the meta-data option. 



No comments:

Post a Comment