How to Deploy a Django App to the Cloud with AWS Lambda
Ready to get into the cloud?
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo 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.
Get unlimited access to Serverless Django with Zappa, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

[00:00 - 00:11] Okay folks, let's do our first deployment. In order to do this, we'll run the z appa deploy command. We'll need to run the deploy command for the initial deployment of any zappa environment.
[00:12 - 00:38] Because we are essentially in development mode, we'll start off with a zappa environment called dev. The zappa deploy command is going to create all the necessary AWS infrastructure that we'll need. Once it's complete, any updates to the code can be done using the update command. We'll explore the zappa update command later. So let's go ahead and kick off our deployment.
[00:39 - 01:11] Oh, looks like we ran into an error. It's down at the bottom here. No region error. And the explanation is you must specify a region. The zappa utility needs to know where an AWS would you like to deploy? Since AWS has a worldwide footprint, we're going to have to select one. So that's pretty straightforward. Let's go ahead and go to our zappa settings.json file.
[01:12 - 01:41] It's easy enough to add an AWS region here. So let's go ahead and do that. Now I'm in Northern Virginia. So we're going to use us east one as our region. So let's save that. And we'll go back and try again. This may take a little while for it to finish. So be patient.
[01:42 - 02:09] The zappa utility is actually creating a number of artifacts in AWS to get your project up and running. And just like that, it's finished. It gives you a message at the bottom saying deployment complete and this long URL. So let's load this long URL into our browser and see what happens.
[02:10 - 02:53] Uh oh, that doesn't seem right. Well, the good news is it's working. And by that, I mean, the AWS Lambda function is operating fine and passing the web request down to your Django code. The problem that we're running to here is that the Django code is not accepting connections for this URL. And we'll need to change that. This is a built in Django security feature, preventing anyone from maliciously trying to access your site using a different host name.
[02:54 - 03:38] It's called host header validation. The fix for this is straightforward. All we have to do is include this Lambda domain link in our settings file. So let's do that now. We go to our Django settings and we scroll and find allowed hosts. Let's go ahead and add local host because we know we'll be doing debugging soon. And we're going to add this link. However, we can't add this link like a browser would want it. We actually have to remove the HTTPS portion of this. And we have to remove the slash dev portion of this. So we just get domain name .
[03:39 - 04:13] After that, we can save. And let's try again. Now that we've changed some Django settings and any Django code for that matter, we can no longer use the deploy command. And if we do use the deploy command, the Zappa utility automatically recognizes that the project is already deployed and tells you you cannot redeploy it. So now we can just use the Zappa update command.
[04:14 - 04:47] The update command tells a Zappa utility to upload the new code that you've changed into the cloud. The Zappa utility is going to zip up your entire project into a single zip file . Upload it to the S3 bucket we've configured. Tell AWS Lambda to load the zip file from the S3 bucket. And then remove the zip file from the S3 bucket. And that process completes the whole update.
[04:48 - 05:36] Okay, now that the update is completed, let's check our website and see what happens now. I'm going to hit refresh on the browser to load the page again. And look at that. We have our first cloud based Django project powered by AWS Lambda. That Zappa has enabled us to upload. So just consider for a bit what is actually happening here when I hit the refresh button, the browser sends HTTP requests to the API gateway that Zappa has created for you. The API gateway fires an event to the AWS Lambda functions. The AWS Lambda functions load your code and routes the request to your function .
[05:37 - 06:40] Your Django code then executes, renders the view and sends that response back through the chain that it came from, is rendered by the browser. Pretty cool, huh? I want to talk a little bit about this link that is given to us. This link is actually pretty ugly. The first part of the domain is randomly generated by Lambda for your project. The particular environment that you're accessing is appended to the end of the link as well. This is not what we call user friendly link. In later lessons, we're going to be able to take this link and make it much more user friendly. But for now, we're going to use it as is. One more thing before we move on to the next lesson is a way to get information about your Zappa deployment. We're going to use the Zappa status command to pull information about your deployed project so that we can analyze the health and status.
[06:41 - 07:15] So you'll see here the Zappa status command dumps a lot of information out to the command line for you to look at. Some things to highlight the number of invocations that has happened in the last 24 hours. The error rate, if any, the URL is right here in case you forget it. And the last time you pushed out updates to the code. Okay, this was a very exciting lesson because we did our first deployment. We're going to move on in the next lesson about how to design your Django project.