Amazon S3: Static Web Sites: Custom Domain or Subdomain - amazon-s3

Amazon.com just announced that one can host static web sites in a S3 bucket. I went to their setup page at http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?WebsiteHosting.html and created a bucket for my static web site, and it worked fine. I have an URL of the form http://[my bucket name].s3-website-us-east-1.amazonaws.com/.
However, I would like to point a subdomain that I own (e.g. static.mydomain.com) to my static web site at Amazon S3. Has anybody figured out how to do that?
I appreciate any help you can give me.

It turns out that to make it work, you cannot just map any arbitrary subdomain to any arbitrary bucket. The fully qualified subdomain name must be the same as the S3 bucket name.
Suppose the name of your site is static.mydomain.com. Then you need to create a S3 bucket with that same name, named static.mydomain.com.
Once you configure that bucket as a S3 static web site, it will have a URL assigned to it that looks something like http://static.mydomain.com.s3-website-us-east-1.amazonaws.com.
Go to your domain host and map your subdomain to the URL from step 2. In enom.com, that meant mapping the host "static" to the address "static.mydomain.com.s3-website-us-east-1.amazonaws.com" as a CNAME record.
Thanks to Uriah and David for suggestions. I eventually got my answer at an Amazon AWS forum.

Make sure that you follow the S3 DNS bucket naming conventions when creating your bucket.
Then:
Enable S3 Website support for the bucket using the AWS Management Console at Properties --> Website
Make sure the files in the bucket have public read permissions
Try the website using the endpoint listed in the Management Console
Setup your CNAME using the listed endpoint (e.g. www.example.com CNAME www.example.com.s3-website-us-east-1.amazonaws.com)

Create a hosted zone say mydomain.com. You can use a web based UI DNS30.
Now create a CNAME entry for this hosted zone.
Add a resource record
with following detail Name as "mydomain.com"
Type as "CNAME"
Value as "mydomain.com.s3.amazonaws.com"
TTl as "on your required configuration"
Hope this will also work for static.mydomain.com
Regards-
Udit

This is called Virtual Hosting of Buckets: http://docs.amazonwebservices.com/AmazonS3/latest/dev/VirtualHosting.html

How about using a DNS service, like the Route 53 that Amazon provides. Set it up with a CNAME from your sub domain to the public bucket URL. Of course, if your domain company provide CNAME configuration, that could work directly.

Update 2019 : AWS custom subdomain hosting in S3
As of today following steps worked for me to have a working subdomain for AWS S3 hosted static website:
Create a bucket with subdomain name. In this example www.subtest.mysite.com
Note: Make sure on 'Permission' tab of bucket:
-Block public access (bucket settings)
-Access Control List &
-Bucket policy
are appropriately set to make sure bucket is public. ( Assuming you already did this for your root domain bucket, those settings can be mirrored on this subdomain bucket)
Upload the index.html file in the bucket
Create a CNAME record with your domain provider

Related

Webserver and Cloudfront distribution with the same domain name

We have a (django) wep application that is running at example.com and we would like to serve some static assets on s3 via cloudfront from this same domain. So if we had a file with key assets/img.jpg we would be able to access it via example.com/assets/img.jpg.
We have been attempting to use this guide but have only been able to get it working with a subdomain to access cloudfront so static.example.com/assets/img.jpg
The issue we are running into is the DNS setup for this, there is already a CNAME for example.com (web app) that routes traffic to the server, but we are unable to create a second entry with the same name example.com to the cloudfront distribution.
In order to do this, go to the configuration for your Cloudfront distribution.
From there, you need to create another "origin" that points to that S3 bucket, and then a "behavior" for the "/assets/*" path to send the traffic to that S3 bucket.

Subdomain point to Amazon S3 Bucket

I am a little bit confused with subdomain point to S3 bucket.
I have domain called mydomain.com from NameCheap.com and my hosting server is Hostgater.
So my website is up and running.
I have a lot of mp3 files. So I have created Amazon S3 bucket call
media.mydomain.com
So I got my static s3 website URL as follow.
http://media.mydomain.com.s3-website-ap-southeast-1.amazonaws.com/
I want to point this url to my subdomain, media.mydomain.com.
May I know how and where to point? NameCheap or Hostgator.
I have created subdomain in hostgator but it can only redirect to S3 URL
Thanks,
Alex
You can make a new DNS record in Namecheap for this task
In your Namecheap account, go to Manage your domain, then Advanced DNS, and Add new Record with CName type
Host: Enter your subdomain media
Value: Enter media.mydomain.com.s3-website-ap-southeast-1.amazonaws.com. (with a dot . at the end)
TTL: Automatic
Save all changes and wait for about 1 hour for the new record to take effect.

URL for Amazon S3 bucket prefix with othername

S3 buckets my url where i am uploading file abcd-ss.s3.amazonaws.com//
but where i want to upload it s3.amazonaws.com//
that should not prefix with abcd-ss.
So i hereby cant understand from where it is coming (abcd-ss.) how to remove it can you suggest me?
You want to use s3.amazonaws.com as your bucket URL? It's not possible.
If you want to use a different name for your bucket URL, this could be implemented
You need to follow these steps:
Regsiter new domain in Route53 or migrate your existing one.
More information here: http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/registrar.html
After you registerd your domain, for exmple mydomain.com, create an S3 bucket with exactly the same name and enable static website hosting on it.
After that, you will be able to point your Route53 bomain to your bucket and have a user-friendly URL. More information here:
http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/RoutingToS3Bucket.html

Why must the Amazon S3 bucket name be the same as website name when hosting a static website

I want to host a static website on S3, i.e. example.com. It requires a bucket name the same as example.com.
Then I found example.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?
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.
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.
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.

Setting up a CNAME for an S3 bucket using Amazon Route 53

I need the following url
http://liangpai.s3.amazonaws.com/uploadImage/shareImage/2012-10/506dc1ef71dcf.png
showing as:
http://imgs.liangpai.me/uploadImage/shareImage/2012-10/506dc1ef71dcf.png
My S3 bucket name is “liangpai”. On Route 53, I created a new CNAME named “imgs”, the value I entered is “liangpai.s3.amazonaws.com”
After this I don’t see the S3 web link changes to my domain link.
Anything wrong here?
Thanks for your advice!
S3 CNAMEs require the bucket name to match the host name. If you want S3 to serve from imgs.langpai.me, you need to create a bucket named imgs.langpai.me. See the S3 documentation for more.
You could create a CloudFront distribution to answer to imgs.langpai.me (see CloudFront CNAME support) have it and forward requests to langpai.s3.amazonaws.com. This would, of course, also incur CloudFront charges.
Method 1 : S3 direct CNAME by bucket name
You can create a CNAME record by bucket name following these steps.
Create a bucket name using the same name you will use for your dns entry. i.e. subdomain.mydomain.net
Create a cname record for subdomain.mydomain.net to s3.amazonaws.com
Set permissions on your bucket based on the access you plan to grant
Method 2 : Static Website on S3
If you are serving static content such as images, files, etc. you can use "Static Website Hosting" for your S3 bucket.
Select your bucket by clicking the bucket icon () and select the "properties" tab on the right and expand "Static Website Hosting". In the Static Website Hosting panel, select the radio button to "Enable website hosting" and set an index and error document. It this point, you will see an "Endpoint" name in this panel. Simple create a DNS CNAME record with the endpoint as the value. You can read the entire Amazon Walkthrough
here.
Try this
Using your own bucket CNAME:
echo S3::getAuthenticatedURL("s3bucket.mydomain.com", $uri, 3600, true);
but you putobject file check for read for Everyone
premisssion type:
S3::ACL_PUBLIC_READ
$s3->putObjectFile($tmp, $bucketname , $path.$filename, S3::ACL_PUBLIC_READ)