Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Amazon S3 raw endpoint access

Say you want to host a static web site on S3 :

  1. You create a bucket with name your-website.com and set it up for web hosting;
  2. You add a CNAME in your domain's zone file to point to your S3 bucket.

Great. Everything works fine when you visit http://your-website.com. But you don't want the raw/"naked" endpoint to be accessible.

Is there any setting in the bucket to disable direct access to http://your-website.com.s3-website.your-region.amazonaws.com ?

The reason is that if your web site is accessible both through http://your-website.com and http://your-website.com.s3-website.your-region.amazonaws.com would hurt your SEO (duplicate content)

like image 817
redben Avatar asked Dec 24 '14 12:12

redben


People also ask

Can I restrict S3 access by IP?

To secure our files on Amazon S3, we can restrict access to a S3 bucket to specific IP addresses. The following bucket policy grants permissions to any user to perform any S3 action on objects in the specified bucket. However, the request must originate from the range of IP addresses specified in the condition.

How do I restrict Amazon S3 bucket access to a specific IAM user?

You can use the NotPrincipal element of an IAM or S3 bucket policy to limit resource access to a specific set of users. This element allows you to block all users who are not defined in its value array, even if they have an Allow in their own IAM user policies.


1 Answers

You mention your major concern is SEO. For that purpose, you could use a other techniques, that are probably easier to implement than the one you initially asked about.

One of the main techniques to deal with duplicate content is to use rel=canonical, which is probably fairly easy to implement. For more information, see http://googlewebmastercentral.blogspot.com.br/2013/04/5-common-mistakes-with-relcanonical.html

If you insist on the need to disable access to the bucket unless the client connects through your CNAME, your best bet is to use CloudFront. You disable the S3 website hosting option on your bucket, make your S3 bucket private (i.e., remove bucket policies or ACLs allowing public read), create a CloudFront distribution, define your bucket as the origin, configure a CNAME on your distribution, change your DNS records to point to your distribution instead of bucket, create an Origin Access Identity (OAI) on your distribution and grant access to your bucket for that OAI. Phew.

By doing all this, there's no way for a user to access the content on your S3 bucket (unless they have an AK/SK with permissions to read the bucket, and send a signed request, obviously). The only way will be through your domain.

For more detail on Origin Access Identity, see http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html

like image 125
Bruno Reis Avatar answered Oct 07 '22 17:10

Bruno Reis