DjangoCon Europe 2019: Serverless Django with Zappa

Writeup of the DjangoCon Europe 2019 talk »Serverless Django with Zappa« by Neal Todd

Neal Todd: A senior developer at Torchbox, home of the open source Wagtail content management system. He's been designing and building Django applications for charities, NGOs and not-for-profits for 10 years.

We're talking about servers without permanent infrastructure – calling "serverless" "functions as a service" would be more accurate. We hope that their cost is relative to their execution time by not paying for idle time. This also allows us to scale without intervention (again, hopefully).

We'll be using at Zappa today, which is a Python package for running server-less Python web applications on AWS Lambda. It takes incoming requests, turns them into WSGI requests and passes them on.

The infrastructure of Zappa is tailored to Amazon Web Services, implying Lambda functions, API Gateway, S3 for strage, IAM (Identity and Access Management), RDS for databases, and additional helpers like VPC, Route53, Certificate Manager.

There is the AWS free tier which can get you started. For further development/usage, you'll have to evaluate costs yourself.

You'll start with zappa init, and then configure the project with a JSON or YAML file in your project directory.

Look at the live demo code at [https://nealtodd.github.io/serverless-django/djconeu-speedrun], including a screencast. It uses the Wagtail Bakery Demo project as a Django application, S3 for both static assets and the SQLite database. You'll have to use zappo-django-utils for the SQLite S3 bucket and django-storages for the staticfiles storage.

Zappa provides further command for deployment, namely deploy dev, certify dev -y, and various commands like zappa manage dev migrate to pass commands to django. You can then look at zappa status dev for status reports and use zappa update dev.

Restricting access with roles and policies isn't trivial, so people tend to use AdministratorAccess for everything.

Using S3 for the SQLite database works great at first, but naturally isn't great at high-write concurrency. RDS database services are free the first year, and then is about USD 14 per month for the smallest instance. If you want to use RDS, you'll want to install dj-database-url to insert the database URL in your DATABASES list.

Aurora Serverless might sound like a good idea, but it is more suited for infrequent, intermittent, or unpredictable workloads, and is not a good fit for production Django applications, because cold starts will take you about 30 seconds for the first request. AWS Lambda must be in VPC, wich again costs money.

In terms of performance, SQLite performs alright, and Postgres and Aurora perform about the same at .7 the response time of SQLite.

Misc notes: You can deploy to all regions instead of to a specific version, reducing latency. You can use scheduled functions via Zappa, and you can use them to keep your serverless instance warm. Be careful not to put anything in your local venv that's not supposed to be there, because it will end up on your Lambda service package.

Costs beyond USD 0 Lambda can rack up, but it's hard to discover what all the costs are.

References - Zappa, Github - AWS Free Tier - Speedrun screencast - Slide deck