Jan 22, 2018
© 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
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Photo sharing app
Metadata
DynamoDB
Extract
Recognize
Store
…
Pictures
S3 Bucket
© 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
© 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
© 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
© 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();
}
© 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?
© 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
© 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?
© 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?
© 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
© 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
© 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
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved
ExtractImageMetadata
ImageTypeCheck
TransformMetadata
Rekognition Thumbnail
StoreMetadata
Start
End
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Metadata
DynamoDB
Extract
Recognize
Store
…
Pictures
S3 Bucket
Photo sharing app
© 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
© 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
© 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
© 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
© 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 …”,
…
}
}
© 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 …”,
…
}
}
© 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/
© 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
© 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
© 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
© 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
© 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
© 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
© 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/
© 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
© 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" }
]
},
© 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
“
© 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
“
© 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
“
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved
13 AWS Lambda Task States
6 Choice States
1 Fail State
DevOps Automation
© 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
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved
Input/Output Processing
State [x]"InputPath" "ResultPath" "OutputPath"
Input
Output
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved
InputPath example
{
”s3key”:”apple.png”,
”timestamp”:12324
}
Input
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved
InputPath example
{
”s3key”:”apple.png”,
”timestamp”:12324
}
"InputPath”:“$”
Input
© 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
}
© 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
}
© 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
}
© 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
© 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
© 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:
© 2017, Amazon Web Services, Inc. or its Affiliates. All rights reservedhttps://states-language.net/spec
© 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:
© 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”
© 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