Skip to content

Use same domain for Cloudfront and Kubernetes resources hosted in other AWS account

0

Situation: A domain lets say example.com that is being used for kubernetes cluster hosting backend applications. It is already using ALB which is multiregion. I do not have any control over the backend ALB. But I have a Gateway resource in Kubernetes cluster from where I can control the traffic to backend applications. Now, I have a Cloudfront which host front end application. Right now Cloudfront is using static.example.com domain. Now, I want to use the same root domain example.com for both backend and front end applications. like example.com/froentendapp and example.com/backendapp

Now, I can think of multiple solution but not sure which one is better and industry standard.

  1. Use Gateway resource in K8 to route traffic to backend and front-end application. It is multi region so not an issue with latency. Only think I don't like that people can still access the Cloudfront from the Cloudfront domain.
  2. Use another load balancer to route the traffic to Cloudfront and Kubernetes Gateway.
  3. Use Cloudfront to serve front end apps and route traffic to kubernetes gateway for backend apps. I heard this it will add more latency because of Cloudfront edge locations.

What do you guys think about the above situation and what should be ideal architecture here ?

asked 2 years ago547 views

1 Answer
1

Based on your situation, the most suitable and industry-standard approach would be to use CloudFront to serve both your frontend and backend applications under the same domain (example.com). This solution offers several advantages:

  1. Unified Domain: You can use example.com for both frontend and backend, achieving your goal of using the same root domain.

  2. Performance: CloudFront's global edge network can significantly improve the performance of both your frontend and backend applications by caching content closer to users.

  3. Security: CloudFront provides additional security features like AWS Shield for DDoS protection.

  4. Flexibility: You can configure different behaviors in CloudFront for different path patterns, allowing you to route requests appropriately.

Here's how you can implement this:

  1. Set up your CloudFront distribution with your frontend S3 bucket as the default origin.

  2. Add a new origin in the same CloudFront distribution pointing to your Kubernetes Gateway.

  3. Create a new behavior in CloudFront for backend requests (e.g., /backendapp/*) and set the origin to your Kubernetes Gateway.

  4. Ensure that the "Host" header is forwarded to the origin in the CloudFront behavior for backend requests. This is crucial for the SNI (Server Name Indication) to work correctly with your ALB.

  5. Update your DNS records to point example.com to your CloudFront distribution.

Regarding your concern about latency: While it's true that requests to the backend will go through CloudFront first, the added latency is typically minimal and is often outweighed by the benefits of CloudFront's global edge network. CloudFront can also cache certain backend responses, potentially improving performance for subsequent requests.

This approach allows you to maintain control over routing through your Kubernetes Gateway while benefiting from CloudFront's global network and features. It also solves the issue of people accessing CloudFront directly through its domain, as you can standardize on your custom domain.

Remember to properly configure security groups, NACLs, and other network settings to ensure that your backend services only accept traffic from CloudFront and not directly from the internet.
Sources
cloudfront distribution with several origins | AWS re:Post
Cloud Front To ALB SNI | AWS re:Post
Use custom URLs by adding alternate domain names (CNAMEs) - Amazon CloudFront

answered 2 years ago

EXPERT

reviewed 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.