The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless...

59
The Serverless Way Rob Allen November 2018

Transcript of The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless...

Page 1: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

The Serverless WayRob Allen

   

November 2018

Page 2: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Deployment options1. Physical servers2. Virtual machines3. Containers

Rob Allen ~ @akrabat

Page 3: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Container deployments1. Platform (e.g. Kubernetes)2. Application (e.g. Cloud Foundry)3. Serverless (e.g. OpenWhisk)

Rob Allen ~ @akrabat

Page 4: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Serverless? 

The first thing to know about serverlesscomputing is that "serverless" is a pretty badname to call it.

- Brandon Butler, Network World

Rob Allen ~ @akrabat

it's not that there's NoOps, it's just that someone else does it for you
Page 5: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

AKA: Functions as a Service• Code• Deployed to the cloud• Executed in response to an event• On-demand scaling• Pay for execution, not when idle

Rob Allen ~ @akrabat

Page 6: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Use-casesSynchronous

Service is invoked and provides immediate response(HTTP requests: APIs, chat bots)

Rob Allen ~ @akrabat

Page 7: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Use-casesSynchronous

Service is invoked and provides immediate response(HTTP requests: APIs, chat bots)

AsynchronousPush a message which drives an action later(web hooks, timed events, database changes)

Rob Allen ~ @akrabat

Page 8: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Use-casesSynchronous

Service is invoked and provides immediate response(HTTP requests: APIs, chat bots)

AsynchronousPush a message which drives an action later(web hooks, timed events, database changes)

StreamingContinuous data flow to be processed

Rob Allen ~ @akrabat

Page 9: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Benefits• No need to think about servers• Concentrate on application code• Pay only for what you use, when you use it• Language agnostic:

• NodeJS, Python, Swift, Go, Java, C#, PHP, Ruby, etc

Rob Allen ~ @akrabat

Page 10: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Challenges• Start up latency• Time limit• State is external• DevOps is still a thing

Rob Allen ~ @akrabat

Page 11: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

It's about value

Rob Allen ~ @akrabat

Page 12: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

When should you use serverless?• Occasional server needs on a static site• Variable traffic levels• Additional compute without extending current platform• Responding to web hooks• Any web app that you want to be cheaper to run!

Rob Allen ~ @akrabat

Page 13: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Serverless providers

Rob Allen ~ @akrabat

Page 14: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Hello WorldAWS Lambda:def my_handler(event, context): name = event.get("name", "World") message = 'Hello {}!'.format(name)

return {'message': message}

Rob Allen ~ @akrabat

Page 15: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Hello WorldApache OpenWhisk:def main(args): name = args.get("name", "World") message = 'Hello {}!'.format(name)

return {'message': message}

Rob Allen ~ @akrabat

Page 16: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Hello WorldGoogle Cloud Functionsdef hello_http(request): name = request.json.get("name", "World") message = 'Hello {}!'.format(name)

return message

Rob Allen ~ @akrabat

Page 17: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Hello WorldAzure Cloud Functionsimport azure.functions as func

def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> str:

name = req.params.json.get("name", "World") message = 'Hello {}!'.format(name)

msg.set(message) return message

Rob Allen ~ @akrabat

Page 18: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Apache OpenWhisk

Rob Allen ~ @akrabat

OpenWhisk is a cloud platform that executes code
in response to events
Page 19: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Concepts

Rob Allen ~ @akrabat

Terminology
**Trigger:**
an event (e.g. incoming HTTP request)
**Rule:**
Map a trigger to an action
**Action:**
A function (with optional parameters)
**Package:**
Group of actions and parameters
**Sequence:**
A set of actions in a row
Page 20: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

The anatomy of an action

def main(args): # Marshall inputs from event parameters name = args.get("name", "World") # Do the work message = 'Hello {}!'.format(name) # return result return {'message': message}

Rob Allen ~ @akrabat

Page 21: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

The anatomy of an action

def main(args): # Marshall inputs from event parameters name = args.get("name", "World") # Do the work message = 'Hello {}!'.format(name) # return result return {'message': message}

Rob Allen ~ @akrabat

Page 22: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

The anatomy of an action

def main(args): # Marshall inputs from event parameters name = args.get("name", "World") # Do the work message = 'Hello {}!'.format(name) # return result return {'message': message}

Rob Allen ~ @akrabat

Page 23: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

The anatomy of an action

def main(args): # Marshall inputs from event parameters name = args.get("name", "World") # Do the work message = 'Hello {}!'.format(name) # return result return {'message': message}

Rob Allen ~ @akrabat

Page 24: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Creating your action in OpenWhisk$ wsk action create hello hello.pyok: updated action hello

Rob Allen ~ @akrabat

Page 25: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Running your action in OpenWhisk$ wsk action create hello hello.pyok: updated action hello

$ wsk action invoke hello --result --param name Rob

Rob Allen ~ @akrabat

Page 26: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Running your action in OpenWhisk$ wsk action create hello hello.pyok: updated action hello

$ wsk action invoke hello --result --param name Rob{ "message": "Hello Rob!"}

Rob Allen ~ @akrabat

Page 27: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Access from the webAPI Gateway provides:• API routing• Rate limiting• Authentication• Custom domains

Rob Allen ~ @akrabat

Page 28: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

API Gateway

Rob Allen ~ @akrabat

Page 29: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

API Gateway

$ wsk api create /myapp /hello GET hello

Rob Allen ~ @akrabat

Page 30: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

API Gateway

$ wsk api create /myapp /hello GET hellook: created API /myapp/hello GET for action /_/hello

Rob Allen ~ @akrabat

Page 31: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

API Gateway

$ wsk api create /myapp /hello GET hellook: created API /myapp/hello GET for action /_/hello

$ curl https://example.com/myapp/hello?name=Rob

Rob Allen ~ @akrabat

Page 32: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

API Gateway

$ wsk api create /myapp /hello GET hellook: created API /myapp/hello GET for action /_/hello

$ curl https://example.com/myapp/hello?name=Rob{ "message": "Hello Rob!"}

Rob Allen ~ @akrabat

Page 33: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Event providers (feeds)• Webhook triggers:

• GitHub• Slack

• Databases: CouchDB• Message Queues: Kafka• Mobile Push• Scheduled

Rob Allen ~ @akrabat

Page 34: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Architecture

Rob Allen ~ @akrabat

Page 35: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Monolith architecture

Rob Allen ~ @akrabat

Page 36: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Serverless architecture

Rob Allen ~ @akrabat

Page 37: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Serverless architecture pattern

Rob Allen ~ @akrabat

Page 38: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Functions arekey

Rob Allen ~ @akrabat

Page 39: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Functions are theUnit of Deployment

Rob Allen ~ @akrabat

We can't deploy anything smaller than a function
They need to be right-sized for your application's tasks
Page 40: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Functions are theUnit of Scale

Rob Allen ~ @akrabat

Your provider will scale each function according to demand
Page 41: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Functions areStateless

Rob Allen ~ @akrabat

Underlying compute infrastructure can change at any time
Local file system & child processes only exist for life of request
Page 42: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Functions haveStructure

Rob Allen ~ @akrabat

Page 43: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

StructureIf it's non-trivial, software engineering principles apply!

• Use multiple methods

Rob Allen ~ @akrabat

Page 44: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

StructureIf it's non-trivial, software engineering principles apply!

• Use multiple methods• Use multiple files

Rob Allen ~ @akrabat

Page 45: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

StructureIf it's non-trivial, software engineering principles apply!

• Use multiple methods• Use multiple files• Integrate reusable dependencies

Rob Allen ~ @akrabat

Page 46: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

SeparationSeparate the action handler from your business logic

Rob Allen ~ @akrabat

Page 47: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

SeparationSeparate the action handler from your business logic

Rob Allen ~ @akrabat

Page 48: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Example from Adobe CIF-Magentofunction postCoupon(args) { const validator = new InputValidator(args, ERROR_TYPE) .checkArguments().mandatoryParameter('id') if (validator.error) { return validator.buildErrorResponse(); }

const cart = new MagentoCart(args, cartMapper.mapCart, 'guestcart'); return cart.byId(args.id).addCoupon(args.code).then(function () { return cart.byId(args.id).get(); }).catch(error => { return cart.handleError(error); });}

Rob Allen ~ @akrabat

Page 49: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Example from Adobe CIF-Magentofunction postCoupon(args) { const validator = new InputValidator(args, ERROR_TYPE) .checkArguments().mandatoryParameter('id') if (validator.error) { return validator.buildErrorResponse(); }

const cart = new MagentoCart(args, cartMapper.mapCart, 'guestcart'); return cart.byId(args.id).addCoupon(args.code).then(function () { return cart.byId(args.id).get(); }).catch(error => { return cart.handleError(error); });}

Rob Allen ~ @akrabat

Validate your inputs
Page 50: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Example from Adobe CIF-Magentofunction postCoupon(args) { const validator = new InputValidator(args, ERROR_TYPE) .checkArguments().mandatoryParameter('id') if (validator.error) { return validator.buildErrorResponse(); }

const cart = new MagentoCart(args, cartMapper.mapCart, 'guestcart'); return cart.byId(args.id).addCoupon(args.code).then(function () { return cart.byId(args.id).get(); }).catch(error => { return cart.handleError(error); });}

Rob Allen ~ @akrabat

Do the work IN ANOTHER MODULE
Page 51: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Example from Adobe CIF-Magentofunction postCoupon(args) { const validator = new InputValidator(args, ERROR_TYPE) .checkArguments().mandatoryParameter('id') if (validator.error) { return validator.buildErrorResponse(); }

const cart = new MagentoCart(args, cartMapper.mapCart, 'guestcart'); return cart.byId(args.id).addCoupon(args.code).then(function () { return cart.byId(args.id).get(); }).catch(error => { return cart.handleError(error); });}

Rob Allen ~ @akrabat

Return the result
Page 52: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Serverless state machines

Rob Allen ~ @akrabat

Page 53: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Serverless state machines

Rob Allen ~ @akrabat

Page 54: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Rob Allen ~ @akrabat

Page 55: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Rob Allen ~ @akrabat

Page 56: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Infrastructure as Codecomposer.sequence( composer.if( 'binday/authenticate', composer.if( 'binday/validate', composer.if( 'binday/is_intent_setday', 'binday/set_bin_day', 'binday/get_next_bin_day' ), 'binday/send_failure' ), 'binday/send_failure' ), 'binday/format_output_for_alexa')

Rob Allen ~ @akrabat

Follow the Infrastructure As Code practice
If it's not in Version Control, it can't be reproduced
Page 57: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Tips & tricks• Make troubleshooting easier:

• Ability to disable event triggers• Identifiers that run throughout functions for a single

"operation"• Don't forget HTTP circuit breakers - retry with a back-off

algorithm• Tune memory settings for each function - affects price (&

performance)

Rob Allen ~ @akrabat

Page 58: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

To sum up

Rob Allen ~ @akrabat

Page 59: The Serverless Way - akrabat.com€¦ · Serverless? The first thing to know about serverless computing is that "serverless" is a pretty bad name to call it. - Brandon Butler, Network

Thank you!

Rob Allen ~ @akrabat