NEW LAUNCH! Serverless Apps with AWS Step Functions

49
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Tim Bray, Senior Principal Engineer December 1, 2016 NEW LAUNCH! Serverless Apps with AWS Step Functions SVR201

Transcript of NEW LAUNCH! Serverless Apps with AWS Step Functions

Page 1: NEW LAUNCH! Serverless Apps with AWS Step Functions

© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Tim Bray, Senior Principal Engineer

December 1, 2016

NEW LAUNCH!

Serverless Apps with

AWS Step Functions

SVR201

Page 2: NEW LAUNCH! Serverless Apps with AWS Step Functions

λλ

λDBMS

λλ

λλ

λ

λ λ

λ

λ

Queue

Modern

Serverless

app

Page 3: NEW LAUNCH! Serverless Apps with AWS Step Functions

Modern

Serverless

app

Page 4: NEW LAUNCH! Serverless Apps with AWS Step Functions

“I want to sequence functions”

“I want to select functions based on data”

“I want to retry functions”

“I want try/catch/finally”

Functions into apps

“I have code that runs for hours”

“I want to run functions in parallel”

Page 5: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 6: NEW LAUNCH! Serverless Apps with AWS Step Functions

Coordination by method call

λ λ

λ

λ

λλ

λ

Page 7: NEW LAUNCH! Serverless Apps with AWS Step Functions

Coordination by function chaining

λ

λλ

λλλ

λ

Page 8: NEW LAUNCH! Serverless Apps with AWS Step Functions

Coordination by database

λλ

λ

λ

λλ

λ

Page 9: NEW LAUNCH! Serverless Apps with AWS Step Functions

Coordination by queues

λ

λ

λ

λ λλ

λ

Page 10: NEW LAUNCH! Serverless Apps with AWS Step Functions

Coordination must-haves

• Scales out

• Doesn’t lose state

• Deals with errors/timeouts

• Easy to build & operate

• Auditable

Page 11: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 12: NEW LAUNCH! Serverless Apps with AWS Step Functions

“State Machine”(noun)

1. A concept used by CompSci

profs for torturing

undergrads, full of arcane

math.

2. A practical way to build and

manage modern Serverless

Cloud apps.

Dictionary

Page 13: NEW LAUNCH! Serverless Apps with AWS Step Functions

“I want to sequence functions”

AWS Step Functions, we can easily change and iterate on the application workflow of our food delivery service in order to optimize operations and continually improve delivery times. AWS Step Functions lets us dynamically scale the steps in our food delivery algorithm so we can manage spikes in customer orders and meet demand.

Mathias Nitzsche, CTO, foodpanda

Page 14: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 15: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 16: NEW LAUNCH! Serverless Apps with AWS Step Functions

var Twit = require('twit');

var T = new Twit(require('botfiles/config.js'));

exports.handler = function myBot(event, context, callback) {

var list = event.inputList;

var textToTweet = list.shift();

var output = { inputList: list }

T.post('statuses/update',

{ status: textToTweet }, function(err, reply) {

if (err) {

console.log('error:', err); context.fail();

} else {

console.log('tweet:', reply); callback(null, output);

}

});

};

Page 17: NEW LAUNCH! Serverless Apps with AWS Step Functions

(Demo)

Page 18: NEW LAUNCH! Serverless Apps with AWS Step Functions

How it works – API-level

Create State Machine – input is a

machine spec in a JSON DSL

Run Machine – input JSON blob,

returns Execution ID

List Executions

Describe Execution

Stop Execution

Page 19: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 20: NEW LAUNCH! Serverless Apps with AWS Step Functions

How it works – API-level

Create State Machine – input is a

machine spec in a JSON DSL

Run Machine – input JSON blob,

returns Execution ID

List Executions

Describe Execution

Stop Execution

Page 21: NEW LAUNCH! Serverless Apps with AWS Step Functions

https://states-language.net/spec

Page 22: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 23: NEW LAUNCH! Serverless Apps with AWS Step Functions

“I want to select functions based on data”

With AWS Step Functions, it was easy to build a multi-step product

updating system to ensure our database and website always have the

latest price and availability information.

AWS Step Functions let us replace a manual updating process with an

automated series of steps, including built-in retry conditions and error

handling, so we can reliably scale before a big show, and keep pace

with rapidly changing fashions.

Jared Browarnik, CTO, TheTake

Page 24: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 25: NEW LAUNCH! Serverless Apps with AWS Step Functions

"ChoiceState": {

"Type" : "Choice",

"Choices": [

{

"Variable": "$.productSource",

"StringEquals": "screen-scrape",

"Next": "ScreenScrapeState"

},{

"Variable": "$.productSource",

"StringEquals": "vendor-a",

"Next": "VendorA"

},{

"Variable": "$.productSource",

"StringEquals": "vendor-b",

"Next": "VendorB"

},

{

"Variable": "$.productSource",

"StringEquals": "vendor-c",

"Next":"VendorC"

},{

"Variable": "$.productSource",

"StringEquals": "updateProduct",

"Next":"UpdateProduct"

}

],

"Default": "ScreenScrapeState”

}

Page 26: NEW LAUNCH! Serverless Apps with AWS Step Functions

“I want to retry functions”

We get transient errors from a RESTful

service we depend on, once every four

or five times we call it. But if we keep

retrying, it eventually works.

Page 27: NEW LAUNCH! Serverless Apps with AWS Step Functions

{

"Comment": "Call out to a RESTful service",

"StartAt": "Call out",

"States": {

"Call out": {

"Type": "Task",

"Resource":

"arn:aws:lambda:eu-central-1:123456789012:function:RestCallout",

"Retry": [

{ "ErrorEquals": [ "HandledError" ], "MaxAttempts": 10 }

],

"End": true

}

}

}

Page 28: NEW LAUNCH! Serverless Apps with AWS Step Functions

(Demo)

Page 29: NEW LAUNCH! Serverless Apps with AWS Step Functions

“I want to run functions in parallel”

We want to send the captured image to

three OCR providers and take the result

with the highest confidence value.“

Page 30: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 31: NEW LAUNCH! Serverless Apps with AWS Step Functions

"Send for OCR": {

"Type": "Parallel",

"Next": "Pick result",

"Branches": [

{

"StartAt": "Prep1",

"States": {

"Prep1": {

"Type": "Pass",

"Result": { "inputList": [ "OCR Provider 1" ] },

"Next": "Go1"

},

"Go1": {

"Type": "Task",

"Resource": "arn:aws:lambda:eu-central-1:123456789012:function:StatesBot",

"End": true

}

}

Page 32: NEW LAUNCH! Serverless Apps with AWS Step Functions

Where does transient application

state live?

In the machine, in JSON texts

passing from state to state.A:

Q:

Page 33: NEW LAUNCH! Serverless Apps with AWS Step Functions

Input processing

{

"title": "Numbers to add",

"numbers": [ 3, 4 ]

}

{

"Type": "Task",

"InputPath": "$.numbers",

"Resource": "arn:aws:lambda…"

[ 3, 4 ]

Raw input:

State spec:

Task input:

Page 34: NEW LAUNCH! Serverless Apps with AWS Step Functions

Input processing

Q: InputPath not provided?

A: State gets raw input as-is.

Q: InputPath is null?

A: State gets an empty JSON object: {}

Q: InputPath produces plural output?

A: State gets it wrapped in a JSON array.

Page 35: NEW LAUNCH! Serverless Apps with AWS Step Functions

Result placement{

"title": "Numbers to add",

"numbers": [ 3, 4 ]

}

{

"Type": "Task",

"InputPath": "$.numbers",

"ResultPath": "$.sum”,

Raw input:

State spec:

Output: {

"title": "Numbers to add",

"numbers": [ 3, 4 ],

”sum": 7

}

Page 36: NEW LAUNCH! Serverless Apps with AWS Step Functions

Result placement

Q: ResultPath not provided?

A: Input discarded, raw output used.

Q: ResultPath is null?

A: State input is state output.

Q: ResultPath produces plural output?

A: Not allowed, validator won’t accept.

Page 37: NEW LAUNCH! Serverless Apps with AWS Step Functions

“I want try/catch/finally”

AWS Step Functions makes it simple to coordinate information

from many different infrastructure systems using easy to design

workflows and create a more intelligent monitoring system for our

Platform as a Service (PaaS).

With AWS Step Functions, we can reliably automate monitoring

decisions and actions in order to reduce human intervention by

over 60%, which improves infrastructure operation productivity and

customer application availability on our platform.

Pedro Pimenta, VP R&D, OutSystems

Page 38: NEW LAUNCH! Serverless Apps with AWS Step Functions

13 AWS Lambda Task States

6 Choice States

1 Fail State

“I want try/catch/finally”

Page 39: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 40: NEW LAUNCH! Serverless Apps with AWS Step Functions

"Access Media": {

"Type": "Task",

"Resource": "arn:aws:lambda:eu-central-1:123456789012:function:FindMedia",

"TimeoutSeconds": 2,

"Next": "Graceful Exit",

"Retry": [

{

"ErrorEquals": [ "States.Timeout" ],

"IntervalSeconds": 2, "MaxAttempts": 2, "BackoffRate": 1.5

}

],

"Catch": [

{ "ErrorEquals": [ "States.ALL" ], "Next": "Clean Up" }

]

},

Page 41: NEW LAUNCH! Serverless Apps with AWS Step Functions

“I have code that runs for hours”

We need to gather data from our

production line, in units of 8-hour shifts.“

Page 42: NEW LAUNCH! Serverless Apps with AWS Step Functions

More APIs

Register Activity Task - Returns ARN

Poll For task (by ARN)

Report Success

Report Failure

Report Heartbeat

Page 43: NEW LAUNCH! Serverless Apps with AWS Step Functions

"NextShift": {

"Type": "Wait",

"TimestampPath": "$.ShiftStart",

"Next": "Gather Plant Data"

},

"Gather Plant Data": {

"Type": "Task",

"Resource":

"arn:aws:states:ap-northeast-1:123456789012:activity:PlWatch",

"TimeoutSeconds": 30000,

"HeartBeatSeconds": 120,

"Next": "Clean up"

}

Page 44: NEW LAUNCH! Serverless Apps with AWS Step Functions

“I want to sequence functions”

“I want to select functions based on data”

“I want to retry functions”

“I want try/catch/finally”

Is this you?

“I have code that runs for hours”

“I want to run functions in parallel”

Page 45: NEW LAUNCH! Serverless Apps with AWS Step Functions
Page 46: NEW LAUNCH! Serverless Apps with AWS Step Functions

2.5¢

How much?

per thousand

state transitions

4,000 free

transitions/month

Free tier:

Page 47: NEW LAUNCH! Serverless Apps with AWS Step Functions

Related Sessions

• CMP319: Building Distributed Applications with AWS

Step Functions (Thursday, 5:30 PM, Venetian, L4, Delfino 4004)

• In the hallway outside this room, starting now!

Page 48: NEW LAUNCH! Serverless Apps with AWS Step Functions

Thank you!

[email protected]

Page 49: NEW LAUNCH! Serverless Apps with AWS Step Functions

Remember to complete

your evaluations!