Is it possible to setup DNS for s3 using multiple buckets for a single domain? - amazon-s3

Is there a way to use another bucket name when hosting a site (or indeed any content) than just www.example.com.s3-region.amazonaws.com? I want to use multiple buckets so that when I update the site I can rollback a version if problems arrise and so that updates are an atomic switch between site versions. I only want one bucket used for a domain at a time.
I.e. something like
Bucket Names:
www.example.com.bucket1
www.example.com.bucket2
Procedure:
www.example.com currently points to -> www.example.com.bucket1.s3-region.amazonaws.com
New site version is uploaded to www.example.com.bucket2.
Once verified DNS is changed so that www.example.com points to -> www.example.com.bucket2.s3-region.amazonaws.com

This should not work because S3 looks at the hostname of the request (www.example.com) to find out what bucket you're trying to access so the bucket has to have the same name.
But it is possible to achieve what you want with Amazon CloudFront. There are two options:
You can create a single distribution and only update the origin of it (the S3 bucket).
You can create two different distributions and update the DNS settings to point to the desired distribution. You would also need to update the CNAME properties in both of the distributions (remove www.example.com from the old distribution and add it to the new one).

Related

Configure Cloudfront with multiple s3 origins using different domains

How can I configure a Cloudfront distribution to use multiple s3 origins with same hierarchy but using different domain names?
Currently I have a Cloudfront distribution with a distribution domain name for example xyz.cloudfront.net.
The distribution has been configured to use an alternate domain e.g. assets.example.com and to serve content using that domain I've added cname record in my DNS management console that maps assets.example.com to xyz.cloudfront.net.
Now this set up works fine when serving content from a single s3 origin as i can call something like assets.example.com/images/my-image.png
However I want to configure 3 s3 origins as follows which have identical hierarchies i.e. they all have an images folder:
dev-bucket.s3.eu-west-2.amazonaws.com
test-bucket.s3.eu-west-2.amazonaws.com
live-bucket.s3.eu-west-2.amazonaws.com
If I've configured assets.example.com to map to the distribution xyz.cloudfront.net, how is cloudfront going to know which origin to serve from?
basically if im running the dev website i want cloudfront to serve content from the dev origin and if im running the test site then i want it to serve using the test origin.
The only way i can see how can i achieve this is creating 3 different cloud front distribution for each environment and map different domains to the distribution e.g. assets-dev.example.com, assets-test.example.com and assets.example.com for the live site.
Any advise appreciated.

Hosting multiple S3 origins under one CloudFront distribution

Is it possible to host multiple S3 origins under the same CloudFront distribution? Let's say I have bucket A and bucket B which both host static websites--could I add them both as origins to a distribution, and specify a path e.g. /alternate for the origin from bucket B so that visiting http://<distribution>/ took you to the website hosted by bucket A and visiting http://<distribution>/alternate brought you to the one hosted by bucket B? From what I've read so far it seems possible to have multiple origins, not necessarily multiple S3 origins.
You can add multiple S3 origins to a CloudFront distribution
However, behaviors are locked to a specific origin. Behavior controls the path. So, if /static is used by Origin A/S3 A, you can't have /static go to /Origin B/S3 B
Someone suggested using multiple CloudFronts with the same CNAME. That does not work either. CloudFront will not allow the same CNAME to be used in multiple distributions.
Academically speaking, this may be possible through LambdaEdge or some other external process that is checking S3 A, and if it is unavailable (say - under maintenance while new code being pushed), it can go and update CF Behavior for /static to Origin B/S3 B.
I wish AWS came up with a more elegant solution to this use case. Like allowing same CNAME in multiple distributions, or providing some kind of R53 integration with S3 buckets (never tried putting two different S3 buckets behind R53 .. probably will not work because the host header would change)
You can attach a Lambda function as a trigger to intercept CloudFront's routing. The implementation is quite involved; but would permit your specific use case.
Example Implementation:
https://aws.amazon.com/blogs/networking-and-content-delivery/dynamically-route-viewer-requests-to-any-origin-using-lambdaedge/
Lambda#Edge:
https://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html

How do I map multiple domains to the same bucket on Amazon S3?

Is it possible to do that? I need to be able to access mydomain.com by typing in my-domain.com in the address bar of the browser?
Now I added a DNS entry:
my-domain.com CNAME mydomain.com
But this doesn't seem to work. I get an 404 not found error.
You can only map a single domain to your S3 bucket. However you could use Cloudfront to do this.
See my answer to another similar question for more information.
We had the same issue and basically I set our CI to publish to two S3 buckets on release. Not ideal but keeps you clear from resetting caches in CloudFront on publish for the short term.

How to setup CNAME for Amazon S3 at Namecheap

I am using amazon S3 to host one of my static sites and wanted to link it to my domain name (domainname.co.uk). So I went into namecheap account-all host records and did something like this,
However, It still doesnt work and throws 404 when I go the website url. By the way, under the "www" option, I am using my S3 url like this, conxxxxxxxxxxx.co.uk.s3-website-eu-west-1.amazonaws.com..
Notice the dot in the end, which is automatically added by Namecheap whenever I try to save, even without the dot. I am not sure if that's causing the issue but it just doesn't work for me.
Going directly to my AWS URL works fine, which implies that something is wrong with my CNAME setup.
Anyone know what am doing wrong here? Namecheap support had absolutely no clue either.
You can only use custom CNAMEs for Amazon S3 if the bucket name matches the CNAME.
For example, if your bucket is named:
files.example.com
and is therefore accessible by default at:
files.example.com.s3-website-us-east-1.amazonaws.com.
A CNAME from files.example.com to the full bucket domain name will allow you to use your custom domain.
However, if your bucket name is not exactly the same as the CNAME you are trying to define, it will not work. In your screenshot, you are trying to use www.... as your CNAME, but the (redacted) bucket name does not contain www.. Note that "exactly" includes case-sensitivity; your bucket name must be all lowercase for a CNAME to work.
The full documentation of this feature is here: http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html#VirtualHostingCustomURLs
If you want/need multiple CNAMEs, and/or a CNAME that does not match the bucket name, Amazon CloudFront allows you to specify arbitrary CNAMEs for a deployment.
Its been a while since this questions was asked , but if anyone looking for NameCheap CNAME setup for static website in amazon AWS s3 bucket then please refer screenshot below.
This setup for AWS S3 bucket hosting + NameCheap DNS record is working for me as of Jul 2019.
This setup is for pointing AWS S3 static website endpoint to your custom domain in NameCheap.
Please note if you are using AWS CloudFront SSL certificate then your CNAME record value will be the CloudFront domain name ( not the static website endpoint).

Cloudfront dist on top level domain

Is it possible to point a top level domain like http://example.com to a amazon cloudfront distribution?
I know it's possible with CNAMEs, but as far as I know, I need to set an A-name record for the top level domain in the DNS settings.
As explained by #dgeske, this can be done.
In my case, I had not purchased the domain from Route 53, and hence had to do extra configuration.
Scenario: You have the following
Cloud front distribution
A second-level domain (example.com) not purchased from Amazon Route 53. It was Google domains in my case, but the idea will work for other providers also.
You want to point the second-level domain (example.com) to the cloud front distribution (as opposed to a subdomain like www.example.com)
Your nomenclature is slightly inaccurate. example.com is not a TLD (top-level domain), it is what is called a second-level domain. See the following image.
Steps to do this.
Create a hosted zone in Route 53.
Route 53 will now give you a list of name servers that you have to set in the domain settings panel of the provider from which you purchased the domain (Google domains in my case).
Go back to Route 53 dashboard, and create an A - Alias record for this hosted zone (use create record set option). Remember to select 'Yes' radio button. Make sure you leave the subdomain part empty (since we are only interested in creating record for second-level domain).
Now you should be able to access your cloudfront distribution at http://example.com.
Depending on your DNS server, it may take a while to get records updated.
You may configure your system to use a public DNS server such as 8.8.8.8 to verify if you are able to access the cloudfront distribution using the URL. I used firefox's DNS over https feature for this. This makes firefox use cloudflare's (not cloudfront) DNS servers. You can also use dig command line utility dig #8.8.8.8 example.com (My domain was fightcoronapune.com, hence, dig #8.8.8.8 fightcoronapune.com) (telling dig to use 8.8.8.8 DNS server to resolve names)
You may additionally get Access Denied error, in which case you will have to configure the default root object for your cloudfront distribution. So that when you visit http://example.com the file http://example.com/index.html is served to you (assuming you specified index.html as default root object). This error has nothing to do with the steps we did above, and you will still get this error even if you directly use your cloudfront distribution's URL given by Amazon (eg. when you go to http://abcd.cloudfront.net directly, instead of going to http://example.com).
Q. Can I point my zone apex (example.com versus www.example.com) at my Amazon CloudFront distribution?
Yes. Amazon Route 53 offers a special type of record called an ‘Alias’ record that lets you map your zone apex (example.com) DNS name to your Amazon CloudFront distribution (for example, d123.cloudfront.net). IP addresses associated with Amazon CloudFront endpoints vary based on your end user’s location (in order to direct the end user to the nearest CloudFront edge location) and can change at any time due to scaling up, scaling down, or software updates. Route 53 responds to each request for an Alias record with the IP address(es) for the distribution. Route 53 doesn't charge for queries to Alias records that are mapped to a CloudFront distribution. These queries are listed as “Intra-AWS-DNS-Queries” on the Amazon Route 53 usage report.
Source: Amazon Route 53 FAQs
My understanding is that you cannot create an A record for Cloudfront.
Amazon provides you with a domain name like YourName.cloudfront.net. They need to manage the DNS resolution for that domain name behind the scenes in order to route each request to the nearest edge server.
you can if you add alias in cloudfront
then select A or AAAA(ipv6 if enabled on cloudfront)