aws step functions 実践
TRANSCRIPT
![Page 1: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/1.jpg)
AWS Step Functions 実践
AWS Premeir Night #3
クラスメソッド株式会社菊池 修治
![Page 2: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/2.jpg)
• 菊池 修治(きくち しゅうじ)–クラスメソッド AWS事業
– Solutions Architect
– AWS Certified in All 5!
– SIer à 製造業 à クラスメソッド
–好きなAWSサービス• VPC、S3、Step Functions
⾃⼰紹介
![Page 3: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/3.jpg)
Agenda
• AWS Step Functions とは
• Amazon State Language
• Step Functions の実⾏– State Machine の実⾏⽅法
– Activity Task の実⾏
![Page 4: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/4.jpg)
Agenda
• AWS Step Functions とは
• Amazon State Language
• Step Functions の実⾏– State Machine の実⾏⽅法
– Activity Task の実⾏
![Page 5: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/5.jpg)
AWS Step Functions
re:Invent 2016 にて発表!
![Page 6: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/6.jpg)
AWS Step Functions• 「Step Functionsやってみた」ブログ公開中
– http://dev.classmethod.jp/referencecat/aws-step-functions/
![Page 7: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/7.jpg)
AWS Step Functions
• 分散されたアプリケーションをビジュアルにコーディネート– Lambdaなどの複数アプリケーションを⼀連の
ワークフローとして定義・実⾏
–処理の流れ・ステップをビジュアル化
![Page 8: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/8.jpg)
AWS Step Functions
![Page 9: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/9.jpg)
AWS Step Functions
• ユースケース–シーケンシャルなアプリケーションの実⾏
–タスクの並列化
–処理の条件分岐
–エラーハンドリング
–⻑時間の実⾏
![Page 10: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/10.jpg)
AWS Step Functions
• 利点–アプリケーション間を疎結合に
–汎⽤性・再利⽤性の向上
–実⾏結果の管理
![Page 11: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/11.jpg)
AWS Step Functions
• コンポーネント– State Machine:定義された⼀連のフロー
– State Language:State Machineを定義する⾔語
![Page 12: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/12.jpg)
Agenda
• AWS Step Functions とは
• Amazon State Language
• Step Functions の実⾏– State Machine の実⾏⽅法
– Activity Task の実⾏
![Page 13: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/13.jpg)
Amazon State Language• JSONで記述
{
"Comment": "A Retry example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Retry": [
{
"ErrorEquals": ["HandledError"],
"IntervalSeconds": 1,
"MaxAttempts": 2,
"BackoffRate": 2.0
},
{
"ErrorEquals": ["States.TaskFailed"],
"IntervalSeconds": 30,
"MaxAttempts": 2,
"BackoffRate": 2.0
},
{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 5,
"MaxAttempts": 5,
"BackoffRate": 2.0
}
],
"End": true
}
}
}
![Page 14: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/14.jpg)
Amazon State Language• State (= Step)をつなげてフロー(State Machine)
を作成
• ⼀度作成したState Machineは変更・修正できない
![Page 15: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/15.jpg)
Amazon State Language• 7つの State– Task :アプリケーションの実⾏– Choice :分岐条件– Parallel :Taskの並列実⾏– Wait :待ち– Fail :異常終了– Succeed :正常終了– Pass :なにもしない
![Page 16: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/16.jpg)
Amazon State Language• 7つの State– Task :アプリケーションの実⾏– Choice :分岐条件– Parallel :Taskの並列実⾏– Wait :待ち– Fail :異常終了– Succeed :正常終了– Pass :なにもしない
![Page 17: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/17.jpg)
Amazon State Language• Task"TaskState": {
"Comment": "Task State example",
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloFunction"
"Next": "NextState",
"TimeoutSeconds": 300,
"Retry": [
{
"ErrorEquals": [ "ErrorA", "ErrorB" ],
"IntervalSeconds": 1,
"BackoffRate": 2,
"MaxAttempts": 2
}
]
}
![Page 18: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/18.jpg)
Amazon State Language• Task"TaskState": {
"Comment": "Task State example",
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloFunction"
"Next": "NextState",
"TimeoutSeconds": 300,
"Retry": [
{
"ErrorEquals": [ "ErrorA", "ErrorB" ],
"IntervalSeconds": 1,
"BackoffRate": 2,
"MaxAttempts": 2
}
]
}
![Page 19: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/19.jpg)
Amazon State Language• Task"TaskState": {
"Comment": "Task State example",
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloFunction"
"Next": "NextState",
"TimeoutSeconds": 300,
"Retry": [
{
"ErrorEquals": [ "ErrorA", "ErrorB" ],
"IntervalSeconds": 1,
"BackoffRate": 2,
"MaxAttempts": 2
}
]
}
![Page 20: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/20.jpg)
Amazon State Language• Task"TaskState": {
"Comment": "Task State example",
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloFunction"
"Next": "NextState",
"TimeoutSeconds": 300,
"Retry": [
{
"ErrorEquals": [ "ErrorA", "ErrorB" ],
"IntervalSeconds": 1,
"BackoffRate": 2,
"MaxAttempts": 2
}
]
}
![Page 21: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/21.jpg)
Amazon State Language• Choice
"ChoiceState": {"Type" : "Choice","Choices": [{"Variable": "$.foo","NumericEquals": 1,"Next": "FirstMatchState"
},{"Variable": "$.foo","NumericEquals": 2,"Next": "SecondMatchState”
}],"Default": "DefaultState"
},
• StringEquals• StringLessThan• StringGreaterThan• StringLessThanEquals• StringGreaterThanEquals• NumericEquals• NumericLessThan• NumericGreaterThan• NumericLessThanEquals• NumericGreaterThanEquals• BooleanEquals• TimestampEquals• TimestampLessThan• TimestampGreaterThan• TimestampLessThanEquals• TimestampGreaterThanEquals
![Page 22: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/22.jpg)
Amazon State Language• Choice "ChoiceStateX": {
"Type" : "Choice","Choices": [{"Not": {"Variable": "$.type","StringEquals": "Private”
},"Next": "Public”
},{"And": [{"Variable": "$.value","NumericGreaterThanEquals": 20
},{"Variable": "$.value","NumericLessThan": 30}
],"Next": "ValueInTwenties”
}],"Default": "DefaultState”},
• Not• Or• And
![Page 23: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/23.jpg)
Amazon State Language• Choice "ChoiceStateX": {
"Type" : "Choice","Choices": [{"Not": {"Variable": "$.type","StringEquals": "Private”
},"Next": "Public”
},{"And": [{"Variable": "$.value","NumericGreaterThanEquals": 20
},{"Variable": "$.value","NumericLessThan": 30}
],"Next": "ValueInTwenties”
}],"Default": "DefaultState”},
• Not• Or• And
![Page 24: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/24.jpg)
Amazon State Language• Choice "ChoiceStateX": {
"Type" : "Choice","Choices": [{"Not": {"Variable": "$.type","StringEquals": "Private”
},"Next": "Public”
},{"And": [{"Variable": "$.value","NumericGreaterThanEquals": 20
},{"Variable": "$.value","NumericLessThan": 30}
],"Next": "ValueInTwenties”
}],"Default": "DefaultState”},
• Not• Or• And
![Page 25: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/25.jpg)
Amazon State Language• Parallel–全ての分岐が完了
してから次のStateへ進む
"Parallel": {"Type": "Parallel","Next": "Final State","Branches": [{"StartAt": "Wait 20s","States": {"Wait 20s": {"Type": "Wait","Seconds": 20,"End": true
}}
},{"StartAt": "Wait 10s","States": {"Wait 10s": {"Type": "Wait","Seconds": 10,"End": true
}}
}]
},
![Page 26: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/26.jpg)
Amazon State Language• Wait– Seconds– SecondsPath– Timestamp– TimestampPath
"wait_ten_seconds" : {"Type" : "Wait","Seconds" : 10,"Next": "NextState”
}
"wait_until" : { "Type": "Wait", "Timestamp": "2016-03-14T01:59:00Z", "Next": "NextState”}
"wait_until" : {"Type": "Wait","TimestampPath": "$.expirydate","Next": "NextState”
}
![Page 27: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/27.jpg)
Agenda
• AWS Step Functions とは
• Amazon State Language
• Step Functions の実⾏– State Machine の実⾏⽅法
– Activity Task の実⾏
![Page 28: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/28.jpg)
State Machine の実⾏• start-execution API
Step FunctionsState Machine
start-execution
![Page 29: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/29.jpg)
State Machine の実⾏• start-execution API
Lambda Step FunctionsState Machine
start-execution
![Page 30: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/30.jpg)
State Machine の実⾏• start-execution API
APIGateway
S3
SNS
Cloud WatchEvent
Lambda Step FunctionsState Machine
start-execution
![Page 31: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/31.jpg)
State Machine の実⾏• 実⾏結果– State Machine ごとに管理
![Page 32: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/32.jpg)
State Machine の実⾏• 実⾏結果– State Machine の実⾏毎に管理
![Page 33: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/33.jpg)
State Machine の実⾏• 実⾏結果– State 毎に管理
![Page 34: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/34.jpg)
Agenda
• AWS Step Functions とは
• Amazon State Language
• Step Functions の実⾏– State Machine の実⾏⽅法
– Activity Task の実⾏
![Page 35: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/35.jpg)
Activity Taskの実⾏
• Lambda以外のアプリケーションの実⾏–EC2
–オンプレミス
• Pull型の実⾏–アプリケーションからポーリング
– LambdaはPush型
![Page 36: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/36.jpg)
Activity Taskの実⾏• Activityの作成
![Page 37: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/37.jpg)
Activity Taskの実⾏• Activityの作成
設定項⽬は名前だけARNが作成される
![Page 38: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/38.jpg)
Activity Taskの実⾏• State Machineの作成{
"Comment": "An example using a Task state.",
"StartAt": "getGreeting",
"Version": "1.0",
"TimeoutSeconds": 300,
"States":
{
"getGreeting": {
"Type": "Task",
"Resource": "arn:aws:states:eu_central-1:123456789012:activity:get-greeting",
"End": true
}
}
}
![Page 39: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/39.jpg)
Activity Taskの実⾏• アプリケーションからポーリング– Get-Activity-Task API
Polling
Get-Activity-Task--Activity ARN
--Worker Name (Option)
Worker
State Machine
![Page 40: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/40.jpg)
Activity Taskの実⾏• State Machine実⾏
InputTaskToken
In progress
Get-Activity-Task--Activity ARN
--Worker Name (Option)
Worker
State Machine
![Page 41: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/41.jpg)
Activity Taskの実⾏• 実⾏完了
Success
Send-Task-Success--Task-Token
--Output
Worker
State Machine
![Page 42: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/42.jpg)
Activity Taskの実⾏• 分散アプリケーション
Workers
State Machines
![Page 43: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/43.jpg)
まとめ• Step Functionsを使うことで分散アプリケーション
による複雑な処理を簡単に定義・実⾏・管理
• マイクロサービス化、サーバレス化
• Lambdaだけでなくサーバ上のアプリケーションも組み込み可能
![Page 44: AWS Step Functions 実践](https://reader036.vdocuments.net/reader036/viewer/2022082213/587d87351a28abcd648b55f1/html5/thumbnails/44.jpg)
classmethod.jp44