CloudFront has two types of edge compute: CloudFront Functions and Lambda@Edge. CloudFront functions only supports viewer-facing (request/response) events, while Lambda@Edge supports both viewer-facing (request/response) and origin-facing (request/response).
Because you want to dynamically change the origin (host) that CloudFront routes the request to, you need to use an origin-request event. That means you'll need Lambda@Edge.
The process to do so is straightforward:
- Ensure the HTTP header that this solution is predicated on is either in your cache policy (if part of your cache key) or origin request policy (if not part of your cache key). This will instruct CloudFront to make the viewer header available to your Lambda@Edge function.
- Write a Lambda@Edge function that conditionally checks the header, and updates the host header of the request to either ALB1 or ALB2. CloudFront will use that host to make the request to your origin.
Example here — https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-content-based-routing-examples