AWS Route 53 Basics – Subdomains and S3 Buckets

Introduction

In previous post, we started with basics of AWS Route 53 and learnt how to register a domain and use it with a simple static website which is actually an S3 bucket configured to serve static contents.

We can visit the S3 based website on the following URL:

http://awsclouddemos.com/

To recap previous post, we can use Route 53 to associate a custom domain name with S3 bucket.

  • We do that by simply creating an Alias record.
  • Because its an alias , Apex records are allowed with alias in Route 53 (which means you could use example.com as apposed to www.example.com for your custom domain name).
  • Now if you are going to use a simple alias record, then the bucket name must match record name (that means, you have to have the forethought of mind to name your bucket properly so that it will match the desired record name that you are going to create later).

Today, we will take one step further and see how to work with subdomains with help of simple and easy to follow demos.

Requirement

Our static website is up and people can visit it by typing the above shown URL. However, if someone try to visit http://www.awsclouddemos.com/ URL, it will show that site can’t be reached as follows:

(note: you might see that it is working with above mentioned URL and later in post you will see the steps to get this done)

The reason is that www.awsclouddemos.com is a sub-domain and currently we have not configured Route 53 and S3 bucket to handle this address.

We want our users to be able to use www.your-domain-name.com, such as www.example.com, to access our sample website.

Following are steps:

  • Create another s3 bucket, for your subdomain.
  • Set up your subdomain bucket for website redirect.
  • Add an alias record for your subdomain

Create another S3 Bucket, for your subdomain

Create a second S3 bucket (via CLI or directly using web console).

Similar to 1st bucket we created in previous post, the name for the second bucket should match the URL scheme of domain i.e. in this case www.awsclouddemos.com

Set up subdomain bucket for website redirect

Here are the steps to configure the second bucket to route traffic to the first bucket.

  • Select subdomain bucket
  • Choose Properties
  • Under Static website hosting, choose Edit.
  • Choose Redirect requests for an object.
  • In the Target bucket box, enter your root domain, for example, awsclouddemos.com.
  • For Protocol, choose http.
  • Choose Save changes.

Add an alias record for your subdomain

We have seen this in previous post as well. We need to go to public hosted zone area of original bucket and Add an Alias record as shown below:

That’s it. Now, if we navigate to website using URL www.awsclouddemos.com, it will redirect us to awsclouddemos.com and site can’t be reached page will no longer shown.

Additional Sub Domains

If you want to create different sub-domains, you can create buckets for each one of those and then configure those buckets to redirect to the apex domain or add additional alias records in Route 53 to enable that resolution.

Create Wild-Card Records

Wildcard records create synthetic records based on the query:

*.awsclouddemos.com –> www.awsclouddemos.com

Idea: If you have lot of subdomains, you don’t wanna have to create separate records for each. You can actually just create one wildcard record to cover all your bases.

Now, this record is added. However, if you try to test it e.g. by visiting a URL like klm.awsclouddemos.com, it will not work.

But didn’t we just added wildcard record to handle this situation?

The answer is yes. So what is wrong? This might seem an error, but actually it make sense.

Why I am showing this to you if this doesn’t work? The reason is that you might try to expect the similar behavior and actually the way we added a wildcard is correct (we will see example of that in later posts).

Here, the reason is because of S3 bucket, and following is explanation form this link:

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, this was a small detour to explain this issue you may also encounter.

Summary

In this post, we learned how setup subdomains routing to S3 buckets using Route 53 records.

In summary, If you want to create different sub-domains, you can create buckets for each one of those and then configure those buckets to redirect to the apex domain or add additional alias records in Route 53 to enable that resolution.

Let me know if you have some questions or comments.