Skip to content

How to forward a HTTPS request from an application load balancer to a CloudFront distribution

0

Hi, We have a domain name www.abcd.com that points to an application load balancer that redirects http(s) requests based on different rules. Now, we would like to redirect www.abcd.com/au or www.abcd.com/au/ to a directory (au) in a S3 bucket (s3-abcd) if possible through a CloudFront distribution. And we would like the users to see the original domain name instead of a CloudFront distribution domain name in the browser address box. Is it possible to implement? If not, what options do I have?

Thanks, PH

3 Answers
0

Yes, it is possible to implement the scenario you described, but it requires a slightly different approach than forwarding requests from an Application Load Balancer (ALB) to CloudFront. Instead, you'll need to set up CloudFront to work with your existing ALB and S3 bucket. Here's how you can achieve this:

  1. Create a CloudFront distribution with two origins:

    • Your existing Application Load Balancer
    • Your S3 bucket (s3-abcd)
  2. Set up a behavior in CloudFront for the path pattern "/au/*" that points to the S3 bucket origin. This will route requests for www.abcd.com/au and www.abcd.com/au/ to the appropriate directory in your S3 bucket.

  3. Set up a default behavior that points to your ALB origin. This will ensure that all other requests are still handled by your existing ALB setup.

  4. Add your domain (www.abcd.com) as an alternate domain name (CNAME) in your CloudFront distribution settings.

  5. Obtain an SSL/TLS certificate for your domain through AWS Certificate Manager (ACM) in the us-east-1 region, and associate it with your CloudFront distribution.

  6. Update your DNS settings to point www.abcd.com to the CloudFront distribution domain name instead of the ALB.

This setup will allow you to:

  • Serve content from your S3 bucket for the /au path
  • Continue using your existing ALB rules for other paths
  • Maintain HTTPS throughout the entire request flow
  • Keep the original domain name (www.abcd.com) visible in the browser address bar

By using CloudFront as the entry point, you can take advantage of its global content delivery network, which can improve performance and reduce latency for your users. Additionally, CloudFront can handle the HTTPS termination, reducing the load on your ALB.

Remember to configure the appropriate security settings, such as using Origin Protocol Policy set to "HTTPS Only" for secure communication between CloudFront and your origins (ALB and S3).

This approach gives you the flexibility to serve content from different sources while maintaining a unified domain presence for your users.

Sources
Restrict access to Application Load Balancers - Amazon CloudFront
Move an alternate domain name to a different distribution - Amazon CloudFront
Choose how CloudFront serves HTTPS requests - Amazon CloudFront

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

  • Hi, I got '502 ERROR' after I configure the CloudFront distribution with the ALB as one of the origins. The error message is as below: 'The request could not be satisfied.

    CloudFront wasn't able to connect to the origin. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner. If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.'

    How to fix this problem?

0

Hello! Yes, this is possible!

If you are already using your ALB as an origin for the CloudFront distribution, you can add the S3 bucket as a second origin in the distribution. Configure a custom origin path (/au) so that requests forwarded to the distribution are directed to the correct directory in the bucket.

In the domain name configuration, add www.abcd.com as a CNAME in the CloudFront settings. Then, attach an SSL/TLS certificate for www.abcd.com using ACM to enable HTTPS. If you are using Route 53, create an Alias record pointing www.abcd.com to the CloudFront distribution.

This configuration ensures that requests to /au are served directly from the S3 bucket, while other requests can continue to be routed to the ALB as needed.

answered 2 years ago

0

Hi,

Following the suggestions above, I created a CloudFront distribution with the existing ALB as the origin to start with. I've checked:

  1. ACM certificates for CloudFront and ALB cover the same domains
  2. ALB security group allows traffic from 0.0.0.0/0
  3. When access ALB directly by it's DNS Name, it works properly

But when I accessed the CloudFront using the distribution domain name or the alternate domain name, I got 502 error with message like below: The request could not be satisfied. CloudFront wasn't able to connect to the origin. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner. If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.

What else do I need to check to make it work?

Thanks, PH

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.