Adventures in Apache OpenWhisk Rob Allen
January 2019
Slides: https://akrabat.com/5507
Slide 2
Apache OpenWhisk
Rob Allen ~ @akrabat
Slide 3
Apache OpenWhisk • • • •
OpenSource http://openwhisk.org Incubator project working towards graduation Many contributors from multiple companies
Rob Allen ~ @akrabat
Slide 4
Apache OpenWhisk • Self-hosted. Deploy to: - Kubernetes - Mesos - OpenShift - Dev: Vagrant & Docker Compose • Multiple public providers: - IBM - RedHat - Adobe (for Adobe Cloud Platform APIs) Rob Allen ~ @akrabat
Slide 5
Supported languages • • • • •
.NET Core Go NodeJS Python Swift
• • • •
Ballerina Java PHP Ruby
Also, your own Docker container can be deployed & invoked
Rob Allen ~ @akrabat
Getting started with OpenWhisk
Rob Allen ~ @akrabat
Slide 9
Hello World def main(args): name = args.get(“name”, “World”) message = ‘Hello {}!’.format(name) return {‘body’: message}
Rob Allen ~ @akrabat
Slide 10
Hello World def main(args): name = args.get(“name”, “World”) message = ‘Hello {}!’.format(name) return {‘body’: message}
Rob Allen ~ @akrabat
Slide 11
Hello World def main(args): name = args.get(“name”, “World”) message = ‘Hello {}!’.format(name) return {‘body’: message}
Rob Allen ~ @akrabat
Slide 12
Hello World def main(args): name = args.get(“name”, “World”) message = ‘Hello {}!’.format(name) return {‘body’: message}
Rob Allen ~ @akrabat
Slide 13
Hello World def main(args): name = args.get(“name”, “World”) message = ‘Hello {}!’.format(name) return {‘body’: message}
Rob Allen ~ @akrabat
Slide 14
Deploy to OpenWhisk $ zip -q hello.zip hello.py
Rob Allen ~ @akrabat
Slide 15
Deploy to OpenWhisk $ zip -q hello.zip hello.py $ wsk action update —kind python:3.7 hello hello.zip ok: updated action hello
Rob Allen ~ @akrabat
Slide 16
Run it $ wsk action invoke hello —result —param name Rob
Rob Allen ~ @akrabat
Slide 17
Deploying your action $ wsk action invoke hello —result —param name Rob { “body”: “Hello Rob!” }
Rob Allen ~ @akrabat
Slide 18
Segue: How did it do this?!
Rob Allen ~ @akrabat
Slide 19
OpenWhisk’s architecture
Rob Allen ~ @akrabat
Slide 20
Create an action
Rob Allen ~ @akrabat
Slide 21
Invoke an action
Rob Allen ~ @akrabat
Slide 22
Action container lifecycle • Hosts the user-written code • Controlled via two end points: /init & /run
Rob Allen ~ @akrabat
Slide 23
Action container lifecycle • Hosts the user-written code • Controlled via two end points: /init & /run
Rob Allen ~ @akrabat
Slide 24
End Segue
Rob Allen ~ @akrabat
Slide 25
Web-enabled actions Access your action via HTTP: Perfect for web hooks Add the —web flag: $ wsk action update —kind python:3.7 —web true \ demo/hello hello.zip
Rob Allen ~ @akrabat
Slide 26
Web-enabled actions Get URL and curl it: $ wsk action get —url demo/hello ok: got action hello https://192.168.1.17/api/v1/web/guest/demo/hello $ curl https://192…17/api/v1/web/guest/demo/hello?name=Rob { “body”: “Hello World!” }
Rob Allen ~ @akrabat
Slide 27
API Gateway When you want to do more with HTTP endpoints • • • • •
Route endpoint methods to actions (Open API Spec support) Custom domains Rate limiting Security (API keys, OAuth, CORS) Analytics
Rob Allen ~ @akrabat
Slide 28
API Gateway
Rob Allen ~ @akrabat
Slide 29
API Gateway
$ wsk api create /myapp /hello GET hello
Rob Allen ~ @akrabat
Slide 30
API Gateway
$ wsk api create /myapp /hello GET hello ok: created API /myapp/hello GET for action /_/hello
Rob Allen ~ @akrabat
Slide 31
API Gateway
$ curl https://example.com/myapp/hello?name=Rob
Rob Allen ~ @akrabat
Slide 32
API Gateway
$ curl https://example.com/myapp/hello?name=Rob { “message”: “Hello Rob!” }
Rob Allen ~ @akrabat
Slide 33
Packages • Group actions together • Set parameters used by all actions $ wsk package update demo $ wsk action update —kind python:3.7 demo/hello hello.zip
Rob Allen ~ @akrabat
Slide 34
Built-in packages • • • • • •
alarms github kafka pushnotifications utils weather
• • • • • •
cloudant jira pushnotifications slack watson websocket
• & any other publicly shared package
Rob Allen ~ @akrabat
Slide 35
Sequences Invoke a set of actions in turn
Rob Allen ~ @akrabat
Slide 36
Composer: Logic for your actions
Rob Allen ~ @akrabat
Slide 37
Composer: Logic for your actions
Rob Allen ~ @akrabat
Slide 38
Composer: It’s just code composer.sequence( composer.if( ‘binday/authenticate’, composer.sequence( ‘format_input_from_alexa’, composer.if( ‘binday/validate’, ‘binday/get_next_bin_day’ ‘binday/send_failure’ ), ), ‘binday/send_failure’ ), ‘binday/format_output_for_alexa’ )
Rob Allen ~ @akrabat
Slide 39
Deployment: Serverless Framework Set up: $ $ $ $
npm install —global serverless serverless-openwhisk serverless create —template openwhisk-php —path app cd app npm install
Rob Allen ~ @akrabat
Slide 40
serverless.yml service: ow-todo-backend provider: name: openwhisk runtime: php plugins: - serverless-openwhisk
Rob Allen ~ @akrabat
Slide 41
serverless.yml functions: list-todos: handler: “src/actions/listTodos.main” name: “todo-backend/list-todos” events: - http: path: /todos method: get add-todo: handler: src/actions/addTodo.main # etc Rob Allen ~ @akrabat
Slide 42
Deploy $ serverless deploy Serverless: Packaging Serverless: Compiling Serverless: Compiling Serverless: Compiling Serverless: Compiling Serverless: Compiling Serverless: Compiling Serverless: Deploying Serverless: Deploying Serverless: Deploying […]
service… Functions… Packages… API Gateway definitions… Rules… Triggers & Feeds… Service Bindings… Packages… Functions… API Gateway definitions…
Rob Allen ~ @akrabat
Slide 43
Resources • http://www.openwhisk.org • https://github.com/apache/incubator-openwhisk-workshop • https://serverless.com/framework/docs/providers/openwhisk Developing Serverless Applications by Raymond Camden Free via https://akrab.at/openwhiskbook
Rob Allen ~ @akrabat