Slide 1

Slide 1 text

Deploying your python backend with (almost) zero cost

Slide 2

Slide 2 text

Srinivasan Rangarajan Systems Architect

Slide 3

Slide 3 text

Physical Servers • Takes few days to provision • Heavily under-utilized • Priced per month • Administrative nightmare

Slide 4

Slide 4 text

Cloud Servers • Few minutes to provision • Create new servers using APIs • Can be turned off when not in use • Priced per hour

Slide 5

Slide 5 text

Lambda Functions • Milliseconds to provision • 100% utilization • Priced every 100ms • No servers to manage • Continuous & automatic scaling

Slide 6

Slide 6 text

How to do it? 1. Create your lambda function. 2. Specify the resource which triggers the function. 3. ??? 4. Profit.

Slide 7

Slide 7 text

Triggers • File gets uploaded into S3 • Receive new message in SQS • Inserts into DynamoDB • Plain old HTTP request

Slide 8

Slide 8 text

hello_world.py def my_handler(event, context): message = 'Hello {} {}!'.format(event['first_name'], event['last_name']) return { 'message' : message }

Slide 9

Slide 9 text

event • Usually a dict • list • str • int • float • Nonetype

Slide 10

Slide 10 text

context • LambdaContext type • how much time is remaining for termination • function_name • function_version • memory_limit_in_mb

Slide 11

Slide 11 text

Deployment Package • Create a project-dir folder • Write your .py files in that folder • pip install all dependencies 
 pip install module_name --target /path/to/project-dir • Zip the project-dir

Slide 12

Slide 12 text

Chalice

Slide 13

Slide 13 text

$ pip install chalice $ chalice new-project helloworld $ cd helloworld

Slide 14

Slide 14 text

app.py from chalice import Chalice app = Chalice(app_name="helloworld") @app.route("/") def index(): return {"hello": “world"} @app.route("/city/{city}") def city(city): return {"city": city}

Slide 15

Slide 15 text

Deploy $ chalice deploy Creating deployment package. Lambda deploy done. API Gateway rest API already found. Deleting root resource id Done deleting existing resources. Deploying to: dev https://68mxz0v402.execute-api.us-east-1.amazonaws.com/dev/

Slide 16

Slide 16 text

Browser/curl 1. https://68mxz0v402.execute-api.us- east-1.amazonaws.com/dev/ • http://bit.do/lambda_index 2. https://68mxz0v402.execute-api.us- east-1.amazonaws.com/dev/city/delhi • http://bit.do/lambda_city

Slide 17

Slide 17 text

request object • app.current_request.query_params • app.current_request.headers • app.current_request.uri_params • app.current_request.method • app.current_request.json_body • app.current_request.raw_body • app.current_request.context • app.current_request.stage_vars

Slide 18

Slide 18 text

Examples

Slide 19

Slide 19 text

Update Product Metadata r = redis.Redis() @app.route('/product/{pid}/update', methods=['POST']) def product_update(pid): """Update price, availability info about a product in redis""" body = app.current_request.json_body client_id = body['client_id'] prod_key = '{}:{}'.format(client_id, pid) price = body.get('price') if price: r.hset(prod_key, 'price', price) availability = body.get('availability') if availability: r.hset(prod_key, 'availability', availability)

Slide 20

Slide 20 text

Thumbnail Creation @app.route('/', methods=['POST']) def index(): body = app.current_request.json_body image = base64.b64decode(body['data']) format = {'jpg': 'jpeg', 'png': 'png'}[body.get('format', 'jpg').lower()] mode = {'max': '', 'min': '^', 'exact': '!'}[body.get('mode', 'max').lower()] width = int(body.get('width', 128)) height = int(body.get('height', 128)) https://github.com/Schweigi/thumbnail-service

Slide 21

Slide 21 text

Thumbnail Creation cmd = [ 'convert', # ImageMagick Convert '-', # Read original picture from StdIn '-auto-orient', # Detect picture orientation from metadata '-thumbnail', '{}x{}{}'.format(width, height, mode), # Thumbnail size '-extent', '{}x{}'.format(width, height), # Fill if original picture i smaller than thumbnail '-gravity', 'Center', # Extend (fill) from the thumbnail middle '-unsharp',' 0x.5', # Un-sharpen slightly to improve small thumbnails '-quality', '80%', # Thumbnail JPG quality '{}:-'.format(format), # Write thumbnail with `format` to StdOut ] p = Popen(cmd, stdout=PIPE, stdin=PIPE) thumbnail = p.communicate(input=image)[0] https://github.com/Schweigi/thumbnail-service

Slide 22

Slide 22 text

Thumbnail Creation if not thumbnail: raise BadRequestError('Image format not supported') filename = '{}_{}x{}.{}'.format(uuid.uuid4(), width, height, format) S3.put_object( Bucket=S3_BUCKET, Key=filename, Body=thumbnail, ACL='public-read', ContentType='image/{}'.format(format), ) return { 'url': 'https://s3.amazonaws.com/{}/{}'.format(S3_BUCKET, filename) } https://github.com/Schweigi/thumbnail-service

Slide 23

Slide 23 text

Other Use Cases

Slide 24

Slide 24 text

Data Pipeline Log file parsing and data cleansing

Slide 25

Slide 25 text

Static Blog commenting system HTTP Gateway + DynamoDB

Slide 26

Slide 26 text

Genome Editing & Sub-second Searches http://benchling.engineering/crispr-aws-lambda/

Slide 27

Slide 27 text

Continuous Integration

Slide 28

Slide 28 text

lambci • Node.js (multiple versions via nave) • Python 2.7 • Java (OpenJDK 1.8 and 1.7) • Go (any version) • Ruby (2.3.1, 2.2.5, 2.1.10, 2.0.0-p648) • PHP (7.0.10, 5.6.25) • Native compilation with a pre-built gcc 4.8.5 • Rust (1.11.0, 1.10.0, but any version should work) https://github.com/lambci/lambci

Slide 29

Slide 29 text

Chat Bots

Slide 30

Slide 30 text

Stateless A: I love Stateless Systems B: Don’t they have drawbacks? A: Don’t what have drawbacks?

Slide 31

Slide 31 text

Limitations • Only 300 seconds of execution time. • Max 1.5GB RAM. • Max 512 MB /tmp disk space. • Max 6MB Request/Response payload • Only python 2.7

Slide 32

Slide 32 text

Advantages • Automatically scalable • Built-in Fault Tolerance • Automatic Administration • Pay per use

Slide 33

Slide 33 text

Pricing • $0.000000208 per 100ms for 128MB RAM • $0.000002501 per 100ms for 1.5GB RAM • $0.0000002 per request • Free tier level • 400,000 GB-seconds per month • 1Million requests

Slide 34

Slide 34 text

Links • https://aws.amazon.com/lambda/ • http://chalice.readthedocs.io/ • https://github.com/Schweigi/thumbnail-service • https://github.com/lambci/lambci • http://benchling.engineering/crispr-aws-lambda/

Slide 35

Slide 35 text

• https://github.com/cnu • https://twitter.com/cnu • [email protected]