- Newest
- Most votes
- Most comments
Hi,
the non default setup (api/*
) isn't working because CloudFront forwards your request to /api/hello
on your origin which doesn't exist.
What's happening is that CloudFront, natively, can prepend to the path for a given origin, but it does not currently have the capability of removing elements of the path (without Lambda@Edge, as noted below).
So, if your endpoint were at /api/hello
at the origin, you could have the path pattern /api/* transformed before sending the request to the origin by setting the "origin path". However, the opposite isn't true. If the endpoint is at /hello
at the origin, there is not a built-in way to serve those files from path pattern /api/hello
.
In short, you can add (prefix) but not take away.
However, Lambda@Edge allows you to declare trigger hooks in the CloudFront flow and write small Javascript functions that inspect and can modify the incoming request, either before the CloudFront cache is checked (viewer request), or after the cache is checked (origin request). This option does not modify which Cache Behavior will actually service the request, because this is always based on the path as requested by the browser -- but you can then modify the path in-flight so that the actual requested object is at a path other than the one requested by the browser.
ㅤ
ㅤ
ㅤ please upvote/accept this answer if you found it helpful
Relevant content
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
Thank you for the detailed explanation 👍🏼
For others who pay reference this post in the future, I resolved the issue by building a new CloudFront Function, attached to the Viewer Request of the Behavior that removes the first segment after the domain before sending the request through to the origin, here's the code:
More info: https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/