11

I was wondering if the following is possible with AWS offerings?

https://www.example.com/a/ -> served by Apache on EC2 Instance A

https://www.example.com/b/ -> served by Apache on EC2 Instance B

To clarify, I do not want files under one directory path to be on the same server instance as files under the other directory path.

I understand this may be possible with a proxy of some sort, but is there an easier solution with one of AWS offerings.

The EC2 Load Balancer does not seem to allow switching based on directory path. Route 53 works at the DNS level, which does not have path information to return IPs based on that.

Joseph Shih
  • 213
  • 2
  • 6
  • The EC2 load balancer is so stupid that it won't read `Host:` headers and filter out bogus traffic. It seems unlikely they will add any features like you're asking for if looking at headers is too much work for them. Mercifully they have other options. – chicks Jan 10 '18 at 21:34

2 Answers2

29

Use the AWS Application Load Balancer, which does Path Based Routing. That second link is a tutorial how to do it.

In short, you set up your ALB as normal, then follow these steps (copied from the AWS tutorial):

  • On the Listeners tab, use the arrow to view the rules for the listener, and then choose Add rule. Specify the rule as follows:

  • For Target group name, choose the second target group that you created.

  • For Path pattern specify the exact pattern to be used for path-based routing (for example, /img/*). For more information, see Listener Rules.

  • Choose Save.

freginold
  • 239
  • 1
  • 7
Tim
  • 30,383
  • 6
  • 47
  • 77
  • Thanks! I checked out EC2 Application Load Balancer originally, went through the wizard, but never actually created one. The wizard didn't give a path option. As the tutorial pointed out, one has to create it first and then edit the rules after. – Joseph Shih Jan 10 '18 at 04:07
  • it does work only in one region! if you have two load balancers in different region and each one handles different url path, then ALB is useless. aws support told us that we can do this scenario with route53 and alb. – Mohammad Eghlima Mar 22 '22 at 19:45
  • @MohammadEghlima this is a four year old question, and it didn't ask about regions. AWS regions are deliberately separate, to keep faults isolated. As AWS support told you, you can use Route53 if you need to distribute traffic to multiple regions. – Tim Mar 22 '22 at 19:51
9

In addition to Tim's excellent answer, you can also achieve this with CloudFront.

First, create your distribution, adding origins for EC2 Instance A and EC2 Instance B (which could also be load balancers, non-AWS hosts, or even S3 buckets).

Then, setup cache behavior rules to map /a/* and /b/* to the appropriate origins.

Whether or not it makes sense to use CloudFront for this purpose will depend on your application and need for a CDN.

Zach Lipton
  • 190
  • 4
  • 4
    This could also apply if you wanted `/a*` and `/b*` to be handled by different services, different accounts, different regions, or even one path handled by something running inside AWS and the other elsewhere. CloudFront has many use cases that don't conceptually require a CDN at all. – Michael - sqlbot Jan 10 '18 at 12:17