Simple HTTP Redirects Using AWS Route 53 and S3

Let’s just say that you want to create a redirect for a root domain while using Route 53 on Amazon Web Services.  Many of the popular domain registration and web hosting providers (e.g. GoDaddy, Domain.com, etc.) have baked in settings to redirect to another URL.

In Route 53, there is no native way to do this, but luckily it’s a couple of simple steps away and you also get to learn a little about web site hosting on AWS S3 (Simple Storage Service) at the same time.

My example is for http://virtualdesignmaster.com which has been redirected to http://virtualdesignmaster.io as you can see by clicking the link.  Now, let’s learn how to do that easily with AWS Route 53 DNS service and S3.

Our assumption is that you’ve registered the domain with Route 53 or are at least hosting the DNS zone and have set the SOA (Start of Authority) record and NS (Name Server) records to AWS DNS settings.

Setting up Your S3 Buckets

The reason that I say buckets as a plural, is that we will want to have a working DNS for both the root domain virtualdesignmaster.com and also the www.virtualdesignmaster.com which is normally a CNAME entry to point to the root domain.

Open up your AWS S3 and click the Create Bucket button.  Name your S3 bucket the same as your domain name.  In my case for the example is virtualdesignmaster.com.  I’ve chose to put it in the US Standard region because it’s the closest to the majority of my traffic for the site:

01-create-bucket-base

 

After you click the create button, you need to click the Properties button on the upper right side of the page.  Expand the section which says Static Website Hosting, select the Redirect all requests to another host name radio button, and type the URL that you want to redirect to.  In my case it is www.virtualdesignmaster.io:

02-bucket-redirect-base

Save the changes and repeat the process, but this time you will name the S3 bucket as the www name for your domain.  My example uses www.virtualdesignmaster.com:

03-create-bucket-www

Open the Properties section, expand the section which says Static Website Hosting, select the Redirect all requests to another host name radio button, and type the URL that you want to redirect to:

04-bucket-redirect-www

That is your S3 configuration.  Next up is the DNS settings.

Route 53 Configuration for S3 Targets

Open up your Route 53 configuration in the AWS console.  Select the Hosted Zones area, choose the radio button for the zone we are working on and click Go to Record Sets:

route53-vdm

Click Create Record Set.  Leave the Name section empty, choose A – IPv4 address as the Type:

05-create-a-record-base

Change the Alias from the default of No to Yes:

06-alias

Click in the Alias Target field which brings up a droplist.  It may take up to 20 or more seconds to populate the list.  Under the S3 website endpoints you will see the one you’ve created.  Select that and click the Create button at the bottom:

06-route53-base

Repeat the process of adding a new record.  This time put www in the Name section.  Keep A-IPv4 address as the Type, and click the Alias Target section to select the S3 website endpoint which matches the A record name:

07-route53-www

DNS is a rather magical system, but it will take up to 15 minutes or more update.  Once some time has passed, you can confirm te redirection is in place by trying the URL in a browser.  It should redirect you to the web site that you’ve assigned in your S3 configuration.

You can confirm from the command line using cURL as well.  Type curl --head yourdomain.com which will show you the output below, and then you can also type curl yourdomain.com which will show you the redirect in the output:

08-curl-redirect

Note that this is different than a 301 or 302 redirect.  The method used by the S3 redirection is a META tag instead.  I’m also assuming that you’re aware that there is a cost for the S3 usage.

Costs of Using S3 for Web Site Hosting and Redirection

As listed on the S3 and storage pricing site, the S3 usage qualifies for a Free Tier to AWS users up to the first 20,000 HTTP GET requests per month for the first year of usage.

aws-s3-free-tier

After your Free Tier usage expires, you are charged 0.004$ per 10,000 HTTP GET requests per month in the US East region.  Pricing across regions can be different, so make sure to take that into account when you choose the location of your S3 buckets:

aws-s3-pricing-GET

Hopefully this helps, plus it’s also handy to know as part of your study towards the AWS Solutions Architect Associate exam.

7 thoughts on “Simple HTTP Redirects Using AWS Route 53 and S3”

    • Hi Varun,

      You can also achieve the same result using the CNAME and pointing to the FQDN URL of the S3 target for the root bucket. I prefer the dynamic mapping because it also makes sure that during a failover or any change behind the scenes that it is done automatically as a clean redirect.

      Hopefully that answers it for you.

      Thanks!

      Eric

      Reply
    • Not when using an alias for the A record, AFAIK. The Route53 A record alias doesn’t reference the full S3 URL for the static website end point that includes the bucket name (http://mybucket.s3-website-eu-west-1.amazonaws.com). Instead it simply references the regional S3 end point (s3-website-eu-west-1.amazonaws.com) and does some clever bucket reference behind the scene (not completely sure how – maybe someone else can expand). Since you can’t provide a domain name for an A record, you can’t use the full S3 static website name either. So to redirect from a sub-domain to the apex when hosted in an S3 bucket, you’ll need to do the re-direction at the S3 level, not the route 53 level.

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.