Top Banner
60

Raleigh DevDay 2017: Serverless orchestration with AWS step functions

Jan 22, 2018

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Raleigh DevDay 2017: Serverless orchestration with AWS step functions
Page 2: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

WIFI: awsDevDay | PASS: CodeHappy

U P N E X T :

Serverless Orchestration with

AWS Step Functions

Page 3: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

T H A N K S T O O U R F R I E N D S A T :

Page 4: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Angela Wang, AWS Solutions Architect

August 1, 2017

Serverless Orchestration with

AWS Step Functions

Page 5: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

I have this idea

Page 6: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Photo sharing app

Metadata

DynamoDB

Extract

Recognize

Store

Pictures

S3 Bucket

Page 7: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image, done, err) {

// metadata

var filemeta = extractImageMetadata(image);

if (!isSupported(metadata)) { err() };

var metadata = transformMetadata(filemeta);

done();

}

Extract the image metadata

Page 8: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image, done, err) {

// metadata

var filemeta = extractImageMetadata(image);

if (!isSupported(metadata)) { err() };

var metadata = transformMetadata(filemeta);

// image recognition

var labels = recognizePicture(image);

storeImageMetadata([metadata, labels])

done();

}

Use deep learning voodoo for object detection

Page 9: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image, done, err) {

// metadata

var filemeta = extractImageMetadata(image);

if (!isSupported(metadata)) { err() };

var metadata = transformMetadata(filemeta);

// image recognition

var labels = recognizePicture(image);

storeImageMetadata([metadata, labels])

// generate thumbnail

generateThumbnail(image);

done();

}

Generate thumbnail and store in S3

Page 10: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image, done, err) {

// metadata

var filemeta = extractImageMetadata(image);

if (!isSupported(metadata)) { err() };

var metadata = transformMetadata(filemeta);

// image recognition

var labels = recognizePicture(image);

storeImageMetadata([metadata, labels])

// generate thumbnail

generateThumbnail(image);

done();

}

Page 11: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image, done, err) {

// metadata

var filemeta = extractImageMetadata(image);

if (!isSupported(metadata)) { err() };

var metadata = transformMetadata(filemeta);

// image recognition

var labels = recognizePicture(image);

storeImageMetadata([metadata, labels])

// generate thumbnail

generateThumbnail(image);

done();

}Everything should really be Microservices / Lambda functions, right?

Page 12: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image, done, err) {

// metadata

extractImageMetadata(image)

.then((img, meta) => isSupported(metadata) … )

.then((img, meta) => transform(metadata) … )

// image recognition

.then((img, meta) => recognizePicture(img) … )

.then((img, meta, labels) => storeImageMetadata … )

// generate thumbnail

.then((img, meta, labels) =>

generateThumbnail(image) … );

}

Make sure we have our callbacks

Page 13: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image) {

// metadata

return extractImageMetadata(image)

.then((img, meta) => isSupported(metadata) … )

.then((img, meta) => transform(metadata) … )

// image recognition

.then((img, meta) => rekognizePicture(img) … )

.then((img, meta, labels) => storeImageMetadata … )

// generate thumbnail

.then((img, meta, labels) =>

generateThumbnail(image) … );

}

What happens if that line fails?

Page 14: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image) {

// metadata

return extractImageMetadata(image)

.then((img, meta) => isSupported(metadata) … )

.then((img, meta) => transform(metadata) … )

// image recognition

.then((img, meta) => rekognizePicture(img) … )

.then((img, meta, labels) => storeImageMetadata … )

// generate thumbnail

.then((img, meta, labels) =>

generateThumbnail(image) … );

}

Can these two be done in parallel?

Page 15: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image) {

// metadata

return extractImageMetadata(image)

.then((img, meta) => isSupported(metadata) … )

.then((img, meta) => transform(metadata) … )

// image recognition

.then((img, meta) => rekognizePicture(img) … )

.then((img, meta, labels) => storeImageMetadata … )

// generate thumbnail

.then((img, meta, labels) =>

generateThumbnail(image) … );

}

This code is “dumb” (but hard to get right)

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition

StoreMetadata

Thumbnail

Page 16: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image) {

// metadata

return extractImageMetadata(image)

.then((img, meta) => isSupported(metadata) … )

.then((img, meta) => transform(metadata) … )

// image recognition

.then((img, meta) => rekognizePicture(img) … )

.then((img, meta, labels) => storeImageMetadata … )

// generate thumbnail

.then((img, meta, labels) =>

generateThumbnail(image) … );

}

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition

StoreMetadata

Thumbnail

Queues and Notification

Page 17: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

function handle(image) {

// metadata

return extractImageMetadata(image)

.then((img, meta) => isSupported(metadata) … )

.then((img, meta) => transform(metadata) … )

// image recognition

.then((img, meta) => rekognizePicture(img) … )

.then((img, meta, labels) => storeImageMetadata … )

// generate thumbnail

.then((img, meta, labels) =>

generateThumbnail(image) … );

}

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition

Thumbnail

StoreMetadata

Page 18: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Page 19: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Metadata

DynamoDB

Extract

Recognize

Store

Pictures

S3 Bucket

Photo sharing app

Page 20: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Photo sharing app

Pictures

S3 BucketStart Workflow

Lambda

Processing Workflow

Step Functions

Extract

Lambda

Recognize

Lambda

Store

Lambda

Metadata

DynamoDB

Object Detection

Rekognition

Page 21: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Demo

Page 22: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

“I want to sequence functions”

“I want to run functions in parallel”

“I want to select functions based on data”

“I want to retry functions”

“I want try/catch/finally”

“I have code that runs for hours”

• Scales out

• Doesn’t lose state

• Deals with errors/timeouts

• Easy to build & operate

• Auditable

Page 23: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

{

"StartAt": "ExtractImageMetadata",

"States": {

"ExtractImageMetadata": {

"Type": "Task",

"Resource": "arn:aws:lambda:mars-ter …”,

"InputPath": "$",

"ResultPath": "$.result",

"End": true

}

}

}

Define in JSON and Then

Visualize in the Console

Page 24: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Execute One or One Million

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Page 25: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Seven State Types

Task: a single unit of work

Synchronous: Lambda

{

"ExtractImageMetadata": {

"Type": "Task",

"Resource": "arn:aws:lambda:mars-ter …”,

}

}

Page 26: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Seven State Types

Task: a single unit of work

Asynchronous: “Activity Task”, Polled by workers

{

"ExtractImageMetadata": {

"Type": ”Task",

"Resource": "arn:aws:states:mars-ter …”,

}

}

Page 27: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Use Activity Task for Manual Approval

Step Functions

Poll

taskToken

Amazon

SES

Poller

Approver

Send success

taskTokenAPI

Gatewayhttps://aws.amazon.com/blogs/compute/

implementing-serverless-manual-approval-steps-in-aws-

step-functions-and-amazon-api-gateway/

Page 28: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Use Activity Task for Calling Asynchronous APIs

Step Functions

Poll

taskToken

Amazon

Elastic

Transcoder

Poller

SNS

Send success

taskToken

Lambda Amazon Confidential

Page 29: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Seven State Types

NotSupported

Choice: Adds branching logic

{

"ExtractImageMetadata": {

"Type": ”Choice",

”Choices": [{

”Variable": ”…",

”StringEquals": ”…",

”Next": ”…",

}

],

”Default": ”NotSupported",

}

}

Task: a single unit of work

Page 30: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Seven State Types

NotSupported

Choice: Adds branching logic

Task: a single unit of work

Fail: Stops an execution and mark as failure

Success: Stops an execution successfully

Page 31: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

StoreMetadata

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

Start

End

Seven State Types

Choice: Adds branching logic

Task: A single unit of work

Parallel: fork and join data across tasks

Fail: Stops an execution and mark as failure

Success: Stops an execution successfully

Page 32: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Seven State Types

Choice: a single unit of work

Task: a single unit of work

Parallel: fork and join data across tasks

Fail: Stops an execution and mark as failure

Success: Stops an execution successfully

Pass: passes inputs to its outputs

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Page 33: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Seven State Types

Choice: a single unit of work

Task: a single unit of work

Parallel: fork and join data across tasks

Fail: Stops an execution and mark as failure

Success: Stops an execution successfully

Pass: passes inputs to its outputs

Wait: wait until n seconds.

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Page 34: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Use Wait + Choice to Wait for AWS Batch Job

while:

sleep (30)

if status=done:

break

https://aws.amazon.com/blogs/compute/building-high-throughput-

genomics-batch-workflows-on-aws-workflow-layer-part-4-of-4/

Page 35: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Seven State Types

Choice: a single unit of work

Task: a single unit of work

Parallel: fork and join data across tasks

Fail: Stops an execution and mark as failure

Success: Stops an execution successfully

Pass: passes inputs to its outputs

Wait: wait until n seconds.

ExtractImageMetadata

ImageTypeCheck

TransformMetadata

Rekognition Thumbnail

StoreMetadata

Start

End

Page 36: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

"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 37: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

“I want to sequence functions”

With 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 38: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Order Fulfillment

Page 39: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

“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 40: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Business Processes

Page 41: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

“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 42: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

13 AWS Lambda Task States

6 Choice States

1 Fail State

DevOps Automation

Page 43: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Document Processing

Page 44: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Integrate with other AWS services

• Create state machines and Activities with AWS

CloudFormation

• Call AWS Step Functions with Amazon API Gateway

• Start state machines in response to events, or on a

schedule, with Amazon CloudWatch Events

• Monitor state machine executions with Amazon CloudWatch

• Log API calls with AWS CloudTrail

Page 45: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Input/Output Processing

State [x]"InputPath" "ResultPath" "OutputPath"

Input

Output

Page 46: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

InputPath example

{

”s3key”:”apple.png”,

”timestamp”:12324

}

Input

Page 47: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

InputPath example

{

”s3key”:”apple.png”,

”timestamp”:12324

}

"InputPath”:“$”

Input

Page 48: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

InputPath example

{

”s3key”:”apple.png”,

”timestamp”:12324

}

State [x]"InputPath”:“$”

Input

Input received by state x:

{

”s3key”:”apple.png”,

”timestamp”:12324

}

Page 49: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

InputPath example

"InputPath”:“$.s3key”

{

”s3key”:”apple.png”,

”timestamp”:12324

}

State [x]"InputPath”:“$”

Input

Input received by state x:

{

”s3key”:”apple.png”,

”timestamp”:12324

}

Page 50: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

InputPath example

State [y]"InputPath”:“$.s3key”

{

”s3key”:”apple.png”,

”timestamp”:12324

}

”apple.png”

State [x]"InputPath”:“$”

Input

Input received by state y:

Input received by state x:

{

”s3key”:”apple.png”,

”timestamp”:12324

}

Page 51: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ResultPath example

{

”s3key”:”apple.png”,

”timestamp”:12324

}

Input

State [x]State output:

{“type”: “png"}

"InputPath”

: “$”

”ResultPath”:“$” { ”type”:”png}Output

Page 52: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

ResultPath example

{

”s3key”:”apple.png”,

”timestamp”:12324

}

Input

State [x]State output:

{“type”: “png"}

"InputPath”

: “$.s3key”

”ResultPath”:“$” { ”type”:”png}Output

”ResultPath”:“$.metadata”

{

”s3key”:”apple.png”,

”timestamp”:12324,

“metadata”: {

“type”: “png"}

}

Output

Page 53: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Where does transient application

state live?

In the machine, in JSON texts

passing from state to state.A:

Q:

Page 54: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reservedhttps://states-language.net/spec

Page 55: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Page 56: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

2.5¢

How much?

per thousand

state transitions

4,000 free

transitions/month

Free tier:

Page 57: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

“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 58: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

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

Resources

• Github repo:

https://github.com/awslabs/lambda-refarch-imagerecognition

• Blogs, reference architectures, tutorials on AWS Step

Functions:

https://aws.amazon.com/step-functions/getting-started

Page 59: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

Thank You!

Page 60: Raleigh DevDay 2017: Serverless orchestration with AWS step functions

Don’t Forget Evaluations!