How to store a Cloudfront custom error page in S3?
I have a Cloudfront plugged on an AWS load balancer.
I saw in the AWS documentation that I can customize Cloudfront custom error pages. I tried to store these error pages in S3 as it is recommended in the AWS doc but it doesn't work. The documentation says :
We recommend that you store custom error pages in an Amazon S3 bucket even if you're using a custom origin. If you store custom error pages on an HTTP server and the server starts to return 5xx errors, CloudFront can't get the files that you want to return to viewers because the origin server is unavailable.
My error page is in S3 with the path 'my-app-bucket/errorpage.html'.
I tried to link this file in the Cloudfront config but I have the following error :
If I try the value /my-app-bucket/errorpage.html, Cloudfront redirects the request to my ELB origin and I get a 404.
Who could explain me how I can do this configuration ?
Solution 1:
These are hints from http://blog.celingest.com/en/2013/12/12/cloudfront-configuring-custom-error-pages/
- You need to add another CloudFront origin pointing to the bucket where your error page is
- The cache behaviour of the newly-created origin should have a Path Pattern pointing to the folder (in the error page bucket) where the error pages reside
- You can then use that path in the Response Page Path when you create the Custom Error Response config
Solution 2:
There's a lot easier way than the one recommended by f01 to accomplish this. Enable static website hosting in S3, specify the error page in S3, and then use the static website hosting URL as the origin for your cloudfront bucket. No special path problems, no custom error response config. It just works. Super easy.
Here's a tutorial video I made:
Custom Error Pages in AWS S3 and Cloudfront
Solution 3:
This answer is a transcript of mbarlockers great video, as that is an external link that could vanish I have transcribed it here.
This is a great video but suffers again from being an external link, I have summerised the entire video here:
- Create a bucket in s3 with a dns compliant name (do not use periods for now)
- Make bucket public
- Upload your page leave permissions on objects as default
- Choose standard storage
- No Encryption
- Test using bucket URL, watch fail
- Test using full object URL, watch fail
- Go into the bucket policy and create a policy that allows Everyone to read your objects using GetObject.
- Ignore the public policy warning
- Try object URL again it should work.
- Enable static website hosting in your bucket settings in properties.
- Fill in the index document and error page fields, we don't need redirects, save
- Check the URL that static web hosting generated and use that and test using a non existant path and you will see your 404
- Go into cloudfront and create a distribution
- Name the origin name as the bucket link that appears
- Leave all options except caching and set at 1 day.
- Save and wait for the cloudfront distribution to deploy.
- now test via the cloudfront URL and test and observe failures
- Go back to cloudfront distribution and goto the custom error pages section and create a custom error page response.
- Specify 403 Forbidden as the HTTP error code
- 300 seconds TTL
- Customise the error response
- set response page path to /error.html (or whatever your error page is called)
- Change response code to 404 not found
- Go back to the browser and retry the bad URL now. The error page will be served.