2

I have an S3 bucket called www.mybucket.com I have an A record: name = www.mybucket.com. It answers well with the index.html contained in the bucket.

Now I want to send any subdomain to that bucket.

I tried first with specific subdomains:

  • an A record ALIAS
    • name: alias.mytestbucket.com
    • alias target www.mytestbucket.com.
    • RESULT: 404, NoSuchBucket, The specified bucket does not exist
  • a CNAME record with NO alias
    • name: cname.mytestbucket.com
    • value: www.mytestbucket.com
    • RESULT: 404, NoSuchBucket, The specified bucket does not exist
  • a CNAME record with alias
    • name: cnamealias.mytestbucket.com
    • alias target: cname.mytestbucket.com. (the only option offered...)
    • RESULT: 404, NoSuchBucket, The specified bucket does not exist

no more ideas...

The final goal will be to redirect all subdomains to the bucket www.mybucket.com. But also ability to send different subdomains to different buckets will be good...

1
  • It's not likely that you should actually be trying this. It should be fine to refer to files in your S3 bucket by a single canonical URL. Jul 7, 2020 at 22:42

2 Answers 2

2

I don't think it's possible. The way this works is that the client resolves www.mybucket.com to a generic S3 endpoint that serves millions of other buckets. It then sends Host HTTP header Host: www.mybucket.com. The S3 service then matches the host header www.mybucket.com to your bucket name www.mybucket.com and serves the index.html file from there. That's how it works.

Even if you make A / ALIAS / CNAME *.mybucket.com pointing to the www.mybucket.com it in the end still resolves to the generic S3 address - that's how DNS works, it doesn't care about any intermediate names.

Now if you try to access xyz.mybucket.com your browser sends Host header Host: xyz.mybucket.com -> S3 can't map it to any existing bucket -> you get NoSuchBucket error.

So no, you can't do it this way.


Possible Workaround: you can use a reverse proxy e.g. on EC2 or in Fargate container and point *.mybucket.com to this proxy hostname. The proxy will then make a backend request to S3 and serve the file to the client. The downside is that everything has to go through your proxy, sacrificing the high-availability of S3 API.

Hope that helps :)

0

@MLu 's answer is correct, I'm just adding a possible workaround.

It does not work for OP's any subdomain requirement, and it's not great for it's not scalable and maintainable, but works good for small website with little-to-none changes over time.

Solution is to force redirect using JS, creating an S3 bucket for every subdomain you want to expose, each one serving a static website (same configuration of the main/original one), containing only a one-liner index.html file. Example:

New bucket with name "some.example.com":

<!DOCTYPE html><html><body><script type="text/javascript">window.location="http://www.example.com"</script></body></html>
New contributor
Hrabal is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .