Using same domain name for frontend and backend deployment in aws

I've made the following diagram showing the pieces which this request will flow through:

Request flow

As @jogold has said, the key is CloudFront, which will behave redirecting to FrontEnd or BackEnd (Same distribution, multiple origins, multiple behaviors)

Remember to create a new origin ("Origins and Origin Groups" tab), but also a new behavior ("Behaviors" tab): Use the expected "Path Param" (Your app also should have this as a BasePath) and pay attention to "Precedence" (Default must always be the last).

In my case, "PathParam" value for BackEnd was "/api/*"

CloudFront example behaviors


There are multiple solutions to this problem.

You could have www.example.com point to your S3 bucket and api.example.com point to your backend.

Another solution would be to use CloudFront and configure it with multiple origins: one for your frontend (S3) and one for your backend (custom origin, API gateway or Application Load Balancer). You would then have a specific behavior like /api/* serve your backend. This solution has the advantage of having a CDN in front of both your frontend and backend and avoiding CORS request in the frontend (+ many more features offered by CloudFront).