Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serverless Django with Zappa—DjangoConUS 2018

Dane Hillard
October 16, 2018

Serverless Django with Zappa—DjangoConUS 2018

A common set of considerations when planning to release a new application are around hosting and resources:

* Which platforms will support my app?
* What OS should I use?
* What WSGI server should I use?
* How much memory and CPU do I need?

Zappa, a framework for running serverless WSGI applications on top of AWS Lambda, can help address these and other questions around application deployment. With some configuration (as code), Zappa can get you up and running quickly, letting you focus on developing the application instead of the infrastructure.

In this talk we’ll go through some of these kinds of questions to see how they might be answered in traditional solutions and then see how Zappa handles them. We’ll also talk about a few specific configuration options for handling SSL certificates and running Django commands on the Zappa-deployed application.

Dane Hillard

October 16, 2018
Tweet

More Decks by Dane Hillard

Other Decks in Technology

Transcript

  1. AWS

  2. AWS recap • ACM for SSL certs • S3 for

    static file storage ◦ (CloudFront for CDN!) • Lambda for request workers • API Gateway for routing
  3. zappa init $ tree . ├── bestpuppers │ ├── __init__.py

    │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py 1 directory, 5 files $ cat ~/.aws/credentials [bestpuppers] aws_access_key_id = ... aws_secret_access_key = ... $ pip install zappa $ zappa init
  4. What happens: • Detect application type (Django, Flask, etc.) •

    Create S3 bucket • Create zappa_settings.json
  5. First deployment $ zappa deploy staging Calling deploy for stage

    staging… Downloading and installing dependencies… Packaging project as zip… Uploading zip (4.3MiB)... Creating API Gateway routes… Deploying API Gateway… Your Zappa deployment is live! https://...execute-api.us-east-1.amazonaws.com/staging
  6. What happens: • Freeze Python requirements along with application code

    into a ZIP file • Upload ZIP file to S3 bucket • Create API Gateway routes • Create Lambda function with code from ZIP file • Hook API Gateway route to Lambda function • Print URL to visit application
  7. Updating the application $ zappa update staging Calling update for

    stage staging… Downloading and installing dependencies… Packaging project as zip… Uploading zip (4.3MiB)... Updating lambda function code... Updating lambda function configuration... Deploying API Gateway... Your updated Zappa deployment is live! https://...execute-api.us-east-1.amazonaws.com/staging
  8. You also get: • Log storage and tailing • Management

    commands and raw Python in Lambda • Auto-scaling to meet request load (watch out for concurrency and cold start time) • Probably a way lower AWS bill!
  9. Odds and ends • Serverless database options ◦ Aurora serverless

    is a thing; managed solution, but comparable in cost to RDS for now ◦ DynamoDB is a thing; NoSQL solution with no production-ready driver for Django that I know of • Asynchronous tasks ◦ Zappa can run functions on a schedule! ◦ Could split into separate application/Lambda and respond to triggers ◦ Long-running lambdas aren’t well-supported
  10. package.json ..., “scripts”: { “clean”: “rm -rf assets/dist”, “pretest”: “coverage

    erase”, “test”: “coverage run manage.py test”, “posttest”: “coverage report”, “prebuild”: “npm run clean”, “build”: “webpack”, “build:prod”: “NODE_ENV=production npm run build -- -p --progress”, “prestart”: “npm run clean”, “start”: “webpack --watch”, “predeploy”: “npm run build:prod && npm run test”, “deploy:migrate”: “zappa manage $npm_config_stage migrate”, “deploy:static”: “zappa manage $npm_config_stage ‘collectstatic --noinput’”, “deploy”: “zappa update $npm_config_stage”, “postdeploy”: npm run deploy:migrate && npm run deploy:static” }