Direct CloudFront to Specific Bucket Folder if Path Elements Exists - amazon-s3

I have a CloudFront distribution in front of a S3 static website.
I want all traffic where first path element is profile to be directed to one file in my bucket.
For example, for a GET request to https://example.com/profile/12345, I want CloudFront to return S3 object s3://<bucket_name>/profile/index.html.
My goal is that I would handle dynamic content for profile/12345 using frontend JS and my API.

Related

CloudFront or S3 ACL: This XML file does not appear to have... AccessDenied / Failed to contact the origin

I'm using custom domain and CloudFront for S3 static hosting site to serve https.
It's working fine when I open the pages through the app's internal buttons or link,
but if I input direct URL in the address bar, or click the browser refresh button, it shows
This XML file does not appear to have any style information associated with it. The document tree is shown below.... Access Denied error screen.
I searched related answers and tried to /index.html in the CloudFront general setting as Default Root Object but it didn't work. (Before this try, it was index.html)
When I updated it as /index.html, even the domain itself didn't work.
I have another S3 static hosting site without CloudFront and certificate just for testing.
This site is working fine even I input direct url or click the refresh button.
Above two S3 bucket have same settings (root object is index.html and error document is also index.html)
After this, I changed CloudFront Origin Domain Name from REST endpoint to website endpoint referred to this docs (https://aws.amazon.com/premiumsupport/knowledge-center/s3-website-cloudfront-error-403/)
But now getting this error when I refresh the screen.
All the object in S3 is owned to bucket owner and has public access.
This app is made by React and using react-router-dom.
Could you give me any hint or advice?
Thanks.
Solved...
My S3 bucket region requires . instead of - when I use website endpoint for cloudfront.
And FYI..
In my case, there are some little difference with the document and some tutorial. My CloudFront distribution doesn't need to use default root object, and individual objects in S3 has no public access but the bucket has it.
There are some specific endpoints to be used for website hosting buckets, which are listed in the Amazon Simple Storage Service endpoints and quotas document. For example, when hosting in eu-west-1, cloudfront will prepopulate the dropdown with example.s3.eu-west-1.amazonaws.com, but if you look into the bucket settings, Static website hosting section, it will show you the correct url example.s3-website-eu-west-1.amazonaws.com
Carefully read the table! The url scheme is not fully consistent, eg. s3-website.us-east-2.amazonaws.com but s3-website-us-east-1.amazonaws.com - just to make your day a bit more joyful.
So I had the exact same issue and was able to resolve it after taking the s3 bucket endpoint located in the properties of the s3 bucket and then pasting it into the cloudfront origins section into the origin domain. I removed the beginning of the endpoint for example: "http://website.com.s3-website.us-east-2.amazonaws.com" you would just remove the "http://" and then post the rest into the cloudfront origin domain and click save. That should solve the problem!
I tried all kinds of different options such as making sure every object was public as well in the s3 bucket. Make sure your s3 bucket is also publicly available.
Certain regions do have different endpoints for your s3 buckets. Here is a link that shows more of that: https://aws.amazon.com/premiumsupport/knowledge-center/s3-rest-api-cloudfront-error-403/

How to host website in S3 and CloudFront?

I am trying to host my Angular website using CloudFront, S3, and Amazon EC2
instances. I am creating a bucket for e.g. bucket_name and creating 3 folders in it naming:-
1. example
2. login
3. logout
And my URL is:-
1. abc.com/example/
2. abc.com/example/login
3. abc.com/example/logout
But when I host my website I am able to access abc.com/example/ but the issue I am facing is that when I tried to access login and logout URL I am getting an error "404 not found". Because it is trying to locate index.html in the example folder of the S3 bucket like in this way example/login/index.html and EC2 instance is for API calls.
To host your site on S3 bucket and aws CloudFront you must include your parent file index.html in S3 bucket and after this you need to configure static website hosting under properties of S3 bucket. Set these fields as use this bucket to host website then enter index document index.html this file must be in S3 bucket, and save this.
Next step you can edit public access of this bucket, if you want this site accessible outside world then turn off block public access under Permissions tab of S3 bucket.
Later, to host this using CloudFront you need to create CloudFront distribution, and set Origin Domain name = Your S3 bucket name and Default Root Object to your parent index.html file (which is stored in s3 bucket) and save this CloudFront distribution, now check the Domain name URL on browser, it should work fine.
After completing this you can setup your EC2 instance for back-end API calls as per your needs.
Try to keep pages like login.html and logout.html. That should work.

How can I hid S3 and CloudFront contents?

I have an S3 bucket and Cloud Front distribution to deliver assets (PDF files) to my site and CMS. I want users to be able to access the PDF, but
I noticed that if I use either the S3 or Could Front URLs that I can erase the file name (e.g., my-bucket.s3.amazonaws.com/file.pdf or cloudfront-domain-name/file.pdf) and see an XML tree of the S3 or Cloud Front that displays all the content (filenames of all the PDFs, and the S3 URL in the case of the CloudFront URL).
How can I prevent somebody from doing this?
make the bucket not open to public, the it wont list the contents in the bucket

Resolve S3 static page from custom path with CloudFront

I've got an S3 bucket configured to host a static website, and if I browse to the bucket URL it shows the index.html file. I also have a CloudFront distribution to show another S3 bucket under a custom domain. Is there any chance I could configure CloudFront to serve one bucket from the root and another from a custom path? So:
mydomain.com -> bucket1/index.html
mydomain.come/some-path -> bucket2/index.html
I already created an origin for the bucket and set up a path pattern for it and some-path, but I'm getting 403 Forbidden, even though if I browse to the origin directly I can see the webpage.
This configuration works fine, but it requires that the object in bucket2 be located at some-path/index.html inside bucket2.
The path pattern you configured in the cache behavior is still part of the request path, so it is forwarded to the origin.
CloudFront does not support removing part of the request path before forwarding the request to the origin.

Unable to point to a Amazon s3 url using Amazon route 53

I am trying to create a record set in route 53 that will point to one of my static file on s3.
Ex: https://s3-us-west-2.amazonaws.com/test/terms-conditions/termsconditions.html
I have created a CNAME record in route 53
"termsconditions.mydomain.com"
and in the value I have given the s3 url.
https://s3-us-west-2.amazonaws.com/test/terms-conditions/termsconditions.html
The record was saved without any errors. But now when I go to
termsconditions.mydomain.com I don't get any response.
Any idea why this is not working?
I have configured other record sets and pointed to EC2 instances and loadbalancers and they are working fine.
When using Amazon Route 53 to point to an Amazon S3 bucket:
The S3 bucket must be named the same as your domain, eg termsconditions.mydomain.com
Configure the bucket for static website hosting
Create a Route 53 record set to alias the domain name to the S3 bucket
Note that the record set can only point to the "domain portion" of the URL, not the whole URL. So, you'll need to use a bucket that is named termsconditions.mydomain.com and then access the page via the complete URL of termsconditions.mydomain.com/termsconditions.html.
If you want to just use the domain portion of a URL (eg termsconditions.mydomain.com) to direct to a specific page, define that page as the "Index" document in the Static Website Hosting configuration page.
See: Setting Up a Static Website Using a Custom Domain