Amazon Cloudfront with S3 Redirect

9,460

Solution 1

Solution: Setup the Redirect as stated in the question then wait out the S3 and CloudFront cache times. They can be 4 hours or more, so you just have to set it all up then wait and hope for the best.

(This is Michael's solution from the Comments, but it's now been years and this really deserved to be marked as Answered).

Solution 2

If you have this issue, check first when you configure the s3 bucket origin for the cloudfront, the autocomplete returns the s3 REST endpoint domain.amazonaws.com which return this ListBucketResult response.

You have to write down manually the website endpoint domain.s3-website-region.amazonaws.com

Important: If you have wrongly configured cloudfront with the REST endpoint you have to invalidate the cache through Invalidations or it will keep returning the REST response

Share:
9,460
Chris Moschini
Author by

Chris Moschini

I'm a Web Consultant, and I build Web Applications. My technology expertise revolves around: jQuery/Javascript, CSS, ASP.Net MVC/C#, and EF Code First/LINQ. I'm competent in other languages like SQL, Java, and VB. You can email me at my first name at my company website.

Updated on September 18, 2022

Comments

  • Chris Moschini
    Chris Moschini over 1 year

    I'm trying to do something that should be relatively straight-forward - I want to setup a few domains and subdomains to redirect to the core domain for our site, but I want to put the redirects out on Cloudfront. Everything works except redirecting the root path - that gets me an XML file partly-describing the S3 bucket.

    Background

    S3

    S3 allows you to setup an all-redirect bucket, like:

    S3 with Redirect all requests setup

    Testing this, the web endpoint (brass9-com.s3-website-us-west-1.amazonaws.com) does what it should - it redirects to brass9.com. Good.

    Cloudfront

    Cloudfront lets you point at an S3 bucket, but the way it suggests you do so is wrong - instead of pointing at the bucket by its name, like brass9-com.s3.amazonaws.com, you need to use the Web Endpoint above. Other than that you can leave everything at its defaults and get good redirect behavior. So, a path like www.brass9.com/portfolio properly redirects to where it should. Also good.

    The problem - the root domain redirect

    The one thing that then fails to work is redirecting from plain www.brass9.com. Instead of getting you a redirect it gets you this odd result:

    <?xml version="1.0" encoding="UTF-8"?>
    <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <Name>brass9-com</Name><Prefix></Prefix>
    <Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated>
    </ListBucketResult>
    

    OK... . So this is not completely unexpected - because there's no Default Root Object. But what Default Root Object would I possibly specify to prevent this behavior? What is the name of the S3 redirect object, if any, that I need to point to? Or is there some other proper config, or is this just a bug in how Cloudfront and S3 interact that Amazon needs to fix?

    Known Not-Working Solution: Default Root Object

    It is possible to specify a Default Root Object of index.html, but it doesn't help any - it just changes the problem. The Cloudfront url instead redirects to /index.html on the main site, which is a 404 (we don't use an index.html file, it's a server-side framework driven site). I could put an index.html on the server, but that defeats the small speed gain of using Cloudfront in the first place.

    Similar questions

    One question asks something similar but gets back a blank, 0-byte response for some reason instead of what I'm seeing, an XML response. It doesn't include a question about that problem or a resolution.

    Related articles

    One article proposes you just serve the entire site out on both the bare and www domain. This screws with any user's bookmarks, your search ranking, etc, etc. You should not do this.

    Several discuss hosting a static website on S3 and Cloudfront rather than a redirect scheme, and so are unrelated.

    So, how do I do this properly?

    Screenshot of Cloudfront Configuration - no Default Root Object, pointing at S3 origin. Ignore the InProgress - I had just toggled Default Root Object on and off to test.

    Cloudfront Distribution with no Default Root Object pointing at S3

    And the Origin config for that Distribution:

    Origin - points to S3, defaults

  • Chris Moschini
    Chris Moschini almost 10 years
    You can (although some API libs forbid it) - but using a blank Default Root Object gets me the ridiculous ListBucketResult XML response you see in my Question.
  • Nate
    Nate over 5 years
    The note about cache invalidation finally helped me solve a problem I've chased for several days. Thank you!
  • dusan
    dusan over 5 years
    Thanks a lot! For Terraform users, in the origin block of the distribution, use aws_s3_bucket.BUCKET.website_endpoint in the domain_name (not bucket_regional_name) and add the custom_origin_config block.
  • Dale Zak
    Dale Zak over 4 years
    You are a lifesaver! We've been struggling with this problem for the last day, kept getting ListBucketResult XML back even though we were changing the distribution settings. Finally after deleting the distribution and creating a new one pointing to domain.s3-website-region.amazonaws.com the redirect worked for us!
  • Walter Silva
    Walter Silva almost 3 years
    thanks, very helpful!