name: orange layout: true --- class: title, center, middle # Going Serverless with Flask ## Experiences with AWS Lambda and OpenWhisk .authorblock[ .author[Alex Miłowski, PhD] .affiliation[.orange[Orange] Silicon Valley] ] --- .fullimage[] .footer.note[Image source: tumblr.com https://68.media.tumblr.com/tumblr_m7botv5oBn1qafsv7o1_500.gif] --- class: abstract # Abstract Deploying applications via serverless infrastructure may require some changes in architecture and assumptions. This talk will walk through experiences deploying Flask-based services on AWS Lambda and OpenWhisk with minimal changes, demo supporting libraries, enumerate some of the challenges, and address practical outcomes derived from the promises of serverless deployment. --- class: normal # Enterprise data devops Need to create an environment where: * microservices provide access to resultants * from big data / AI analytics * to enterprise applications * or consumer services --- class: normal # Scale Out Infrastructure .figure[  ] .compact[ Can help address: * issues of peak load * simplifying deployment (tradeoffs?) * kubernetes/openwhisk enables separation of concerns * Apache OpenWhisk - http://openwhisk.incubator.apache.org ] --- # Experiment: Flask on FaaS The experiment: 1. Take a reasonably complicated Flask application. 2. Deploy on AWS Lambda or OpenWhisk 3. Compare! What needs to change? Does that matter? What is the comparable performance? Is the tradeoff (if any) worth the scale out? --- # Zappa for AWS Lambda "Serverless Python Web Services - Powered by AWS Lambda and API Gateway" https://www.zappa.io or https://github.com/Miserlou/Zappa As easy as: ```bash $ pip install zappa $ zappa init $ zappa deploy ``` Well, not quite … but almost! --- # 𝚫 - 1/2 - `blog.py` Replicating the main and packaging configuration: ```python class Config(object): DEBUG=True SERVER_NAME='test.milowski.com:8080' DOCS='http://...' # location of static documents ... from flask import Flask app = Flask(__name__) from duckpond.apps.blog.assets import assets from duckpond.apps.blog.docs import docs from duckpond.apps.blog.views import views app.register_blueprint(assets) app.register_blueprint(docs) app.register_blueprint(views) ``` .footer.note[duckpond project @ https://github.com/alexmilowski/duckpond] --- # 𝚫 - 2/2 - `production.py` Configuration from objects: ```python from blog import Config,app class ProductionConfig(Config): DEBUG=False SERVER_NAME='app.milowski.com:8080' app.config.from_object('production.ProductionConfig') if __name__ == '__main__': app.run() ``` .footer.note[You can run this directly for local testing or use via FaaS.] --- class: separator center middle # Demo Time ## Zappa for AWS --- # Flask on OpenWhisk? * Zappa doesn't support OpenWhisk … (possible) * What is needed: * a library that bridges the FaaS layer to the WSGI * packaging specific to the FaaS environment * deployment orchestration * How hard is that? 😬 --- # So I wrote my own … https://github.com/alexmilowski/flask-openwhisk A bridge: OpenWhisk ↔︎ WSGI request / response Seemless but you need a `__main__.py`: ```python from flaskwsk.handle import invoke from web import app def main(args): return invoke(app,args) ``` Only addresses the need for a library! --- # Packaging for OpenWhisk * functions run in Docker containers * "warmed" by initializing with the code * zipped into an archive with dependencies via `virtualenv` * python container expects: ``` __main__.py ... virtualenv/... ``` --- class: separator center middle # Demo Time ## flaskwsk for OpenWhisk / IBM bluemix --- # Results .split[
.compact[ * *Lambda* vs *nginx / gunicorn* via **AWS**: * ~ 1.6x slower * larger variance * shared db / resources * random high values * *OpenWhisk* via **IBM Bluemix**: * high values for cold containers * performs well with remote database * *OpenWhisk* is open source so … we can contribute! ] ] .footer.note.small[Looks scrambled? Reload! Life is too short to wonder why C3/D3/SVG does this kind of thing. C'est la vie!] --- class: takeaway # Takeaway .split[ .column[ Key conclusions: .compact[ * increased latency * tradeoff for scale out * no changes possible ] ] .column[ Followup research: .compact[ * scale out interactions via kubernetes * container warming * WSGI optimization ] ] ] .thanks[Thank you!] .authorblock[ .author[Alex Miłowski, PhD] .email[alex@milowski.com] .email[alex.milowski@orange.com] ] .authorblock[@alexmilowski ]