Objective
I have the following I wish to host under a single subdomain https://sub.example.com:
- An external Wix site, which should be accessible at the root subdomain
https://sub.example.com - An API accessible through AWS API Gateway, which should be accessible at
https://sub.example.com/api - A flat static site in a restricted AWS S3 bucket, which should be accessible at
https://sub.example.com/docs/
Attempted solution
I'm having trouble combining all this in AWS Cloudfront and Route 53. So far, I've tried setting up a Cloudfront distribution with the following default origins and behaviours:
- Origin:
https://username.wix.com/website(root domain as Origin domain and/websiteas Origin Path). Behaviour: Path asDefault (*) - Origin: API Gateway Invoke URL (stage is Origin Path). Behaviour: Path as
/api*. - Origin: S3 bucket domain. Behaviour: Path as
/docs*.
In Route 53 I have the sub.example.com A and AAAA records pointing at the Cloudfront distribution domain.
Problems
I'm running into a few issues here:
- Redirection of the Wix site is imperfect. Cloudfront hides the Wix domain at the root, but then redirects to the Wix domain for any subdirectories (e.g.
https://sub.example.com/about. Also, when specifyingindex.htmlas Default root object (this is needed for the S3 static site), the Wix site 404s. - API calls work perfectly fine at
https://sub.example.com/api/, but for some reason, the S3 static site is inaccessible (Access Denied) athttps://sub.example.com/docs/, even when the Default root object is set asindex.html.
Regarding Problem 1, I acknowledge that Cloudfront is not really meant for proxying external sites, but I'm not really sure how to otherwise keep this site at the root subdomain, especially since a Default (*) behaviour is mandatory.
Regarding Problem 2, I'm left a bit flummoxed. I tested all policies and OAIs using a separate distribution to the S3 bucket and confirmed that this is all correct. The Behaviour precedence is also correctly set (0: /docs*, 1: /api*, 2: *).
Regarding both these problems, I wonder if there may be some Route 53 wizardry that can partly solve them. Perhaps solutions can be found in Cloudfront Functions or Lambda@Edge. However, I'm not very experienced at these.
Any advice would be very welcome.