Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path based routing to cloudfront and ec2

So currently we have two ec2 instances (lets say A and B) and a cloudfront.

If the user goes to www.appdomain.com/app the user should get routed to the cloudfront SPA page. However if the user goes www.appdomain.com the user should be routed to the EC2 instance A, and if user goes to www.appdomain.com/api be routed to EC2 instance B.

All of these applications must be on the same domain.

Now we found out how to set path rules using an application load balancer, but would like to know how to set it to cloudfront as well.

Update: So in summary the question is how do we route /app to cloudfront / and /api to ec2.

like image 855
MilindaD Avatar asked Jan 30 '19 17:01

MilindaD


1 Answers

All of these applications must be on the same domain.

In this scenario, every request for that domain must pass through CloudFront first.

Your DNS record will need to point to CloudFront (not the ALB) and CloudFront is then responsible for routing the request to the appropriate target -- to an EC2 instance via an ALB, to an S3 bucket, to wherever you need the requests to go -- and each of these things is called a content origin.

Once the origins are specified by their individual domain name (not your site's domain name, but a domain name specifically for the resource in question), you define CloudFront path patterns to select which origin is to receive the request for each pattern (e.g. /api*).

Once your DNS is changed to point to CloudFront, all requests go there first, and are handed off to the next service, unless CloudFront has a cached copy of the requested object -- in which case, CloudFront will serve it from its cache, and nothing will be sent to the origin.

You can't route from ALB to CloudFront, but you can route from CloudFront to ALB.

You can't subdivide a domain into multiple, different path-based content origins without using a reverse proxy that is able to match the paths and fetch the content on behalf of the requester -- HTTP and DNS don't support such functionality. CloudFront, in addition to providing the CDN service, is also a reverse proxy.

ALB, of course, is also a reverse proxy, but does not support as many different types of content origins as CloudFront does -- ALB only supports EC2 instances, servers in your data center (in which case, ALB must have a VPN path in order to reach them), and Lambda functions as content origins. CloudFront can use literally anything as a content origin as long as it speaks HTTP/HTTPS and is accessible via the Internet. (To choose a somewhat random example, CloudFront can even use a service from another vendor -- like a Google Cloud Storage bucket -- as a content origin, if that was something you needed to do, for whatever reason... because these are accessible via HTTP across the public Internet.)

like image 176
Michael - sqlbot Avatar answered Oct 14 '22 02:10

Michael - sqlbot