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

Serverless with Python

Serverless with Python

Talk at PyConES 2017

Alejandro Guirao Rodríguez

September 24, 2017

More Decks by Alejandro Guirao Rodríguez

Other Decks in Programming


  1. The pioneer: AWS Lambda S3 bucket DynamoDB table Kinesis Streams

    SNS Topic SES e-mail Cognito CloudFormation stack CloudWatch logs & events CodeCommit Config Alexa skill Lex API Gateway AWS Lambda Event sources
  2. AWS Lambda: Manual deploy def my_handler(event, context): message = 'Hello

    {} {}!'.format(event['first_name'], event['last_name']) return { 'message' : message }
  3. AWS Lambda: Manual deploy aws lambda create-function \ --region eu-west-1

    \ --function-name HelloPython \ --zip-file fileb://deployment-package.zip \ --role role-arn \ --handler hello_python.my_handler \ --runtime python3.6 \ --timeout 15 \ --memory-size 512
  4. AWS Lambda: Manual deploy $ aws lambda invoke \ --function-name

    <function-arn> \ --payload '{"first_name":"Margaret", "last_name":"Hamilton"}' out.json $ cat out.json {"message": "Hello Margaret Hamilton!"}
  5. Testing AWS Lambda • Decouple business logic • Test with

    sample events • Use lambci/lambda docker run -v $PWD:/var/task lambci/lambda:python3.6 my_module.my_handler
  6. AWS Lambda: chalice • Microframework for API Gateway + Lambda

    functions • Flask-like • A CLI that creates and configures the AWS resources needed (Lambda, API Gateway, IAM…) and makes and uploads the deployment package • Officially supported by AWS
  7. AWS Lambda: chalice $ pip install chalice $ chalice new-project

    helloworld && cd helloworld $ cat app.py from chalice import Chalice app = Chalice(app_name='helloworld') @app.route('/', methods=['POST']) def greeting(): body = app.current_request.json_body message = 'Hello {} {}!'.format(body['first_name'], body['last_name']) return { "message" : message } $ chalice deploy --dev
  8. AWS Lambda: zappa • Zappa is a tool for deploying

    your Python WSGI apps to AWS • Manages the lifecycle of the AWS resources and your application versioning (via S3) • Advanced features as remote invocations or scheduling configuration and async task execution $ zappa init $ zappa deploy
  9. Other hosted solutions • Azure Functions • IBM BlueMix Cloud

    Functions (based on OpenWhisk) • Iron.io (based on Docker containers) • Google cloud functions does NOT yet support Python
  10. DIY: Iron functions • Based on Docker containers: stdin and

    stdout as communication mechanism • Pulls images from a Docker registry (Dockerhub) • Configuration based on a func.yml file • Creation of a function: $ fn build && fn push $ fn apps create helloapp && fn routes create helloapp /hello
  11. DIY: Iron functions import os import sys import json if

    not os.isatty(sys.stdin.fileno()): obj = json.loads(sys.stdin.read()) first_name = obj.get("first_name") last_name = obj.get("last_name") message = 'Hello {} {}!'.format(first_name, last_name) print(json.dumps({"message": message}))
  12. DIY: Apache OpenWhisk Complex Architecture: • Event-based programming with triggers,

    rules and actions $ wsk action create hello hello.py --kind python:3 $ wsk action invoke hello --param first_name Margaret --param last_name Hamilton --result
  13. DIY: OpenFaas • Works on top of Docker Swarm or

    Kubernetes • Autoscales to meet the concurrency needed • Configuration via .yaml or UI provider: name: faas gateway: functions: hello: lang: Dockerfile handler: ./handler image: alexellis2/faas-hello-python3
  14. DIY: fission • Runs on Kubernetes (locally on Minikube) •

    100 msec cold-start latencies $ fission function create --name hello --env python3 --code hello.py $ fission route add --function hello --url /hello import json from flask import request def main(): data = json.loads(request.get_data().decode()) message = 'Hello {} {}!'.format(data['first_name'], data['last_name']) return json.dumps({"message": message}