Custom error page per origin

0

Hello,

I have a Cloudfront distribution in front of two origins, one private s3 bucket for static content, and one API gateway backed by Lambdas for the dynamic part. The S3 origin is associated to the default behavior, while the API gateway is associated to /api/* Because the static content does its own HTTP error handling, I have setup a custom error page for code 404 that returns /index.html as its content

This works fine except that if the lambdas return a 404 error code, then their content gets replaced by the content of index.html which is not what I want. Is there a way to limit the custom error page to just one origin, or one behavior ?

As I could not find it in the console, I tried to handle this way: Modify the lambdas to return 294 instead of 404 so that the custom error page is not triggered. Then use a Cloudfront function for viewer-response to change back the code to 404. While the status code gets replaced, Cloudfront also destroys the content before returning it to the user. That's very strange as it can be seen in the original response object given to the function that there is a Content-Length header that is not zero. And if I don't modify the status code, that content gets delivered to the client just fine (only of the status code is lower than 300). Would there be a way to tell Cloudfront to leave alone the content it receives even if the viewer-response function returns a 404? Maybe a x-amz-preserve-content header in the response object?

Another solution that I tried was removing the custom error page, leave the lambdas untouched and use a viewer-request Cloudfront function to replace the uri by /index.html if the original uri does not contain a dot. While this works in my particular case it's not a guarantee that it'll work in the future.

I did not try using Lambda@Edge to handle this because they are costly and difficult to deploy/remove/update in an automated way. I mean, there are huge delays between operations that can be performed on those which are quite painful to manage.

In the end, I'm looking for either of two things:

  1. Have a custom error page per origin or behavior
  2. A way to tell Cloudfront to leave the content alone even if a Cloudfront viewer-response function returns a status code greater than or equal to 300

If there is a place to submit a feature request, please let me know.

Thanks for your answers

asked a year ago534 views
1 Answer
0

I am not aware of a way to do what you are describing without Lambda@Edge. I have setup something similar in the past where I would use a Lambda@Edge origin response function to look for a response code, fetch a static page from S3, and then return that new page to the viewer with a 200. This was specifically to hide origin errors (5XX) from the viewer and return a maintenance page.

If you do decide to use L@E then you could have two behaviors (one for /api and one for *) and then each behavior would invoke their own L@E to set their appropriate error page on the response. If you want a code snippet to handle this with L@E I would be happy to share, just let me know.

profile pictureAWS
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions