Search code examples
amazon-web-servicesamazon-s3amazon-ec2dnsamazon-cloudfront

how to route S3, EC2 at cloudfront, aws


My S3(front-end codes), and EC2(back-end codes) are BOTH linked to 'example.com' What I like to do here is to direct users to view the pages, stored in S3, unless they input 'example.com/api/'on their browser. In other words, 'example.com/api/' for the server access, and the other routes for the react app access.

which service should I use in AWS? CloudFront, Route53, Load Balance. I am so confused. thank you.


Solution

  • TLDR; Create a single CloudFront distribution for your domain with separate cache behaviors for each of the paths you need to support. You can easily add additional applications, control caching, or write edge functions (using Lambda@Edge or CloudFront Functions) for any further customization you might need.

    Details: CloudFront allows you to configure multiple origins (e.g. S3, EC2, API Gateway, ELB, custom URLs, and so forth) and then create cache behaviors (routes) that direct traffic to the appropriate origin.

    In your case, you would create a cache behavior of /api/* to point to your EC2 origin, and the default cache behavior (think of this as your fallback route) would point to your S3 origin for all requests that do not begin with /api/