Back to the video

Serverless Django with Zappa by Dane Hillard

Transcript

By Dane Hillard, Speaker 2, Speaker 3, Speaker 4, Speaker 5, Speaker 6. From DjangoCon US 2018. Published November 8, 2018.

Watch the video Markdown version 3,478 words · auto-generated · en

Summary

Dane Hillard explains how Zappa deploys Django applications as serverless AWS services. The setup replaces self-managed web servers, application workers, certificates, and static-file storage with API Gateway, Lambda, Amazon Certificate Manager, and S3, while Lambda handles scaling and charges are based on execution time. He demonstrates the Zappa configuration and deployment workflow, then covers logging, management commands, scheduled tasks, packaging optimisations, automation, database choices, cost, and cold-start latency.

Transcript

Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.

0:00 Introduction to Zappa

Dane Hillard

Yeah, so thank you. As she said, my name's Dane and I am a web developer at Ithaca. You may know us from JSTOR. org. It's an academic research platform. And on Twitter, I'm Easy as Python. And GitHub, I'm Dane. So feel free to connect. And I am here today to talk to you about Zappa. Well not this Zappa, although he warrants a talk All on his own, many talks perhaps but Zappa nonetheless. And specifically how we can do serverless Django with Zappa. And specifically, I mean this Zappa. It's a framework written by a guy named Rich Jones.

1:03 Traditional Django Servers

Dane Hillard

And what it really does is let you do serverless AWS deployments for Python applications. And so during this talk, I'm going to kind of emphasize these pieces here in bold colors. So what we're gonna do is talk about each of those in a little bit of detail. So we'll talk about servers, what a server really is, and what the less part of serverless really means. And then some specifics about Amazon Web Services and how you go about actually deploying an application there. So first, let's talk a little bit about servers. They are , at their core, a thing that a user can make a request to

and receive a response from as a result. And so many of you being Django developers are probably intimately familiar with this idea. But what really happens and how do how do we achieve that workflow along the way? So you have a server and when a request comes in, it typically gets handled at first by a web server which knows how to speak HTTP. And then that passes it along to something like GUnicorn, which is a way to translate an HTTP request into something that Python can understand. and ultimately it will end up at your Django application.

And there's there's some additional things that you'll probably be using if you're doing any kind of dynamic application. You'll probably need to talk to a database. You'll probably need static assets to style your pages. And you'll probably want a certificate of some kind if you want to be working over HTTPS. And as you get more traffic to your application, you want to be able to handle more concurrent requests. So you'll start making more processes for your Django application, and each of those can handle a request at a time. And then when your server inevitably can't handle the load and falls over, you decide maybe you'll make several of those servers and put them behind a load balancer.

3:28 The Meaning of Serverless

Dane Hillard

So you start it you start scaling horizontally to handle all this traffic, and that's fine but if you're like me and don't really have time to do a lot of automation of your DevOps and things like that, managing all of these servers can get kind of tedious and kind of frustrating. So how do we do how do we do less? Like Ron Swanson. There has to be a better way and we want to free ourselves of the earthly burden of all this machinery. What less really means to me in serverless, which is sort of a marketing buzzword is that you do less management and you do less

sort of fiddling with all of these moving parts and try to find a way to more easily manage all of these things in an automated fashion and abstract some of those things so that you don't have to see the hairy details every day. So this again is what a server might look like and We want to find a way to basically take these different pieces that are on the server and get them off of the server so that we don't have to Deal with them anymore or can deal with them in a better way. So let's start with cert. How many of you are familiar with Let's Encrypt? Great, that's like half of you or

5:02 AWS Infrastructure Services

Dane Hillard

so. It essentially lets you much more easily set up a cert and serve your website over HTTPS to the world. And Amazon also has an answer to this called Amazon Certificate Manager. So for the sake of argument, let's offload that to the Amazon certificate manager. This way we can just easily create a certificate. in the in the console and apply it to the resources that we want that certificate to be on. So that's that's sort of locked in. And now that is off of our server. And then the next thing is those static assets.

It could be the media that your users are uploading as well. So right now you might store those on your server at least temporarily when they are uploaded or when you do a collect static during your deployment. But we can store those in Amazon as well. It has a thing called simple storage service. And you can refer to those different files by their By their object basically in S3. So let's lock that in. And the last the last remaining pieces are really the parts that are important to the request response lifecycle that leads from HTTP request down to a Python translation down to Django.

6:34 Lambda and API Gateway

Dane Hillard

So how do we get rid of that stuff? That seems kind of tricky. So one way we could imagine doing this is with some other Amazon tools. So we're taking away the job of routing from Nginx and using instead Amazon's API gateway and we're taking the job of translating requests from HTTP to Python away and using Lambda to do that. So how many of you have heard of Lambda? Okay, that's like most of you. So that's great. Lambda is Amazon's functions as a service, essentially. And

what they do is let you respond to a request by executing some function And it would be great because it would be great if we could use Django as the underlying function for this. And effectively get rid of our server. So as you can see, sort of from this last transition, we've kind of freed ourselves of the need for this server wrapping all of our code. So how do we get there? And how more importantly, how do we respond to more activity in our application? So let's let's talk a little bit about what the details are under the hood in AWS.

8:08 Serverless Django Architecture

Dane Hillard

So imagine we are creating a website where we can post pictures of puppies, all the best puppers. What we need for our for our certificate is to create a certificate in Amazon Certificate Manager. And then what that lets us do is reference that certificate with an Amazon resource number or an ARN. So that long string EC is the identifier basically for that cert. By the way, bestbubbers. com still available, so you should buy that. And then for our static assets we need a bucket to store those in S3.

And so we're gonna tell we're gonna tell Zappa how to where to put its stuff by referencing that bucket name as well. And the next piece, which is sort of the crux of it, is that we need to put our Django application into Lambda. And there's sort of this wrapper around it that does that WISGI translation for us. And then Lambda can handle all of that horizontal scaling that we talked about earlier. So once we once we're able to do that, we're sort of home free. And with the API gateway, we can manage our different environments. And route requests for different domains

to different sets of applications. So to recap, Amazon Certificate Manager for SSL certs and S3 for static file storage, and if you're feeling really ambitious. You can put CloudFront in front of it to get a globally distributed set of static files to speed up delivery to users And then Lambda for our request workers and the API gateway to route requests to different applications. So the next step is deployment. Hopefully you can do it the first time. So the way

10:26 Zappa Configuration and Deployment

Dane Hillard

the way that we do this with Zappa is basically to initialize our configuration and set up the details in our Zappa settings. So how do we do this? In order to get your application set up with Zappa, like here, I've just basically started with Django Start Project Best Puppers. So you have your single application module package rather with your settings and your URL patterns and your WISGI. py as well as your manage. py. You'll also need your AWS credentials listed.

and the way I prefer to do this is to have a different profile for each of my projects, even if they end up using similar set of credentials. So once you have those two things in place, you can just pip install Zappa and run Zappa init. And what that's going to do is detect your application type. So it's not actually just Django that is supported, it's really any WISGI application and even some non-WSGI applications, if I if I remember correctly. And then it will create that S3 bucket so that it can upload artifacts for your application. And then it will create this zappa settings. json file. And at first that'll look probably something like this.

So you have a staging environment, maybe a production environment. And each one will have the AWS region that that is deployed to, and then it will automatically figure out your Django settings module And the profile name is referring to that AWS credentials profile. And then the project name is your Django project, and the S3 bucket is that bucket name that we talked about earlier. So then now that you've got all this configuration in place, you're sort of ready to do your first deployment. It's it's pretty much that easy. So you run Zappa deploy staging, and staging is the name

of that environment from your sat from your Zappa settings JSON. And what that'll do is well I won't I won't make you read all of this code here or this output here, but What happens is that it will freeze your Python requirements along with all of your application code into a single zip file. And then it'll upload that zip file to S3 into that bucket. And then it will create API gateway routes for you. And it will create Lambda functions that ultimately point to the Django view code that you need to execute. in response to requests and then hook the API gateway to those Lambda functions. And importantly it prints out the URL you can visit your application at the end.

It's some long randomly generated string of characters, followed by this execute API US East One or whichever region you're in, Amazon AWS. com and then slash your environment. And updating your application is quite similar. Again it's it's Downloading all of your dependencies, packaging them up as a zip, uploading that to S3 and then since you're updating your application, it'll update the Lambda and redeploy the API gateway. And then give you the URL to visit that again. So the next step after this is getting your certs in order.

14:19 Custom Domains and Operations

Dane Hillard

And fortunately that is very easy too. So once you've created a certificate, an Amazon certificate manager, you can take that ARN, that AWS resource manager or resource number string and specify it in your environment as the certificate ARN and then tell it the domain that you want to certify. And then you just run Zappa certify stagey. And that will certify the stag that you specified using the cert that you specified And then through this you also get log storage and tailing. So you can See what's happening in your application in real time as well as persist those logs for later use.

You also get management commands and even the ability to run raw Python against your Lambda. Against your environment where the lambda lives. And as you should know from Lambda, you get auto-scaling to meet your request load. Some points there though are to watch out for concurrency and cold start time. You can read more about that. There's a good article I could point you to later. But essentially just if you're if you're trying to serve very fast requests you may want to look into that first And importantly, you'll probably have a way lower AWS bill because you don't have servers running when they're not serving any requests.

15:54 Databases and Background Tasks

Dane Hillard

So a request comes in, it is managed by that Lambda function, and as soon as that response is sent, that Lambda goes away. And you only pay for the execution time of that request lifecycle. And a couple of odds and ends to our like serverless database options. Amazon did release Aurora Serverless recently. That's their MySQL compatible product. And it's so it's a managed solution. But the cost is really comparable to any of their other database solutions at the moment. So there wouldn't necessarily be a price gain there, although you might still like the managed solution facet of it. There's also DynamoDB, but

that's a NoSQL solution, and I don't know, at least at the time of writing this talk, of any of any production ready drivers for Django for using Dynamo DB. There are a couple of other options out there that are not necessarily Django specific, that are also not relational databases, but might still persist data for you in a way that's that's useful to you. And then you can also run asynchronous tasks. So Zappa can actually run functions from your code on a schedule if you like. You can specify things in like a cron syntax and say every four hours please run this thing. If you wanted to do celery workers and things like that.

you'd you'd have to look into that yourself because that's not a domain I'm familiar with. But you can also split your application into separate lambdas if you like, if you really need. Sort of separate asynchronous behavior. And long-running lambdas aren't super well supported, although I did see that AWS recently upped the execution limit to 15 minutes. I think it was previously five. So you've got triple the time to do what you need to do. So some tips are that you would Probably want to exclude most of your unneeded files. Node modules is huge. I mean it's it's I don't know.

18:12 Deployment Optimization

Dane Hillard

You d you don't want to ship more than you have to. So like node modules, if you use PyCharm, your dot idea folder. And if you are uploading media files locally, you probably want to exclude that directory too. And it just reduces the zip file size so it can zip it faster and ship it faster. When you go to deploy. And then the other major tip I have is that you should automate your process. You I there's there's a number of options out there. A lot of people use fabric. I personally use npm scripts because I'm already using Webpack for my static assets so I have a few scripts. already. So my package JSON

looks something like this. That's a lot of stuff. But if you if you want to know more about this, I'm happy to like show you this file and talk about all the parts of it in the hall. And what it essentially allows me to do is deploy my Django application by just running npm run deploy dash stage equals production So that's basically all I have. And if you'd like to, like I said, see more of this or talk about anything specific or see a live demo. I'm happy to do that. And here's my contact info again. So thanks.

19:43 Questions

Speaker 2

Hi, thanks for the presentation. What are some of the cost considerations for performance? So how would you compare you pay for every invocation. of the Lambda function that's a small price individually, how would that compare to having your own EC2 instance?

Dane Hillard

Yeah, good question. So I was previously running four applications on an EC2 instance and spending something around fifty dollars a month. I don't remember the EC2 instance size that I was using. But I'm essentially paying zero dollars per month now. And Amazon gives you something like three and a half million invocations free per month. So if you're If you're at sort of a hobby level amount of traffic, I would say there's almost no likelihood that it's going to be more expensive. There's certainly gonna be a break-even point there if you're If you're getting so many requests that your Lambda is essentially running all the time, I could definitely see that that might

Perhaps even outrun your EC2 cost. Definitely a consideration. Yeah. Good question.

Speaker 3

Hi. Did does Zappa also help you when you said it goes to the assets goes to S three, does that do it automatically when you collect asset, does it go to S three or do you have to

Dane Hillard

Yeah, good question. So Zapple will not do that for you. There are a couple of really good solutions out there for putting your static assets into S3. There's a library called Django Storages. And it just takes a few small pieces of configuration, like adding some settings to your settings. py. And maybe creating the buckets yourself initially. But you can essentially collect your static files and upload your media files directly to S3 using Django storages. So that's how I'm doing it currently

Speaker 4

You mentioned some serverless database options. How does that compare to like a standard RDS instance?

Dane Hillard

Can you clarify your question?

Speaker 4

you mentioned that Aurora and Dynamo DB and under serverless databases in the talk. I just want to know if that's any if there's anything special about that for using with Lambda as compared to like an RDS Postgres instance.

Dane Hillard

I see. I mean Aurora again is like MySQL compatible, so if you're already using MySQL, you can probably switch to Aurora pretty easily. You have to migrate your data and stuff obviously, but I think the serverless aspect of their Aurora product is really just them saying we have a managed solution for this instead of you having to go and click and create an RDS instance yourself and run backups and make sure that It's handling the load that you're getting. So it also manages that horizontal scaling. So if you're if you're having a lot of database reads, it'll scale out for you. But in terms of sort of how you interact with the database,

for Aurora at least I don't imagine there's a difference. If you went to DynamoDB or some other non-relational database, that would be a more significant shift. Does that answer your question? All right, cool.

Speaker 4

Thanks for your talk.

Speaker 5

Can you tell us about the latency per request and are you aware of any factors that change that latency depending on complexity of your slug or output size?

Dane Hillard

Yeah, it's not something I've delved super far into, to be honest, but anecdotally on a totally cold start, I have websites again receiving like hobby levels of traffic. But They take maybe a couple seconds to load if I haven't hit them in a while. And then subsequent requests are essentially instantaneous as far as I can I can find. feel. I could show you just real quick like maybe I can't. Here we go. Sorry, multiple desktops. Well, I can show I'm I'm happy to show you in the hallway, but yeah, it's quick loading.

Speaker 6

All right, so we're about out of time. Thank you so much, Dane.