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.
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.
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.
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.