28
votes

I want to host a static website on S3 , i.e. abcd.com. It requires a bucket name the same as abcd.com.

Then I found abcd.com had been occupied by other people.

So that's my question, why bucket name must be the same as the website name? For there are Route 53 to map the website to the bucket endpoints, it appears no necessary for this limitation.

Is there any reason for this ?

3

3 Answers

16
votes

The brief answer is, "that's how Amazon designed it."

If the bucket name weren't the same as the domain name, how would S3 know which bucket to use to serve requests for a given domain?

You can't say "Route 53," because S3 was created before Route 53, and web site hosting in S3 works the same even if you aren't using Route 53 for DNS.

Similarly, it can't be a configuration option on the bucket, because that would just create a new series of problems -- if the previous owner of a domain still had their bucket configured with your domain, you'd have exactly the same problem as you do, now.

You can still host your site on S3, but with a mismatched bucket name, you need either a reverse proxy server in EC2 in the same region, to rewrite the host header in each request to match the bucket name, or, you can use CloudFront to accomplish a similar purpose, because the bucket name, then, does not need to match -- CloudFront will rewrite the Host header also.

3
votes

There's a pretty simple reason for this: by the time Amazon gets the request from your browser, the main information available is the domain in the URL, which isn't enough to figure it out.

Suppose your site is example.com, but that bucket name is taken, so you make the bucket my-example. Then you'll have an URL something like http://my-example.s3-website.us-east-1.amazonaws.com/. That will work just fine in your browser, because it gets resolved to some AWS web server, which looks at the Host HTTP header, pulls out your bucket name, and grabs your bucket content.

Now suppose you add something to Route53 to make example.com work. You can either add A records, which let your browser turn example.com directly into an IP address for some AWS S3 webserver. Or you can put in a CNAME, which points from example.com to the full my-example hostname. Either way, your browser's going to look up an IP address, contact an Amazon webserver, and send a Host header that just says example.com. So if that isn't the bucket name, it doesn't know what to do.

Admittedly, it could go an extra step. After all, you told it the hostname when you set up the bucket for serving websites. So at first thought, it seems like it would be nice if it used that as well. However, that won't really solve your problem either, because whoever set up the example.com bucket could well have set it up for hosting.

It seems like the best way to work around this is Cloudfront, which can associate domain names with arbitrary buckets.

2
votes

I think is the way AWS have designed this and that's it : Check this

I have this done for my company's website and it works great!

Create an S3 bucket and configure it to host a website

Amazon S3 lets you store and retrieve your data from anywhere on the internet. To organize your data, you create buckets and upload your data to the buckets by using the AWS Management Console. You can use S3 to host a static website in a bucket. The following procedure explains how to create a bucket and configure it for website hosting.

To create an S3 bucket and configure it to host a website

  • Open the Amazon S3 console at https://console.aws.amazon.com/s3/.
  • Choose Create bucket.
  • Enter the following values:
    • Bucket name - Enter the name of your domain, such as example.com.
    • Region - Choose the region closest to most of your users.
      • Make note of the region that you choose; you'll need this information later in the process.
        • Choose Next.
        • On the Configure options page, choose Next to accept the default values.
        • On the Set permissions page, uncheck the Block all public access check box, and choose Next.

Note The console displays a message about public access to the bucket. Later in this procedure, you add a bucket policy that limits access to the bucket.

  • On the Review page, choose Create bucket.
  • On the list of S3 buckets, choose the name of the bucket that you just created.
  • Choose the Properties tab.
  • Choose Static website hosting.
  • Choose Use this bucket to host a website.
  • For Index document, enter the name of the file that contains the main page for your website.

Note You'll create an HTML file and upload it to your bucket later in the process.

  • Choose Save.
  • Choose the Permissions tab.
  • Choose Bucket policy.

Copy the following bucket policy and paste it into a text editor. This policy grants everyone on the internet ("Principal":"*") permission to get the files ("Action":["s3:GetObject"]) in the S3 bucket that is associated with your domain name ("arn:aws:s3:::your-domain-name/*"):

{
   "Version":"2012-10-17",
   "Statement":[{
      "Sid":"AddPerm",
      "Effect":"Allow",
      "Principal":"*",
      "Action":[
         "s3:GetObject"
      ],
      "Resource":[
         "arn:aws:s3:::your-domain-name/*"
      ]
    }]
}
  • In the bucket policy, replace the value your-domain-name with the name of your domain, such as example.com. This value must match the name of the bucket.
  • Choose Save.

Create another S3 Bucket, for www.your-domain-name

In the preceding procedure, you created a bucket for your domain name, such as example.com. This allows your users to access your website by using your domain name, such as example.com.

If you also want your users to be able to use www.your-domain-name, such as www.example.com, to access your sample website, you create a second S3 bucket. You then configure the second bucket to route traffic to the first bucket.

Note Websites typically redirect your-domain-name to www.your-domain-name, for example, from example.com to www.example.com. Because of the way S3 works, you must set up the redirection in the opposite direction, from www.example.com to example.com.

To create an S3 bucket for www.your-domain-name

  • Choose Create bucket.
  • Enter the following values:
    • Bucket name - Enter www.your-domain-name. For example, if you registered the domain name example.com, enter www.example.com.
    • Region -Choose the same region that you created the first bucket in.
  • Choose Next.
  • On the Configure options page, choose Next to accept the default values.
  • On the Set permissions page, choose Next to accept the default values.
  • On the Review page, choose Create bucket.
  • n the list of S3 buckets, choose the name of the bucket that you just created.
  • Choose the Properties tab.
  • Choose Static website hosting.
  • Choose Redirect requests.
    • Enter the following values:
      • Target bucket or domain
      • Enter the name of the bucket that you want to redirect requests to. This is the name of the bucket that you created in the procedure To create an S3 bucket and configure it to host a website.
      • Protocol - Enter http. You're redirecting requests to an S3 bucket that is configured as a website endpoint, and Amazon S3 doesn't support HTTPS connections for website endpoints.
  • Choose Save.