Does AWS offer a way to route HTTPS traffic to two different EC2 instances based on directory path?

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.


Solution 1:

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.

Solution 2:

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.