Domain Handling with Zappa - WWW and Non-WWW Options

The great debate

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Serverless Django with Zappa course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Serverless Django with Zappa, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Serverless Django with Zappa
  • [00:00 - 00:15] So in this module we chose to have the bear or apex domain zappaguide.com as our primary site. If you go up and click on the browser website you'll see there's no www.prefix in front of our website. This is a matter of personal preference.

    [00:16 - 00:54] There's no right or wrong answer. In fact a lot of browsers are now moving to removing the www on display. Check out newline.co If you click on the site the browser hides the www in front of newline.co and when you click out of it it just shows you the bear domain name even though you went to the www version. If we go to www.zappaguide.com you'll notice it is not working. We would need additional configuration to make this work. So how do you get both the bear domain and the www version working?

    [00:55 - 02:21] There are generally two approaches. The first approach is using an HTTP redirect. If the user goes to www.zappaguide.com that endpoint can send the browser an HTTP 301 code to tell the browser to redirect to the bear or apex domain. While this works fine you would need an HTTP server to be able to send the browser the 301 code. That means you're setting up a whole web server just to send a code to redirect the browser. If you happen to have a web server lying around that may be convenient but there's another approach that we're going to talk about that may be more convenient without setting up another server. The other approach we're going to use is to alias one site to the other . That means both zappaguide.com and www.zappaguide.com work and they use the same zappa project. It's like two doors that go into the same room. The browser's address bar doesn't change and the user operates on the site normally. One small caveat of this approach is to be a little more careful if you're using cookies to make sure they are shared between the two domains. Going down this route is a little more complex. We need to make some advanced configuration to the AWS services but it can be done.

    [02:22 - 02:53] This lesson is entirely optional if you don't need or want both www and the bear apex working but here's how to do it. First we go to our ACM certificate. You remember a few lessons ago when we set one up? Well we added zappaguide.com and we also associated the domain name www.zappaguide.com. Believe it or not for certificates these are considered different domain names.

    [02:54 - 04:00] So it's a good thing we did that otherwise we'd have to go back and reissue the certificate to have both. Oh and while we're at it let's make sure we add the new domain name to our Django settings so we don't get hung up on this again. Let's go ahead and put the dub dub version in our allowed host save this and meanwhile while we're doing the rest of our configuration in Amazon we'll update our zappa project. All right now that's going let's go back to Amazon console. The next service we have to hop to is the API gateway service. A lot of this is under the hood mechanics that zappa project has set up for you but we're going to make some changes to here. First let's click on the left where it says custom domain names and then click on your domain name that's relevant here which is zappaguide.com. We'll notice a few things about this endpoint. It's an edge endpoint and we'll have to remember that later. Also it's TLS 1.0 and finally the API mappings are pretty straightforward.

    [04:01 - 04:40] Now let's create our own domain name. Here in the domain name we're going to enter dub dub dub zappaguide.com. We're going to keep TLS 1.2 and we're going to switch our endpoint type to be an edge optimized type. Finally we're going to choose a certificate that we've already created an ACM. There should only be one that you recognize for your custom domain and at that point you can go ahead and create the domain name. Great now we need to add some API mappings.

    [04:41 - 05:03] So let's click on configure API mappings. We'll essentially need one mapping so that all the web requests go to the proper stage. So we're going to add a new mapping. We're going to add our active project course. I have a couple in here. We're going to pick our stage and leave the path blank and then say save.

    [05:04 - 05:28] Great. So that takes care of our API gateway. Let's move on to the next service . The next service is route 53. We're going to have to change some DNS records to make sure www is routed to the correct location. Okay, here we are in the hosted zone for zappaguide.com.

    [05:29 - 05:45] I'm going to click on create record. The record name that we want to create is for the www record. All right, for this record type, we're going to select CNAME. The value is going to be zappaguide.com.

    [05:46 - 06:20] The TTL is time to live in terms of seconds. Odds are we're not going to be making changes very much to our record. We're going to put in two days here, which makes our number of queries from clients a little less. And then we're going to go ahead and create records. Awesome. That's created successfully. Now we should see www zappaguide.com listed in our records.

    [06:21 - 06:46] And finally, let's check to make sure our update finished. And it did. Let's try to reload www.zappaguide.com. And look at that. It now works. So this process was a little complicated, but not too bad, especially if you want both www and the apex or bear domain working on the same zappa project.