Slide 1

Slide 1 text

Dane Hillard Web Application Developer, ITHAKA Twitter / easyaspython GitHub / daneah

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Zappa

Slide 4

Slide 4 text

Serverless Django with Zappa

Slide 5

Slide 5 text

Miserlou/Zappa

Slide 6

Slide 6 text

Miserlou/Zappa A tool for serverless AWS deployment of Python applications

Slide 7

Slide 7 text

Miserlou/Zappa A tool for serverless AWS deployment of Python applications

Slide 8

Slide 8 text

Overview ● Server ● Less ● AWS ● Deployment

Slide 9

Slide 9 text

Server

Slide 10

Slide 10 text

Server Request Response

Slide 11

Slide 11 text

Server nginx gunicorn Django assets cert Django Django database

Slide 12

Slide 12 text

load balancer

Slide 13

Slide 13 text

Less

Slide 14

Slide 14 text

nginx gunicorn Django Assets cert Request Response database

Slide 15

Slide 15 text

nginx gunicorn Django Assets cert Request Response database

Slide 16

Slide 16 text

nginx gunicorn Django Assets ACM Request Response database

Slide 17

Slide 17 text

nginx gunicorn Django Assets ACM Request Response database

Slide 18

Slide 18 text

nginx gunicorn Django Assets ACM Request Response database

Slide 19

Slide 19 text

nginx gunicorn Django S3 ACM Request Response database

Slide 20

Slide 20 text

nginx gunicorn Django S3 ACM Request Response database

Slide 21

Slide 21 text

nginx gunicorn Django S3 ACM Request Response database

Slide 22

Slide 22 text

API gateway Lambda Django S3 ACM Request Response database

Slide 23

Slide 23 text

API gateway Lambda Django S3 ACM Request Response database

Slide 24

Slide 24 text

API gateway Lambda Django S3 ACM Request Response database Lambda Lambda Django Django

Slide 25

Slide 25 text

AWS

Slide 26

Slide 26 text

AWS Certificate Manager bestpuppers.com *.bestpuppers.com Zappa arn:aws:acm:us-east-1:...:certificate/... Still available

Slide 27

Slide 27 text

Simple Storage Service (S3) Zappa S3 bucket best-puppers

Slide 28

Slide 28 text

Lambda WSGI Django WSGI Django WSGI Django

Slide 29

Slide 29 text

API Gateway stage production

Slide 30

Slide 30 text

AWS recap ● ACM for SSL certs ● S3 for static file storage ○ (CloudFront for CDN!) ● Lambda for request workers ● API Gateway for routing

Slide 31

Slide 31 text

Deployment

Slide 32

Slide 32 text

The setup zappa init and zappa_settings.json

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

What happens: ● Detect application type (Django, Flask, etc.) ● Create S3 bucket ● Create zappa_settings.json

Slide 35

Slide 35 text

zappa_settings.json { “staging”: { “aws_region”: “us-east-1”, “django_settings”: “bestpuppers.settings”, “profile_name”: “bestpuppers”, “project_name”: “bestpuppers”, “s3_bucket”: “best-puppers”, }, ... }

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Certificates { “staging”: { ..., “certificate_arn”: “arn:aws:acm:us-east-1:...:certificate/...” “domain”: “staging.bestpuppers.com” } } $ zappa certify staging

Slide 40

Slide 40 text

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!

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Tips

Slide 43

Slide 43 text

exclude unneeded files! coughnode_modulescough

Slide 44

Slide 44 text

exclude unneeded files ● node_modules ● .idea/ ● media/ ● Reduces ZIP file size and upload time

Slide 45

Slide 45 text

Automate your process! Use a task runner like Fabric or npm scripts

Slide 46

Slide 46 text

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” }

Slide 47

Slide 47 text

npm scripts $ npm run deploy --stage=production

Slide 48

Slide 48 text

Thanks! Find me in the hall for a demo

Slide 49

Slide 49 text

Dane Hillard Web Application Developer, ITHAKA Twitter / easyaspython GitHub / daneah