serve Multiple websites from a single CloudFront

serve Multiple websites from a single CloudFront

To serve both mobile.com and tablet.com websites from a single CloudFront distribution and route requests to the appropriate S3 bucket based on the incoming domain request , you can follow these steps:

  1. Set up your S3 buckets:

    • Ensure you have two S3 buckets named appropriately for your domains, such as mobile.com and tablet.com.

    • Make sure the buckets are configured for static website hosting.

  2. Create a CloudFront distribution:

    • Navigate to the CloudFront console and create a new distribution.

    • For the distribution settings, you'll need to configure multiple origin settings, one for each S3 bucket.

  3. Configure multiple origins in CloudFront:

    • Add the first origin: Point this to the mobile.com S3 bucket. You can name this origin something like mobile-origin.

    • Add the second origin: Point this to the tablet.com S3 bucket. Name this origin something like tablet-origin.

  4. Configure origin groups (optional):

    • If you want to add redundancy or failover, you can use origin groups. This step is optional and can be skipped if not needed.
  5. Set up cache behaviors:

    • You need to create cache behaviors that determine which origin requests should be forwarded to based on the request conditions.

    • Create a cache behavior for mobile.com: Set the path pattern to something that will match your mobile site requests, and select the mobile-origin as the target. In the "Host" header field, specify mobile.com.

    • Create a cache behavior for tablet.com: Set the path pattern to match your tablet site requests, and select the tablet-origin as the target. In the "Host" header field, specify tablet.com.

  6. Configure CloudFront to respond to your domains:

    • In the distribution settings, under "Alternate Domain Names (CNAMEs)", add both mobile.com and tablet.com.

    • This tells CloudFront to accept requests for these domains and serve them through this distribution.

  7. Set up SSL/TLS certificates:

    • If you haven't already, you'll need to provision SSL/TLS certificates for your domains through AWS Certificate Manager (ACM) or another certificate authority.

    • Associate these certificates with your CloudFront distribution to ensure secure (HTTPS) connections.

  8. Update DNS settings:

    • Finally, update the DNS settings for mobile.com and tablet.com to point to your CloudFront distribution. This is usually done by creating CNAME records in your DNS settings that point to your CloudFront distribution domain name (e.g., d123456789.cloudfront.net).
  9. Testing:

    • Once everything is set up and the DNS changes have propagated, test accessing both mobile.com and tablet.com to ensure they are served correctly from their respective S3 buckets via the CloudFront distribution.

This setup allows you to use a single CloudFront distribution to serve multiple websites hosted in separate S3 buckets, with routing based on the incoming domain name.