Sorry for the boring technical post. I’m writing this up here so that I can find it the next time I decide to build a silly web page. While I deal with a full-on production environment at work, most of my recreational projects can be done entirely client-side, so Amazon S3 is a great place for me to keep them.
When you read Amazon’s documentation for setting up a static site in an S3 bucket, they recommend using their own Route 53 service for DNS. This makes sense for them because why should they anticipate all the many possible ways you would need to configure some sort of external DNS when Amazon runs their own DNS? However, I did not want to use Amazon’s DNS for the project I set up this morning; I wanted to handle DNS via my domain registrar, Namesilo.
In this situation, DNS is sort of like a logic puzzle. An “A record” (alias) can only point a domain name to an IP address (numeric). If your website lives somewhere with a static IP address, then that is great. But that is not the case with S3; your S3 bucket has a name that remains constant, but it does not have a static IP address.
On the other hand, a CNAME can redirect a subdomain to another location that can be specified by its name. So you can use a CNAME to redirect a subdomain to you S3 bucket.
Here’s the issue: You could redirect something like
www.example.com to your S3 bucket
by using a CNAME, but there is no way for a CNAME or an A record to send plain ol’
example.com to the bucket.
Here’s how I solved it. I’m going to use
example.com in place of my domain name throughout. The big-picture goal here is to have the content all live in a bucket
www.example.com and to have everything redirect to this location.
I made a new bucket with the name
www.example.com, and I uploaded rudimentary
error.htmlfiles. I followed Amazon’s directions on how to get this bucket to serve these files as a static website (using the weird and long Amazon URL). I copied and pasted the recommended public policy from their documentation to set the bucket to public. I pasted the weird and long Amazon URL into my browser to make sure that my content was being served from that location. This part needs to be working before moving on to the next step.
Don’t know if it’s necessary, but I also made a
example.combucket that is not public. I set Properties -> Static Website Hosting to redirect all requests to
Next I logged into Namesilo and chose the option to configure DNS for this domain. They have a template called “WWW redirect”. This redirects the “naked” domain
example.comto the subdomain
www.example.com. I added this template.
Finally, I created a CNAME that sends
www.example.comto the funky AWS S3 URL.
Wait five minutes. It probably won’t work instataneously. If it’s not working after five minutes, it might take a while longer. They say that it could take up to two days, but I’ve never had it take longer than 10 minutes. After 10 minutes, you should be able to type your URL into your browser and be taken to your website hosted on S3.