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:
- Have a custom error page per origin or behavior
- 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