S3 Static Website + Cloudfront question

I have a static website on S3 with a cloudfront distribution which works fine. I’m wondering if there is a way to prevent someone going to my website through the S3 URL?

My concern is that someone could find my S3 URL and eat up my S3 GET requests.


Related discussions:

You can restrict access to the bucket so that only CloudFront is allowed to retrieve objects. CloudFront identifies itself with an “origin access identity” when it makes requests so you modify your bucket policy to require this Principal which will implicitly deny everything and everyone else.

This is explained better here:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html

Plus there’s a sample bucket policy here:
https://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html#example-bucket-policies-use-case-6

I had a look at this earlier but couldn’t get it to work. When editing the origin, I couldn’t see the ‘Restrict Bucket Access’ option.

The documentation says, ’ You can only create an origin access identity for origins for which Origin Type is S3 Origin’.

My Origin Domain Name is the URL to the static website e.g. url.com.s3-website-eu-west-1.amazonaws.com. If I remove that and choose the option for the bucket (url.com.s3.amazonaws.com), the option now appears.

Am I able to change Origin Domain Name and keep everything working?

That sounds about right (partially recalling this from memory). If the request goes over the public URL then S3 is unable to differentiate whether the request is coming from CloudFront or anyone else on the Internet.

The option to create the policy through the UI means you won’t have to write the bucket policy yourself however it might interrupt service to the bucket while you get the configuration all set out right. Perhaps you could deploy a completely new bucket & CloudFront distribution for testing and then flip over the DNS to the new distribution once you’re satisfied it’s working?

I have a cloudformation template for this somewhere but it’s not quite finished. Maybe I’ll dig it out and finish it off.

Yes it should still work.

I’d love to be corrected if I’m wrong, but I think you end up giving up all the nice things that come from treating S3 as a website if you treat it as a bucket. So far I haven’t found a way to restrict it in that mode.

Here you go. Cloudformation template for serving an https website through cloudfront from s3 while also setting the s3 bucket policy to be secure.

https://blog.m-taylor.co.uk/2018/01/cloudformation-template-for-a-cloudfront-enabled-s3-website.html

What would those ‘nice things’ be?

Index pages, nice error pages: https://www.slideshare.net/AmazonWebServices/introducing-amazon-s3-website-features

Ah ok. I’ve managed to get around this by setting up Custom Error Pages through Cloudfront.